Importing Libraries into QTCreator - qt

I am creating an OpenGL project in QT Creator, and would like to import GLFW and potentially other libraries later on.
I compiled and imported GLFW and was able to import it via
#include <GLFW/glfw3.h>
Qt Creator was even able to autofill the glfw methods. However, when I attempted to call one of them (e.g. glfwInit();, the compiler threw me the following error:
Undefined symbols for architecture x86_64:
"_CFArrayAppendValue", referenced from:
_addJoystickElement in libglfw3.a(iokit_joystick.m.o)
"_CFArrayApplyFunction", referenced from:
__glfwInitJoysticks in libglfw3.a(iokit_joystick.m.o)
_addJoystickElement in libglfw3.a(iokit_joystick.m.o)
"_CFArrayCreateMutable", referenced from:
... etc
I'm assuming it was unable to find the implementation of these methods.
I imported GLFW through the following procedure:
1. I used CMake to build a Unix Script. 2. I ran "make" in terminal on the generated makefile. This created a file called "libglfw3.a" 3. I imported the library into the project through QT, setting the Library as libglfw3.a and include path as "mypath/include/" (this folder contains another folder called GLFW, which contains glfw3.h).
QT Creator then entered the following into the QT project file.
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/glfw-3.1.1/src/release/ -lglfw3
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/glfw-3.1.1/src/debug/ -lglfw3
else:unix: LIBS += -L$$PWD/glfw-3.1.1/src/ -lglfw3
INCLUDEPATH += $$PWD/glfw-3.1.1/include
DEPENDPATH += $$PWD/glfw-3.1.1/include
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/release/libglfw3.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/debug/libglfw3.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/release/glfw3.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/debug/glfw3.lib
else:unix: PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/libglfw3.a
Help would be greatly appreciated. I've spent at least 10 hours trying to figure out how to import a GL library into QT Creator, but all I can find are CMake tutorials for XCode, Visual Studio, etc.
I would ideally like to this by just modifying the QT .pro file, because I don't have experience with CMake, but if there are no alternatives, that's fine.
I'm developing with QT creator on Mac 10.10.15.

If anyone's curious, I figured it out.
I followed the procedure here: Compiling with GLFW3, linker errors 'undefined reference'
I didn't at first think this was relevant because it was specifically referring to building on XCode, but it turns out you have to do the exact same thing with QT Creator.
right click the project, Import Library -> System/Library/Frameworks -> and import Cocoa, CoreVideo, and IOKit.
My .pro file had the following lines added to them after I did all that
mac: LIBS += -framework Cocoa
else:unix|win32: LIBS += -lCocoa
mac: LIBS += -framework IOKit
else:unix|win32: LIBS += -lIOKit
mac: LIBS += -framework CoreVideo
else:unix|win32: LIBS += -lCoreVideo

Related

Including library files with qmake without hardcoding the compiler version

When I include an external library using Qt Creator, it adds something like the following to my .pro file:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../build-mylibrary-Desktop_Qt_5_15_1_MSVC2019_64bit-Release/mylibrary/release/ -lmylibrary
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../build-mylibrary-Desktop_Qt_5_15_1_MSVC2019_64bit-Debug/mylibrary/debug/ -lmylibrary
else:unix: LIBS += -L$$PWD/../../../build-mylibrary-Desktop_Qt_5_15_1_MSVC2019_64bit-Release/mylibrary/ -lmylibrary
INCLUDEPATH += $$PWD/../../../mylibrary
DEPENDPATH += $$PWD/../../../mylibrary
The path to the library is specific to the compiler and Qt version. I have several libraries that I would like to distribute. There are various dependencies between them. The libraries can be build by different compilers and different Qt versions. So I don't want to distribute the code with a .pro file that assumes MSVC2019 and Qt 5.15.1 (as in the example above). Is there a standard solution to this problem? Or do I just need to expect people who download the code to change the folder names?
For internal usage, you can use the following qmake variables to dynamically build the libs path (based on the current target):
QT_VERSION
QMAKE_MSC_VER
For external people, such an approach may not work out of the box, as the build path is configured by the external user, not by the .pro-file.
If your goals is to resolve the linking/include dependencies between your libs, SUBDIRS may be a solution.
Note that qmake is being replaced in favour of cmake. Maybe cmake has a better solution, but I'm not very familiar with it.
A possible solution is to use DESTDIR in your .pro files.
For example:
DESTDIR = ../bin
LIBS *= -L$$OUT_PWD/$$DESTDIR -lmylib

How to add opencv DLL on Windows to QTCreator project?

I've built opencv and added these lines to .pro file:
INCLUDEPATH += C:/opencv-3.4.1/build/install/include
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_core341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_highgui341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_imgcodecs341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_imgproc341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_features2d341d.lib
now if I start the project, it links successfully but fails to start:
Starting C:\Users\steve\Documents\build-qttest4-Desktop_Qt_5_11_0_MSVC2015_64bit-Debug\debug\qttest4.exe...
The program has unexpectedly finished.
The process was ended forcefully.
Can I somehow tell QTCreator to add DLL files to put opencv DLL files to build-qttest4-Desktop_Qt_5_11_0_MSVC2015_64bit-Debug folder?
Here is a solution (which doesn't match top google search tutorial):
INCLUDEPATH += C:/opencv-3.4.1/build/install/include
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_core341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_highgui341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_imgcodecs341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_imgproc341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_features2d341d.lib
LIBS += -L"C:/opencv-3.4.1/build/bin/Debug"
and don't forget to run Build->Run qmake.
There are 2 ways to use DLL in some Windows Application
Use WinAPI LoadLibrary function and GetProcAddress function.
Set inside a Qt PRO file a location of LIB file that will tell to application that binary code is in YourLibraryName.DLL. But when you run debugger, application doesn't know where is DLL. One of possible solutions is to put full path to opencv DLL folder to PATH environment variable in windows Control Panel

QtOpengl conflict with Opengl framwork on mac

I am trying to merge cocos2d-x 3.12 and Qt 5.7 together and was keep getting the conflict error message when linking:
In file included from Qt5.7.0/5.7/clang_64/lib/QtOpenGL.framework/Headers/QGLWidget:1:
In file included from Qt5.7.0/5.7/clang_64/lib/QtOpenGL.framework/Headers/qgl.h:45:
In file included from Qt5.7.0/5.7/clang_64/lib/QtGui.framework/Headers/qopengl.h:124:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl3.h:2704:21: error: conflicting types for 'glDepthRange'
GLAPI void APIENTRY glDepthRangef (GLclampf n, GLclampf f);
^
demo/cocos2d-x/cocos/platform/mac/CCGL-mac.h:43:41: note: expanded from macro 'glDepthRangef'
#define glDepthRangef glDepthRange
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl3.h:1662:21: note: previous declaration is here
GLAPI void APIENTRY glDepthRange (GLclampd near, GLclampd far);
I tried to remove the Opengl Framework in .pro file like this :
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtApplication
TEMPLATE = app
#INCLUDEPATH += /usr/local/include
#LIBS += -L/usr/local/lib/ -lGLEW
LIBS += -L/usr/local/lib/ -lz
LIBS += -L/usr/local/lib/ -lcurl
LIBS += -framework Cocoa
LIBS += -framework Foundation
LIBS += -framework OpenAL
LIBS += -framework IOKit
LIBS += -framework CoreVideo
LIBS += -framework OpenGL
But it was not working, still the same error.
Any advice will be appreciated, thanks :)
I assume this error arises due to Qt's policy of overwriting OpenGL-related macro definitions as provided by glew.h or GL.h. Others have tried to deal with this and encountered similar problems (e.g. GLEW and Qt5 redefinition of headers). The workaround that got it going for me was to avoid including GL.h/glew.h and any Qt header related to OpenGL in the same file.

Qt creator: include path for external lib not found

I'm trying to add net-snmp lib to my project using win7 + cygwin:
Project file seems to contain valid lib and path entries:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/C:/usr/lib/
-lnetsnmp else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/C:/usr/lib/ -lnetsnmpd
INCLUDEPATH += $$PWD/C:/usr/include DEPENDPATH += $$PWD/C:/usr/include
But I can't build the project, because include file is not found, also it exists under given directory physically (c:/usr/include/net-snmp/net-snmp-config.h)
../snmptest1221313123/main.cpp:1:38: fatal error: net-snmp/net-snmp-config.h: No such file or directory
#include
I've read all "add external lib" topics, but it doesn't help to solve this.
Re-running qmake, re-opening Qt creator, or running qmake -r from terminal doesn't help neither.
Remove $$PWD/ (both from include and libs) and try again: run qmake and rebuild.

Qt: Static linking of libraries

I'm developing a Qt application in windows. I have created a lib file in visual studio. I have included the header file containing function prototypes in my Qt App and also added lib file in .pro file as follows
win32: LIBS += -L$$PWD/../Lib/ -lCpLib
INCLUDEPATH += $$PWD/../Lib
DEPENDPATH += $$PWD/../Lib
win32: PRE_TARGETDEPS += $$PWD/../Lib/CpLib.lib
I called the functions from my Qt app. It shows compile error as "undefined reference"
Please help me.
Regards....
The specification of your LIBS is passing linker arguments in gcc format (i.e. using -L to specify the directory and -l to specify the library name).
As you have created your lib file in Visual Studio, you'll need to specify the full library name
e.g.
win32: LIBS += yourlibrary.lib
alternatively, recompile the libraries using MinGW

Resources