Debug a makefile with pkgconfig - c++

.
Novice to makefile. I am trying out the Gstreamer library. Need to issue a command to the CLI as
'gcc basic-tutorial-1.c -o basic-tutorial-1 `pkg-config --cflags --libs gstreamer-1.0`
I am trying to write a make file to simplify things
And my makefile is as follows
CC = gcc
CFLAGS = `pkg-config --cflags gstreamer-1.0`
LDFLAGS = `pkg-config --libs gstreamer-1.0`
CLIBS = `pkg-config --libs gstreamer-1.0`
SOURCE = basic-tutorial-1.c
TARGET = demo
video : $(TARGET).o
$(CC) $(LDFLAGS) -o $(TARGET).o
$(TARGET).o : $(SOURCE)
$(CC) $(CFLAGS) -c $(SOURCE)
clean:
rm -f $(TARGET) $(OBJECTS)
It gives me an error message
make: Entering directory '/home/test/VSCode/C++/Gstreamer'
gcc `pkg-config --cflags gstreamer-1.0` -c basic-tutorial-1.c
gcc `pkg-config --libs gstreamer-1.0` -o demo.o
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
makefile:14: recipe for target 'video' failed
make: *** [video] Error 1
make: Leaving directory '/home/test/VSCode/C++/Gstreamer'
What should i do next?
Thanks

Check if you have the main() function in the source files being compiled

For GNU make,
CFLAGS = `pkg-config --cflags gstreamer-1.0`
is wrong, and you should code:
CFLAGS = $(shell pkg-config --cflags gstreamer-1.0) -Wall -Wextra -g
Read the documentation of GNU make and try make -p

This recipe is incomplete:
video : $(TARGET).o
$(CC) $(LDFLAGS) -o $(TARGET).o
What are you compiling? If $(TARGET).o is a dependency, why is it listed as the output of $(CC)? Not sure what you really want here, but perhaps:
$(CC) $(TARGET).o -o video $(LDFLAGS)

Related

Makefile to compile a class with main opencv

I hope you're all fine.
I'm learning opencv (c++) currently and I'm trying to compile a "main.cpp" with a class called "Algorithms"(.h and .cpp) and for some reason I get an error like this one when trying :
g++ `pkg-config --cflags opencv4` `pkg-config --libs opencv4` src/Algorithms.cpp -o obj/Algorihtms.o -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [makefile:15: obj/Algorithms.o] Error 1
Here is my makefile:
CFLAGS = `pkg-config --cflags opencv4`
LIBS = `pkg-config --libs opencv4`
B = bin
O = obj
S = src
FLAGS = -c -Wall
all: $(O) $(B) $(B)/main
$(B)/main: $(O)/Algorithms.o
g++ $(FLAGS) -ggdb $(S)/main.cpp -o $(O)/main.o
$(O)/Algorithms.o:
g++ $(CFLAGS) $(LIBS) $(S)/Algorithms.cpp -o $(O)/Algorihtms.o -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
$(O):
mkdir $(O)
$(B):
mkdir $(B)
clean: $(O) $(B)
rm -rf $(O)
rm -rf $(B)
(Opencv is installed and I know that if I compile "main.cpp" alone it works, but I can't get "main.cpp" and the class "Algorithm" to compile simultaneously.)
Thanks a lot.
If you do not want to link to an executable, you need to make GCC aware. The default is to compile and link. Use option -c:
$(O)/Algorithms.o:
g++ -c $(CFLAGS) $(LIBS) $(S)/Algorithms.cpp -o $(O)/Algorihtms.o -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
From the manpage:
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The
"overall options" allow you to stop this process at an intermediate stage. For example, the -c
option says not to run the linker. Then the output consists of object files output by the
assembler.
However in your case, as mentioned in my comment, the question is why you need an intermediate object file at all.
Answering to my own question.
I don't know why I didn't figure that out immediately in my makefile.
Whatever, here is a working makefile that does the job :
CFLAGS = `pkg-config --cflags opencv4`
LIBS = `pkg-config --libs opencv4`
B = bin
O = obj
S = src
FLAGS = -c -Wall
all: $(O) $(B) $(B)/main
$(B)/main: $(O)/Algorithms.o
g++ $(CFLAGS) $(LIBS) $(S)/main.cpp -o $(B)/main $(O)/*.o -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
$(O)/Algorithms.o:
g++ $(FLAGS) $(CFLAGS) $(LIBS) $(S)/Algorithms.cpp -o $(O)/Algorithms.o
$(O):
mkdir $(O)
$(B):
mkdir $(B)
clean: $(O) $(B)
rm -rf $(O)
rm -rf $(B)
I'm sure this makefile is not the most efficient and it can be improved in many ways.

Compile a c++ program on PC to communicate with DJI phantom 4 pro

I am trying to receive on my PC(linux mint), a streaming from the camera of a DJI phantom 4 using OpenCV.
The example I am following is : https://developer.dji.com/guidance-sdk/documentation/tutorials/index.html (I am following the linux part)
I copied the Copy libDJI_guidance.so in /usr/local/lib and I checked, it is there.
The makefile is :
#define a compiler
CXX = g++
#define target name
TARGET = main
#define dependencies of target
OBJECTS = main.o DJI_utility.o
#define the Include and Library path
CFLAGS = -g -Wall -I/usr/local/include -I../../../include
LDFLAGS = -Wl,-rpath,./ -lpthread -lrt -L./ -L/usr/local/lib/ -lDJI_guidance -lusb-1.0 `pkg-config --cflags --libs opencv`
$(TARGET) : $(OBJECTS)
$(CXX) -o $(TARGET) $(OBJECTS) $(LDFLAGS)
main.o : main.cpp DJI_utility.h
$(CXX) $(CFLAGS) -c main.cpp DJI_utility.h
DJI_utility.o : DJI_utility.cpp DJI_utility.h
$(CXX) $(CFLAGS) -c DJI_utility.cpp DJI_utility.h
clean:
rm -rf *.o *.gch *.avi $(TARGET)
But when i excute a make in the command line i get:
g++ -o main main.o DJI_utility.o -Wl,-rpath,./ -lpthread -lrt -L./ -L/usr/local/lib/ -lDJI_guidance -lusb-1.0 `pkg-config --cflags --libs opencv`
/usr/bin/ld: skipping incompatible /usr/local/lib//libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: skipping incompatible /usr/local/lib/libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: skipping incompatible //usr/local/lib/libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: cannot find -lDJI_guidance
/usr/bin/ld: cannot find -lusb-1.0
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'main' failed
make: *** [main] Error 1
The project is located in:
~/Documents/studies_SRT/SRT5/TX_drone/Guidance-SDK/demo/guidance_track
The output of an ls is:
DJI_guidance.h DJI_utility.cpp DJI_utility.h DJI_utility.h.gch DJI_utility.o main.cpp main.o Makefile
Thank you.
You seem to have downloaded library files for the wrong architecture. If you're on a 64-bit system, download the 64-bit version of the libraries.

Adding a library

I'm doing some modifications to an existing project which is pretty big, so it's built with the autotools. The modifications involve the Ibex library, so I've added an #include "ibex.h" in one of the source files. The library is properly installed on my system, I have the following files:
/usr/local/lib/libibex.a
/usr/local/include/ibex/ibex.h
/usr/local/share/pkgconfig/ibex.pc
Results of the pkg-config commands:
$ pkg-config --libs ibex
-L/usr/local/lib -libex -lprim -lClp -lCoinUtils -lm
$ pkg-config --cflags ibex
-frounding-math -ffloat-store -I/usr/local/include -I/usr/local/include/ibex
The original Makefile.am corresponding to the compil unit I want to get to use ibex, is as follows:
noinst_LTLIBRARIES = liblrasolver.la
AM_CPPFLAGS=$(config_includedirs)
liblrasolver_la_SOURCES = LAVar.h LAVar.C Delta.h Delta.C LRASolver.h LRASolver.C LAArray.h LAArray.C LARow.h LARow.C LAColumn.h LAColumn.C
if WANT_LIBRARY
include_HEADERS = Delta.h LAArray.h LAColumn.h LARow.h LAVar.h LRASolver.h
endif
I modified it this way:
noinst_LTLIBRARIES = liblrasolver.la
AM_CPPFLAGS=$(config_includedirs) `pkg-config --cflags ibex`
AM_LDFLAGS=`pkg-config --libs ibex` -lblas -llapack
liblrasolver_la_SOURCES = LAVar.h LAVar.C Delta.h Delta.C LRASolver.h LRASolver.C LAArray.h LAArray.C LARow.h LARow.C LAColumn.h LAColumn.C
if WANT_LIBRARY
include_HEADERS = Delta.h LAArray.h LAColumn.h LARow.h LAVar.h LRASolver.h
endif
Came to this modification by looking into the generic Makefile provided along with ibex sources to compile ibex projects:
SRCS=$(wildcard *.cpp)
BINS=$(SRCS:.cpp=)
CXXFLAGS := $(shell pkg-config --cflags ibex)
LIBS := $(shell pkg-config --libs ibex) -lblas -llapack
ifeq ($(DEBUG), yes)
CXXFLAGS := $(CXXFLAGS) -O0 -g -pg -Wall -frounding-math
else
CXXFLAGS := $(CXXFLAGS) -O3 -DNDEBUG -Wno-deprecated -frounding-math
endif
all: $(BINS)
% : %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $# $< $(LIBS)
clean:
rm -f $(BINS)
Ok, autoreconf works, as well as ./configure (though its output never talks about "ibex" which I find suspicious already). But make fails. Doesn't find the header:
../../../src/tsolvers/lrasolver/LRASolver.h:38:18: fatal error: ibex.h: No such file or directory
#include "ibex.h"
You're not using pkg-config correctly. You should use the PKG_CHECK_MODULES macro, see for reference https://autotools.io/pkgconfig/pkg_check_modules.html (full disclosure: I wrote that.)
You're also using AM_LDFLAGS incorrectly since libraries are not ldflags. You should use _LIBADD.

How to include hidapi in makefile?

how should properly include HIDAPI library?. After installing from the Fedora 20 repositories. It is installed in /usr/lib
This is my makefile:
CC=g++
CCFLAGS= -Wall -O0
SOURCE= Main.cpp ModuloUsb.cpp ModuloUsb.h
EXEC=modulo
test:$(SOURCE)
$(CC) $(CCFLAGS) $(SOURCE) -I../hidapi `pkg-config --libs --cflags libusb-1.0` -o $(EXEC)
After make command:
Any suggestions??

Problem using yaml-cpp on OS X

So I'm having trouble compiling my application which is using yaml-cpp
I'm including "yaml.h" in my source files (just like the examples in the yaml-cpp wiki) but when I try compiling the application I get the following error:
g++ -c -o entityresourcemanager.o entityresourcemanager.cpp
entityresourcemanager.cpp:2:18: error: yaml.h: No such file or directory
make: *** [entityresourcemanager.o] Error 1
my makefile looks like this:
CC = g++
CFLAGS = -Wall
APPNAME = game
UNAME = uname
OBJECTS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
mac: $(OBJECTS)
$(CC) `pkg-config --cflags --libs sdl` `pkg-config --cflags --libs yaml-cpp` $(CFLAGS) -o $(APPNAME) $(OBJECTS)
pkg-config --cflags --libs yaml-cpp returns:
-I/usr/local/include/yaml-cpp -L/usr/local/lib -lyaml-cpp
and yaml.h is indeed located in /usr/local/include/yaml-cpp
Any idea what I could do?
Thanks
Your default target is "mac" and you have rule how to build it. It depends on object files and you do not have any rules how to build those, so make is using its implicit rules. Those rules do just that:
g++ -c -o entityresourcemanager.o entityresourcemanager.cpp
As you can see there is no -I/usr/local/... part here.
The easiest way to fix that is to change CPPFLAGS and LDFLAGS value globally:
YAML_CFLAGS := $(shell pkg-config --cflags yaml-cpp)
YAML_LDFLAGS := $(shell pkg-config --libs yaml-cpp)
SDL_CFLAGS := $(shell pkg-config --cflags sdl)
SDL_LDFLAGS := $(shell pkg-config --libs sdl)
CPPFLAGS += $(YAML_CFLAGS) $(SDL_CFLAGS)
LDFLAGS += $(YAML_LDFLAGS) $(SDL_LDFLAGS)
mac: $(OBJECTS)
$(CXX) -o $(APPNAME) $(OBJECTS) $(LDFLAGS)
CPPFLAGS value is used by implicit rules that build object files from cpp files, so now compiler should find yaml headers.
Edit:
LDFLAGS probably should go after OBJECTS
Don't you mismatch your include directory?
-I/usr/local/include
instead of
-I/usr/local/include/yaml-cpp