How to supply compiler options in Qt? - c++

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.

Related

Why do I have an error while compiling my code in C with Qt creator but can easily do this using make file?

I use gstreamer in my project, and if I compile program with make - IT WORKS well.
SOURCES= \
main.cpp
all: main.cpp
g++ $(INC) -std=gnu++11 $(SOURCES) -g -o myexe -lpthread -Wall -lgstapp-1.0 `pkg-config --cflags --libs gstreamer-1.0`
clean:
rm -rf *.o myexe
But I wrote the same in my .pro file in Qt creator and I get an error:
/usr/include/gstreamer-1.0/gst/gstelement.h:55: gst/gstconfig.h: No such file or directory
This is my full .pro file:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
TEMPLATE = app
INCLUDEPATH += /usr/include/gstreamer-1.0/ \
/usr/include/glib-2.0/ \
/usr/lib/x86_64-linux-gnu/glib-2.0/include/ \
/usr/include/libxml2/
LIBS += -std=gnu++11
LIBS += -lgstapp-1.0
LIBS += -lpthread
LIBS += `pkg-config --cflags --libs gstreamer-1.0`
SOURCES += main.cpp
Please tell, how to fix this ? I really want to use Qt creator.
Here is the correct .pro file with all needed paths:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
TEMPLATE = app
INCLUDEPATH += /usr/include/gstreamer-1.0/ \
/usr/lib/x86_64-linux-gnu/gstreamer-1.0/include \
/usr/include/glib-2.0/ \
/usr/lib/x86_64-linux-gnu/glib-2.0/include/ \
/usr/include/libxml2/
LIBS += -std=gnu++11
LIBS += -lgstapp-1.0
LIBS += -lpthread
LIBS += `pkg-config --cflags --libs gstreamer-1.0`
SOURCES += main.cpp

qtmake - shared resources between two projects/targets

I'm trying to set up directory with shared files but I ended up with No rule to make target 'position.cpp', .... What am I doing wrong?
filesystem tree (simplified):
Project.pro client server shared
./client:
client.pro main.cpp main.h
./server:
main.cpp main.h server.pro
./shared:
position.cpp position.h shared.pri
Project.pro:
TEMPLATE = subdirs
SUBDIRS = client server
client.pro (server.pro looks similar):
LIBS += $$system(sdl2-config --libs) -lSDL2_ttf -lSDL2_image -lSDL2_net
QMAKE_CXXFLAGS += $$system(sdl2-config --cflags) -Wall -Wextra -Werror -pedantic -std=c++11
SOURCES += \
main.cpp
HEADERS += \
main.h
include(../shared/shared.pri)
shared.pri:
SOURCES += \
position.cpp
HEADERS += \
position.h
The given error could occur when make fails to find the referenced position.cpp file. The reason is that it tries to find the position.cpp in the directory where the .pro and not the .pri file is. To fix it you might try to use _PRO_FILE_PWD_ variable in your .pri file to properly set path to the position.cpp(h) files.

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 ?

QMake Linking Problem With GCC

I have a problem with qmake and the make file that it generates. My program needs to be linked against two libraries. I add them in main.pro as follows.
LIBS += -L lib/somelib1/bin -lsomelib1 -L lib/somelib2/bin -lsomelib2
How ever I arrange the above line qmake tells gcc this.
g++ -o programname someobject.o -L lib/somelib1/bin lib/somelib2/bin -lsomelib1 -lsomelib2
The problem is that it should look like this.
g++ -o programname someobject.o -L lib/somelib1/bin -L lib/somelib2/bin -lsomelib1
-lsomelib2
GCC gives the following error.
lib/somelib2/bin: file not recognized: Is a directory
Thanks in advance.
You should not put spaces between the flags and the arguments:
LIBS += -Llib/somelib1/bin -lsomelib1 -Llib/somelib2/bin -lsomelib2
Or
LIBS += -L"lib/somelib1/bin" -lsomelib1 -L"lib/somelib2/bin" -lsomelib2
And why are your static/import libraries in the "bin" directory? There should be .a files in the "lib" directory.
You could try putting the library search paths under the QMAKE_LIBDIR tag. So your qmake file would have:
QMAKE_LIBDIR += lib/somelib1/bin lib/somelib2/bin
LIBS += -lsomelib1 -lsomelib2

Linking libraries to a QT project using pkg-config output

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`