2

My Makefile looks like this :

all:main.cpp
    g++ -I../../DrAPI/ -Wl,--no-as-needed -ldl -lrt -o SampleApp01 $< 
clean:
    rm -f SampleApp01

this is what I would do without Makefile:

gcc main.cpp -o test $(pkg-config --cflags --libs libmongoc-1.0)

now because of importing DrAPI I have to use Makefile to include that API as well, but I would lose libmongoc-1.0 without pkg-config. In that case, how should I add $(pkg-config --cflags --libs libmongoc-1.0) into my Makefile so that it works?

dome some
  • 479
  • 7
  • 22
  • http://stackoverflow.com/questions/28533059/how-to-use-pkg-config-in-make. – Eric Jan 24 '17 at 18:19
  • Summary from the linked answer: you may also use $(shell [command[) if the backticks do not work. – MakisH Mar 05 '19 at 06:50
  • This use of `pkg-config` is not properly used, only half way there. You only add libraries with the `-l` switch, but not in which directory they are stored, which is the `-L` switch. It might, or might not work. Depending how your libraries you use are set up. – Anders Apr 14 '21 at 13:00

3 Answers3

4

I would have made a Makefile that looks like this, so it use the rules that are already in make(1). Try make -f /dev/null --print-data-base to see them. Suggest grep-ing COMPILE.c and LINK.c to get the important stuff. COMPILE.cpp, COMPILE.C and COMPILE.cxx are for c++ and COMPILE.c are for c.

#!/bin/make
# Makefile
# 2020-02-10 Anders Jackson

# Basic setup
CXX       = g++
CXXFLAGS  = -c -g -Wall -Wextra
CXXFLAGS += -I../../DrAPI/
LDFLAGS   =
LDLIBS    = -ldl -lrt

# `pkg-config --list-all | grep -e mongo` will find if installed
# Here is the libmongoc-1.0 setup.
CXXFLAGS += `pkg-config --cflags          libmongoc-1.0`
LDFLAGS  += `pkg-config --libs-only-L     libmongoc-1.0`
LDLIBS   += `pgk-config --libs-only-other libmongoc-1.0`
LDLIBS   += `pkg-config --libs-only-l     libmongoc-1.0`

# Adjusting for this project
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)

EXECS = main

all: $(EXECS)      # Link exec from objects
$(OBJECTS):        # Make all .o from .cpp (no local header files)

clean:
      -rm $(OBJECTS)
      -rm $(EXECS)
# eof

Notice that here there that object files only has dependencies on the source file. If a source file has some local header file dependencies, you need to add them manually.

This file also see to that when linking, those -L switches are before object files and -l switches are after. If you try to just use just one pkg-config --libs in LDFLAGS, it will not work. I also just add one thing at the time to the variables with +=, like for LDLIBS, as it makes it easier to see what is added.

The built in rules are:

OUTPUT_OPTION = -o $@
# Compilation for C and C++
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $(OUTPUT_OPTION) $<
COMPILE.cpp = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $(OUTPUT_OPTION) $<
# Linking for C and C++
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@
LINK.cpp = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@

It helps to understand which variable goes where.

Anders
  • 520
  • 4
  • 11
1

all:main.cpp g++ -I../../DrAPI/ `pkg-config --cflags --libs libmongoc-1.0` -Wl,--no-as-needed -ldl -lrt -o SampleApp01 $< clean: rm -f SampleApp01

igagis
  • 1,959
  • 1
  • 17
  • 27
  • I don't understand your answer, can you be more specific on how you add this to a makefile ? Thank you – Ilan Mar 23 '18 at 19:28
  • using the back tick ` you can invoke another shell command and substitute its result to the place. I'd ask you to be more specific also about what exactly you don't understand. – igagis Mar 23 '18 at 21:23
  • Thank you very much, It is the first time I'm using a pkg-config in a makefile, and I'm not sure on how to use the synthax, to have it work properly. So I should I use your synthax after LIBS= in my makefile – Ilan Mar 24 '18 at 10:38
  • This is not about `makefile`, this is about `shell`. But yes, you can try using it when assigning `LIBS` variable – igagis Mar 24 '18 at 16:09
1

When I compile my C code using the GTK+ library I create a variable called GTK and set it to pkg-config like this:

GTK = `pkg-config --cflags --libs gtk+-3.0`

Compiling the code using:

$(CC) $(CFLAGS) $(OBJECTS) $(GTK) -o $@

Where CC = gcc, CFLAGS are some other compiler flags (like -I ../../DrAPI) and OBJECTS are the source and object files.

This way your makefile could look like this:

DRAPI = `pkg-config --cflags --libs libmongoc-1.0`

all:main.cpp
    g++ -I../../DrAPI/ -Wl,--no-as-needed -ldl -lrt $(DRAPI) -o SampleApp01 $< 
clean:
    rm -f SampleApp01

EDIT: Forget about this. Use a CFLAGS variable and store pkg-config --cflags [some library, eg. gtk+-3.0] and a LIBS variable to store pkg-config --libs [some library again]. Now your makefile could look like this:

CFLAGS = -I../../DrAPI/ -Wl,--no-as-needed -ldl -lrt `pkg-config --cflags libmongoc-1.0`
LIBS = `pkg-config --libs libmongoc-1.0`

all:main.cpp
    g++ $(CFLAGS) $(LIBS) -o SampleApp01 $< 
clean:
    rm -f SampleApp01

Hope this helps!

kwyntes
  • 1,045
  • 10
  • 27
  • I would just set `CC=g++` and then all is set up. The `all` rule should point to the binary to build, not the source. So `all:main` would be all needed for `all`. No need to tell `make` how to compile and link c code. It already know that. – Anders Feb 09 '20 at 22:31