Refer to included libraries in Qt C++ Project - c++

There is a high chance that this might get downvoted, but I am still posting this since I did not find the answer anywhere. I have added a Static External Library to my Project and this is how the MyApp.pro looks like at the moment:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release/ -lCommonLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release/ -lCommonLibd
else:unix: LIBS += -L$$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release/ -lCommonLib
INCLUDEPATH += $$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release
DEPENDPATH += $$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release/libCommonLib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release/libCommonLibd.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release/CommonLib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release/CommonLibd.lib
else:unix: PRE_TARGETDEPS += $$PWD/../common/build-CommonLib-Desktop_Qt_5_3_MSVC2012_OpenGL_32bit-Release/release/libCommonLib.a
However, I am still unsure as how to refer to the Library (and a specific header file in there) from a header file in my project. I tried doing #include<CommonLib/Test.h> but it does not recognize either CommonLib or CommonLib/Test.h.

Your INCLUDEPATH points to the build directory, while library headers are most likely in the directory with CommonLib sources.

Related

Linking a lib file with a non-standard name in Qt

On Windows, I'm attempting to add an external DLL to my Qt project (via Qt Creator). I have the following generated artifacts I'm trying to reference:
target/debug/mylib.d
target/debug/mylib.dll
target/debug/mylib.dll.d
target/debug/mylib.dll.lib
Adding the library/dll generates the following entry in my .pro file:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/target/release/ -lmylib.dll
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/target/debug/ -lmylib.dll
INCLUDEPATH += $$PWD/target/debug
DEPENDPATH += $$PWD/target/debug
Qt is expecting "mylib.dll.lib" to be named "mylib.lib", so with the above configuration the build fails with the error:
error: LNK1104: cannot open file 'mylib.lib'
The build works correctly if I rename "mylib.dll.lib" to "mylib.lib", but I'd rather not introduce this extra step, if possible. The dll.lib suffix is generated by Rust/Cargo, and there aren't any plans to allow this to be configured.
After doing some research, I've tried a couple of different options, including referencing it in PRE_TARGETDEPS, but I can't make the LNK1104 error disappear. What am I missing?
I figured it out. The trick is to refer to the files directly (i.e. not using the -L / -l parameters), like this:
win32:CONFIG(release, debug|release): LIBS += $$PWD/target/release/mylib.dll.lib
else:win32:CONFIG(debug, debug|release): LIBS += $$PWD/target/debug/mylib.dll.lib
win32:CONFIG(release, debug|release): LIBS += $$PWD/target/release/mylib.dll
else:win32:CONFIG(debug, debug|release): LIBS += $$PWD/target/debug/mylib.dll

How to get the target directory in PRO file

I am building an application in Debug mode, how to get the target directory in pro file.
I didnt explicitly mentioned the target directory using "DESTDIR"
CORE_API_PATH = $$PWD/../Bin
SEPARATOR = "/"
QT += core gui xml widgets printsupport svg
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Cinemas
TEMPLATE = app
RC_ICONS += cinemas-icon.ico
qtHaveModule(opengl) {
DEFINES += QT_OPENGL_SUPPORT
QT += opengl
}
LIBS += -lQt5Concurrent -lglu32 -lopengl32 -lglut32 -LC:\Qt\Qt5.3.2\Tools\mingw482_32\i686-w64-mingw32\lib\glut -LC:\Qt\Qt5.3.2\Tools\mingw482_32\i686-w64-mingw32\lib\glu32
win32:CONFIG(release, debug|release): LIBS += "$$quote($${CORE_API_PATH}/Release/CoreApi.dll)"
else:win32:CONFIG(debug, debug|release): LIBS += "$$quote($${CORE_API_PATH}/Debug/CoreApiD.dll)"
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/SSH/lib/ -lssh2
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/SSH/lib/ -lssh2
else:unix: LIBS += -L$$PWD/SSH/lib/ -lssh2
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/3rdparty/qwt/lib/ -lqwt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/3rdparty/qwt/lib/ -lqwtd
else:unix: LIBS += -L$$PWD/qwt/lib/ -libqwt
QT += concurrent network
CONFIG += c++11
RESOURCES += \
cinemasresource.qrc
FORMS += \
I want to copy the app.exe from the current folder to some other folder
On Windows you can use DLLDESTDIR variable which specifies where to copy the target dll or exe. Just add this to your .pro :
CONFIG(release, debug|release): DLLDESTDIR += $$PWD/../exec
On Linux you can use QMAKE_POST_LINK variable which contains the command to execute after linking the TARGET together. So it is like:
CONFIG(release, debug|release): QMAKE_POST_LINK += $$quote(cp project $$PWD/../exec)
Here project is the name of the target file which you provide by TARGET = project
These will copy the executable binary to a directory named exec one level upper than the program working directory. You can have your arbitrary path.

How to correctly link your own library to your project in Qt on Windows?

In my QtCreator C++ project I'm using subdirs template and have several subprojects including lib-project. In app-project I'm trying this:
win32:CONFIG(release, debug|release): LIBS += -L../Library/release -lLibrary
else:win32:CONFIG(debug, debug|release): LIBS += -L../Library/debug -lLibrary
else:unix: LIBS += -L../Library -lLibrary
On Linux it works well, but on Windows library compiles (there're .dll and .a files), linker links it (if I change the path to wrong, like -L../Library/debugg I have an error - can't find -lLibrary), but then I have "undefined reference to [my_classes]". INCLUDEPATH and DEPENDPATH are also correct (because it works on Linux):
INCLUDEPATH += $$PWD/.. $$PWD/../Library
DEPENDPATH += $$PWD/.. $$PWD/../Library
Also tried this:
LIBS += -L$$OUT_PWD/../Library/release -lLibrary
Same result, so what should I do?

Why i am in getting undefined refrence error after linking liblas to Qt?

I am trying to link liblas library to Qt.I am using following code in .pro file to link it.
//////////////code to link liblas to Qt/////////////////////////////////
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../OSGeo4W/lib/ -lliblas
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../OSGeo4W/lib/ -lliblas
else:unix: LIBS += -L$$PWD/../../../../OSGeo4W/lib/ -lliblas
INCLUDEPATH += $$PWD/../../../../OSGeo4W/include
DEPENDPATH += $$PWD/../../../../OSGeo4W/include
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../OSGeo4W/lib/ -llaszip
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../OSGeo4W/lib/ -llaszipd
else:unix: LIBS += -L$$PWD/../../../../OSGeo4W/lib/ -llaszip
INCLUDEPATH += $$PWD/../../../../OSGeo4W/include
DEPENDPATH += $$PWD/../../../../OSGeo4W/include
///////////////////////////////////////////////////////////////
and when i am running the code given below, I am getting undefined reference error
ifstream stream;
stream.open("lidar_sample.las", std::ios::in | std::ios::binary);
ReaderFactory f;
Reader reader = f.CreateWithStream(stream);
error: undefined reference to `liblas::ReaderFactory::CreateWithStream(std::istream&)'
only two possible answers here.
your liblas is not found by the linker
your liblas is not
the same version as your headers.

"No rule to make target" error when adding an external library in Qt Creator

I am new in the world of Qt. I would like to add an external library of wxMSW-2.8.12 in Qt Creator. In order to do that, I right-clicked on my .pro file and clicked on "Add Library", then I select "External Library". I finally choose the Library file, click on "Static" for "Linkage".
This is what I got automatically from Qt Creator:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/ -lwxbase28ud_xml
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/ -lwxbase28ud_xmld
else:unix: LIBS += -L$$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/ -lwxbase28ud_xml
INCLUDEPATH += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib DEPENDPATH += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/libwxbase28ud_xml.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/libwxbase28ud_xmld.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/wxbase28ud_xml.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/wxbase28ud_xmld.lib
else:unix: PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/libwxbase28ud_xml.a
But, when running, I get this error:
:-1: erreur : No rule to make target 'C:/Qt/Qt5.2.0/Tools/QtCreator/bin/testConverter/../../../../../../wxMSW-2.8.12/lib/vc_lib/libwxbase28ud_xmld.a', needed by 'debug\testConverter.exe'. Stop.
I tried to clean and rerun qmake but with any success.
Any suggestions?