So I am learning to program in OpenGL 3.3 and I am using qtcreator as my IDE with qmake as the compiler. Everything is fine except I have to read 2 files (fragmentshader.frag and vertexshader.vert) using ifstream.
I have included those 2 files in the ".pro" like this:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp \
shaders.cpp
LIBS += -lGLEW -lglfw -lGL -lX11 -lpthread -lXrandr -lXi
DISTFILES += \
vertexshader.vert \
fragmentshader.frag
HEADERS += \
shaders.h
and in the code I try to directly read "vertexshader.vert" and "fragmentshader.frag".
My question is: How do I include these files in my application without having to specify an absolute path?
I had the same problem, it comes from the fact Qt is running files from another directory (by default from the build one).
The Fix
So you have two options:
change the Working directory in Projects->Build & Run->Run to %{sourceDir}.
change the Build directory in Projects->Build & Run->Build to . or leave it blank.
Explanations
The first option run your program (.exe on Windows) from the sourceDir, where your project sits, so path will remain the same relatively.
The second option place all your build files (ie: Makefile, *.o and ".exe") in your project directory (they won't appear in the project view unless your Add them), I prefer this way because now your project directory contains everything related to it.
Choose the one you prefer the most, hope it helps.
Related
I am trying to run a QT project on Windows that I have developed on Linux Ubuntu. Unfortunately, I cannot manage it to properly link the boost library. Here is a detailed description of the problem.
I downloaded MinGW from https://nuwen.net/mingw.html, version 13.5, such that the gcc version 5.3.0 matches the MinGW version of QT. The reason to choose this version of MinGW is that it contains the boost libraries for compression such as zlib. From the command line I compile and run my program without any problems:
g++ -std=c++11 -w -IC:/MinGW/include -LC:/MinGW/lib bAya.cpp A.o B.o C.o -o baya -lboost_iostreams -lz -lbz2
where A,B, C are my pre-compiled object files.
In QT creator I selected the MinGW compiler (C:\MinGW_53\MinGW\bin\g++.exe) and created a new default kit with the compiler.
My .pro file looks as follows:
MAKE_CXXFLAGS += -std=c++11
INCLUDEPATH += C:/MinGW_53/MinGW/include \
C:/Users/kuzk/Documents/src
LIBS += -LC:/MinGW_53/MinGW/lib/mylib \
-lboost_system \
-lboost_iostreams \
-lz \
-lbz2
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp\
mainwindow.cpp \
../src/A.cpp \
../src/B.cpp \
../src/C.cpp
HEADERS += mainwindow.h \
../src/A.h \
../src/B.h \
../src/C.h
The folder C:/MinGW_53/MinGW/lib/mylib contains my .a files such as libboost_iostreams.a
When I build it, I get many undefined reference errors such as
error: undefined reference to `boost::iostreams::zlib::default_strategy'
Interestingly, if C:/MinGW_53/MinGW/lib/mylib contains also libstdc++.a I also get linking errors to std::cout, std::ifstream etc.
I am currently stuck and I will be very helpful for any feedback.
Best,
Konstantin
Adding the library path for boost_iostreams:
-LC:/MinGW/lib \
after the line:
LIBS += -LC:/MinGW_53/MinGW/lib/mylib \
should fix it.
At the end I was able to resolve the problem. Here is what I did. I am using QT 5.7 on Windows 10.
I compiled boost using QT's console as described here: http://cpp-qt-mac-win.blogspot.co.uk/2011/10/qt-boost-for-beginners-step-by-step.html
Then I created a new project and added the paths to boost and the necessary libraries, as described in my question.
An important note. I tried to update the paths in the existing project, by running qmake and then rebuilding the project. I was getting the very same errors as I was getting with the old paths to "standard" boost. Apparently, something was cached and the changes didn't do anything. However, if I give a wrong path to a library it complained. So, QT has very confusing behavior. If someone can explain it, it will be helpful.
I start a programm which includes a library (IDA) in
/home/MYUSERNAME/EB/IDA/Earlybite/
The library IDA has two folders:
/home/MYUSERNAME/EB/IDA/IDA/Includes/ (for h-file)
/home/MYUSERNAME/EB/IDA/IDA/Libs/ (for so-files)
This is the linking which works:
LIBS += -L$$PWD/../IDA/Libs/ -Wl,-rpath=$$PWD/../IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += $$PWD/../IDA/Includes/
The problem is that PWD only shows the path in which Earlybite starts. In this case
/home/MYUSERNAME/EB/IDA/Earlybite/, but if the programm starts e.g. in /home/MYUSERNAME/EB/IDA/ ...the linking will not work.
So I tried to link with the HOME environment variable. E.g.
LIBS += -L$$HOME/EB/IDA/IDA/Libs/ -Wl,-rpath=$$HOME/EB/IDA/IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += $$HOME/EB/IDA/IDA/Includes/
But this do not work.
I also tried
LIBS += -L/home/$$USER/EB/IDA/IDA/Libs/ -Wl,-rpath=/home/$$USER/EB/IDA/IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += /home/$$USER/EB/IDA/IDA/Includes/
But this do not work, too.
(I've also tried every try with a single $ and with two $ symbols...)
Edit: I just remembered that you can use $$(HOME) which will read environment variable during qmake execution, so you just need to add () around HOME. Using $_PRO_FILE_PWD_ is still a good practice, but the last options is a workaround rather than straightforward solution I think.
You can try and use $$_PRO_FILE_PWD_, this variable points to .pro file location, and create path relative to project file. Also check qmake Variables for additional references.
Or you can do:
HOME = $$system(echo $HOME)
message($$HOME)
LIBS += -L$$HOME ...
About $$system link
I'm trying to use the Sha512 function in openSSL but can't seem to get it to work as I get compiler errors just starting into the code. I include #include <openssl/sha.h> at the top of the .cpp file, then in the action of a button event I put just the following code below.
SHA512_CTX ctx;
SHA512_Init(&ctx);
//Will uncomment below in later if I get SHA512_Init to work
//SHA512_Update(&ctx, string, strlen(string));
//SHA512_Final(digest, &ctx);
I get a linker error telling my undefined symbols for architecture x86_64, implying the function does not exist?
I'm aware QT 5 has a hash function, but I'm limited to QT 4.8 so I can not use the cryptographic sha512 hash function available in the QT 5+ framework.
Any help is appreciated!
Used macports to install openssl
I'm using Mac OS 10.9.2
MAKE FILE
#-------------------------------------------------
#
# Project created by QtCreator 2014-06-11T20:27:49
#
#-------------------------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ExchangeTab
TEMPLATE = app
LIBS += -L/usr/include/openssl -openssl
INCLUDEPATH += /usr/include/openssl
SOURCES += main.cpp\
mainwindow.cpp \
httpsocket.cpp \
cloaksend.cpp \
exchange.cpp
HEADERS += mainwindow.h \
httpsocket.h \
cloaksend.h \
exchange.h
FORMS += mainwindow.ui
RESOURCES += \
AppResources.qrc
Looking for cross platform solution please.
You need to link to the right library (openSSL)
Have a look here: How to Include OpenSSL in a Qt project
Specifically, add this to your .pro file.:
LIBS += -L/opt/local/lib/ -lcrypto
For including .h files add this line to your .pro file :
INCLUDEPATH += /opt/local/include
[1] says that the default include path will be /opt/local/include/.
LIBS += -L/usr/include/openssl -openssl
INCLUDEPATH += /usr/include/openssl
This looks incorrect. The OpenSSL libraries are libcrypto (-lcrypto) and libssl (-lssl). There is nolibopenssl(-lopenssl). Try:
LIBS += -L/usr/lib -lcrypto
INCLUDEPATH += /usr/include/openssl
But the libraries are version 0.9.8. You might consider upgrading to 1.0.1h.
$ ls /usr/lib | grep crypto
libcrypto.0.9.7.dylib
libcrypto.0.9.8.dylib
libcrypto.dylib
libk5crypto.dylib
And
$ /usr/bin/openssl version
OpenSSL 0.9.8y 5 Feb 2013
If you choose to upgrade, OpenSSL will install into /usr/local/ssl. Avoid mixing/matching version of OpenSSL with the following.
INCLUDEPATH += /usr/local/ssl/include/openssl
LIBS += /usr/local/ssl/lib/libcrypto.a
Its OK to specify objects and archives in LIBS. See How to add object files to a project in Qt.
Okay so I may have answered my own question but I will need some help understanding why it worked.
My make file was indeed the problem. I added the following 3 lines.
INCLUDEPATH += $$OPENSSL_INCLUDE_PATH
LIBS += $$join(OPENSSL_LIB_PATH,,-L,)
LIBS += -lcrypto
Then it magically compiled just fine. I found these in another project made with QT that compiled for OpenSSL.
Interestingly enough. I removed the top two lines so only the following remained. Then ran the clean on my project to be sure the code was being recompiled.
LIBS += -lcrypto
This also just 'worked' without linker errors. It looks like that is the only command I need. Question is... will that be cross platform friendly if I take this code and compile on linux or windows? That I am not sure, but this worked.
I've been attempting for the past 16 hours to attach the libusb library to a Qt project without much success. I would appreciate any input on the matter, it's getting frustrating.
The .pro file is this:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH +=/usr/local/include/libusb-1.0
LIBS += -L/usr/local/lib -libusb-1.0.a
LIBS += -L<libusb.h>
Source code:
#include <iostream>
#include <libusb.h>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Compiler output:
13:01:50: Running steps for project lallala...
13:01:50: Configuration unchanged, skipping qmake step.
13:01:50: Starting: "/usr/bin/make" -w
make: Entering directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
/Users/MAXIMUS/Qt5.0.0/5.0.0/clang_64/bin/qmake -spec macx-g++42 CONFIG+=debug CONFIG+=x86_64 CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ../lallala/lallala.pro
make: Leaving directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
make: Entering directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
g++-4.2 -headerpad_max_install_names -mmacosx-version-min=10.6 -o lallala main.o -L/usr/local/lib -libusb-1.0.a -L<libusb.h>
/bin/sh: -c: line 0: syntax error near unexpected token `newline'
/bin/sh: -c: line 0: `g++-4.2 -headerpad_max_install_names -mmacosx-version-min=10.6 -o lallala main.o -L/usr/local/lib -libusb-1.0.a -L<libusb.h> '
make: *** [lallala] Error 2
make: Leaving directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
13:01:50: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project lallala (kit: Desktop Qt 5.0.0 clang 64bit (SDK))
When executing step 'Make'
Forming my comment into a proper answer; this is not the correct syntax to use:
LIBS += -L/usr/local/lib -libusb-1.0.a
LIBS += -L<libusb.h>
The proper one would be this:
LIBS += -L/usr/local/lib -lusb-1.0
or
LIBS += -l/full/path/to/libusb-1.0.a
You can drop the second LIBS line in your initial attempt because you have already specified the path in the former, and putting an "include" statement in there would not be reasonable anyhow. So, this is what you could write for your complete .pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH +=/usr/local/include/libusb-1.0
LIBS += -L/usr/local/lib -lusb-1.0
This is not Qt specific, just generic linkage issue: -lfoo extends to $(prefix)foo$(suffix), where the prefix and suffix are figured out automatically based on the platform. That is, the prefix would be lib in your case, and suffix would be either .a or .so on Unix, probably .dylib on Mac, etc.
You may wish to look into pkg-config support if it is possible to establish. In that case, you would write something like this what we did in QtSerialPort:
CONFIG += link_pkgconfig
PKGCONFIG += libudev
Yet another option is to add the GUI through the QtCreator IDE or similar IDE that you may be using. There is an option usually in the "Linker" section to add a library. Here are two screenshots from my QtCreator:
Click on the project name on the left in the project source tree navigator, and select Add Library. Then the first screenshot will come up, and you can select the external option, and then you can see the second.
It is needless to say that you would need to run qmake after these changes to generate the corresponding Makefile on your desired platform.
Syntax is the following:
-L%LIBRARY_PATH% to make a specific path visible and
-l%LIBRARY_NAME% to link a specific library that is located in a visible path
so I guess this should work (I don't think you need the .a extension):
LIBS += -L/usr/local/lib -llibusb-1.0
and I have no idea what this would do:
LIBS += -L
so I guess I'd remove it.
Once fixed run qmake then build...
Hope it helps...
I currently run Arch Linux on my laptop and was hoping to know why Qt Creator isn't finding my glut library (which exists on my system).
My setup qmake file looks as follows:
...
/*sources and headers above*/
QT += opengl
LIBS += -lfreeglut
INCLUDEPATH += -L/usr/lib/
And when I run a locate glut, I get the following:
/usr/include/kwinglutils.h
/usr/include/kwinglutils_funcs.h
/usr/include/GL/freeglut.h
/usr/include/GL/freeglut_ext.h
/usr/include/GL/freeglut_std.h
/usr/include/GL/glut.h
/usr/lib/libglut.a
/usr/lib/libglut.so
/usr/lib/libglut.so.3
/usr/lib/libglut.so.3.9.0
/usr/share/avogadro/fragments/amino_acids/D-glutamic_acid.cml
/usr/share/avogadro/fragments/amino_acids/D-glutamine.cml
/usr/share/avogadro/fragments/amino_acids/L-glutamic_acid.cml
/usr/share/avogadro/fragments/amino_acids/L-glutamine.cml
/usr/share/licenses/freeglut
/usr/share/licenses/freeglut/LICENSE
/var/lib/pacman/local/freeglut-2.6.0-1
/var/lib/pacman/local/freeglut-2.6.0-1/changelog
/var/lib/pacman/local/freeglut-2.6.0-1/desc
/var/lib/pacman/local/freeglut-2.6.0-1/files
Note that I have tried -lglut32 in my qmake file as well.
What could I be missing here?
When you specify -lfoobar in your .pro file (or with gcc in general), you're directing the compiler to search for the library libfoobar.a. Judging from your locate output it looks like you want:
LIBS += -lglut
Oh, silly me. I just realized that the correct lib to add was just -lglut and not -lglut32. This is because there exists libglut.so, and not libglut32.so.