Linking libraries to a QT project using pkg-config output - c++

This is a bit of a newbie question. I am trying to add the OpenCV libraries to a QT project.
This question says the link flags are given by
pkg-config --libs opencv
If I paste the command line output into the project file like:
LIBS += -L/usr/local/lib -lml -lcvaux -lhighgui -lcv -lcxcore
then everything compiles fine, but now this isn't portable. How can I simply reference the output of the command?
Update: Tried Ken Bloom's suggestion, but it won't compile. The actual generated compiler commands are
# How it should be, at least on my machine
g++ -o QOpenCVTest main.o qopencvtest.o moc_qopencvtest.o -L/usr/lib -L/usr/local/lib -lml -lcvaux -lhighgui -lcv -lcxcore -lQtGui -lQtCore -lpthread
# with CONFIG and PKGCONFIG
g++ -o QOpenCVTest main.o qopencvtest.o moc_qopencvtest.o -L/usr/lib -lQtGui -lQtCore -lpthread

CONFIG += link_pkgconfig
PKGCONFIG += opencv
(I got this answer from http://beaufour.dk/blog/2008/02/using-pkgconfig.html)

Ken's answer worked great. I just had to remove the spaces on either side of the += first.
CONFIG+=link_pkgconfig PKGCONFIG+=opencv

In the newer version of Qt, this needs to be done to avoid a package not found error:
QT_CONFIG -= no-pkg-config
CONFIG += link_pkgconfig
PKGCONFIG += protobuf #or whatever package here
Also had to do this for Mac:
mac {
PKG_CONFIG = /usr/local/bin/pkg-config
}

Something like this in your qmake file should do
LIBS += `pkg-config --libs opencv`
Edit: Hmm, Ken Bloom's answer might be more portable, but erhm not documented?

Add the following lines to your .pro file:
INCLUDEPATH += `pkg-config --cflags opencv`
LIBS += `pkg-config --libs opencv`

Related

How to solve compile error in gRPC generated files?

I am creating client-server application, using gRPC. So far I was not using TLS encryption. Now I want to enable it, and strangely I get this error. It seems, at least to me, that this is a linker error. What would be best ways to solve it?
CLion doesn't highlight anything, so I assumed everything was syntactically OK, but when compiling I got that error.
/usr/bin/ld: CMakeFiles/projectname.dir/main.cpp.o: in function `grpc::SslServerCredentials(grpc::SslServerCredentialsOptions const&)':
/home/username/projectname/third_party/grpc/include/grpcpp/security/server_credentials.h:60: undefined reference to `grpc_impl::SslServerCredentials(grpc::SslServerCredentialsOptions const&)'
collect2: error: ld returned 1 exit status
This is a makefile that I use to generate C++ code from gRPC specification:
HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
CXX = g++
CPPFLAGS += `pkg-config --cflags protobuf grpc`
CXXFLAGS += -std=c++11
ifeq ($(SYSTEM),Darwin)
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\
-lgrpc++_reflection\
-ldl
else
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
-ldl
endif
PROTOC = protoc
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
PROTOS_PATH = ./
vpath %.proto $(PROTOS_PATH)
%.grpc.pb.cc: %.proto
$(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
%.pb.cc: %.proto
$(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
clean:
rm -f *.o *.pb.cc *.pb.h
Then problem was solved like this:
I had to chage grpc++_unsecure to grpc++ under target_link_libraries in CMakeLists.txt cmake build configuration file.
I had forgotten/not thought about this at first.
target_link_libraries(bita_server
pqxx
sodium
protobuf::libprotobuf
# grpc++_unsecure
grpc++
SQLiteCpp
sqlite3
pthread
dl
${_PROTOBUF_LIBPROTOBUF}
)
In CMakeLists.txt under grpc folder, there are many target_link_libraries. which one to edit in order to include the change.

compiling c++ projects with dlib library on linux

I want to use dlib library for my c++ projects in linux. I have installed it successfully and able to compile and run the .cpp samples files given under the dlib. I have compiled the sample files through the
"g++ -std=c++11 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp" given in [http://dlib.net/compile.html][1]
but I am unable to use the same command to run .cpp files which are the outside dlib-18.18/examples directory
Can someone help me out with compiling c++ file (with dlib library usage) from any user directory in linux?
I came to know that we have to include the path of dlib installation folder while trying to compile it from any other user directory. So I tried the command
" g++ -O3 -I/home/praneeth/computervision/.. ../dlib/all/source.cpp -lpthread -lX11 project3_face.cpp -o project_face pkg-config --cflags opencv pkg-config --libs opencv"
but it gave me the result:
g++: error: ../dlib/all/source.cpp: No such file or directory
Note: dlib-18.18 folder is present in my /home/praneeth/computervision/
I don't know how correct is this but it got compiled when I ran the command like: g++ -O3 -I/home/praneeth/computervision/dlib-18.18 /home/praneeth/computervision/dlib-18.18/dlib/all/source.cpp -lpthread -lX11 project3_face.cpp -o project_face pkg-config --cflags opencv pkg-config --libs opencv any comments on this regarding why this works?
g++ -Wl,-V -std=c++11 -o3 -I/home/user/dlib-19.6 /home/user/dlib-19.6/dlib/all/source.cpp -lpthread -lX11 -o Test Test.cpp pkg-config opencv --cflags --libs

gcc linker can't find library

I get a strange error while building my Qt C++ project on Ubuntu Linux using GCC 5.2.1:
/usr/bin/ld: cannot find -llibmath
I include external dynamic library to maky qmake project using command:
LIBS += -L/home/rem -llibmath
and I have library file at path /home/rem/libmath.so
As I can see from compiler output:
g++ -Wl,-rpath,/home/rem/Qt/5.5/gcc_64 -Wl,-rpath,/home/rem/Qt/5.5/gcc_64/lib -o Bazis main.o builder.o -L/home/rem -llibmath -L/home/rem/Qt/5.5/gcc_64/lib -lQt5OpenGL -L/usr/lib64 -lQt5QuickWidgets -lQt5Widgets -lQt5Quick -lQt5Gui -lQt5Sql -lQt5Test -lQt5Qml -lQt5Network -lQt5Core -lGL -lpthread
all parameters are correctly send by qmake to g++.
What is the source of my problem?
The solution is simple:
I changed my .pro file from:
LIBS += -L/home/rem -llibmath
to:
LIBS += -L/home/rem -lmath

How to supply compiler options in Qt?

I am trying to learn Qt, I have file test.cpp that I run via the terminal using the following command:
g++ `pkg-config --cflags --libs libsbml` test.cpp -L /usr/local/lib -lsbml -lstdc++ -lm
How can I suppl the same options to Qt?
Thank you.
You could write the qmake snippet below. In short, you would need to take a look at the following qmake variables:
LIBS
INCLUDEPATH
TEMPLATE
TARGET
HEADERS
SOURCES
CONFIG
PKGCONFIG
test.pro
TEMPLATE = app
TARGET = test
INCLUDEPATH += .
LIBS += -L /usr/local/lib -lsbml -lstdc++ -lm
unix {
CONFIG += link_pkgconfig
PKGCONFIG += libsbml
}
HEADERS += test.h
SOURCES += test.cpp
In .pro file add:
LIBS += -L /usr/local/lib -lsbml -lstdc++ -lm
look at the Makefile to figure out what variables are used. The makefile is in the build folder made by Qt.

QT SDL Not Recognized (Have Qmake File listed. Am I doing it wrong?)

Here is my qmake file. For whatever reason, when I try to compile the program SDL isn't being recognized. Why is this?
LIBS += -L/usr/include/SDL.h -lSDL
HEADERS += \
render.h \
screenwriter.h
SOURCES += \
screenwriter.cpp \
render.cpp
It seems SDL uses pkgconfig:
$ repoquery -l SDL-devel | fgrep .pc
/usr/lib/pkgconfig/sdl.pc
/usr/lib64/pkgconfig/sdl.pc
So the best way to link with it would be to use link_pkgconfig, instead of adding it manually to LIBS:
CONFIG += link_pkgconfig
PKGCONFIG += sdl
This will automatically modify QMAKE_CXXFLAGS, QMAKE_CFLAGS, and LIBS for you, by calling pkg-config --cflags sdl and pkg-config --libs sdl.
Have you tried
`sdl-config --libs`
instead of -lSDL ?