Location of OpenGL module when using Qt - qt

I am new to using OpenGL with Qt Creator and I have been playing around with the information I got from various tutorials. However, I never was able to find out where the OpenGL function definitions actually are!
I was thinking that when I linked the opengl module to the qmake file like this:
QT += core gui opengl
the OpenGL stuff was included, but then I thought again when I was reading about the QGLWidget.
So where does the OpenGL API become included into my code?
P.S. My OS is Windows 7 and I my graphics card is AMD Radeon HD 6250 Graphics.

The QT variable is used to include Qt modules into your project. By adding opengl you're adding the Qt OpenGL module which contains Qt code that uses OpenGL (and links to it). If by "include" you mean link, than the OpenGL module of Qt (libQtOpenGL.so) links the various OpenGL libraries. Also, the Qt module includes the headers of the Qt OpenGL module which in turn includes OpenGL headers.
The OpenGL headers and the OpenGL libraries are wherever your OS keeps those. Your project knows where those are because of the Qt mkspecs for your platform, whatever it is. In the mkspecs, include paths and link paths are already included.
For instance, now I'm on a Mac OS X, and Qt mkspecs are installed in /usr/local/Qt4.8/mkspecs. Here I have all the platform descriptions, under common you can find mac.conf, which is a part of the description of my platform. Inside it you can see:
QMAKE_INCDIR_OPENGL = /System/Library/Frameworks/OpenGL.framework/Headers \
/System/Library/Frameworks/AGL.framework/Headers/
This information is used when Qt Creator (qmake) is asked to produce a Makefile for any application. This way your application knows where to find the headers for OpenGL. Your project will automatically add that to the include paths only when you add the opengl module.
As another example, this is part of the definition for the Linux platform:
...
QMAKE_INCDIR =
QMAKE_LIBDIR =
QMAKE_INCDIR_X11 = /usr/X11R6/include
QMAKE_LIBDIR_X11 = /usr/X11R6/lib
QMAKE_INCDIR_QT = $$[QT_INSTALL_HEADERS]
QMAKE_LIBDIR_QT = $$[QT_INSTALL_LIBS]
QMAKE_INCDIR_OPENGL = /usr/X11R6/include
QMAKE_LIBDIR_OPENGL = /usr/X11R6/lib
QMAKE_INCDIR_OPENGL_ES1 = $$QMAKE_INCDIR_OPENGL
QMAKE_LIBDIR_OPENGL_ES1 = $$QMAKE_LIBDIR_OPENGL
QMAKE_INCDIR_OPENGL_ES2 = $$QMAKE_INCDIR_OPENGL
QMAKE_LIBDIR_OPENGL_ES2 = $$QMAKE_LIBDIR_OPENGL
QMAKE_INCDIR_EGL =
QMAKE_LIBDIR_EGL =
QMAKE_INCDIR_OPENVG =
QMAKE_LIBDIR_OPENVG =
...
I suppose you can understand on your own what this means ;-)
So to answer your question: the include paths and the link paths to the OpenGL libraries are included into the Makefile qmake produces for your application taking the information stored in the mkspec file which is located somewhere in your system. The actual OpenGL headers and libraries are located in a default location in your system, and this has nothing to do with Qt itself. You might need to include the headers in your source code if you use OpenGL libraries directly (or you might not need, it depends), but the include paths should already be provided by your mkspec files.
EDIT: Look what happens automatically when adding the opengl module: this is the command line Qt Creator uses to compile a C++ file without the opengl module (on Mac):
g++ -c -pipe -g -gdwarf-2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -Wall -W -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Qt4.8/mkspecs/macx-g++ -I../TextEdit -I/Library/Frameworks/QtCore.framework/Versions/4/Headers -I/usr/include/QtCore -I/Library/Frameworks/QtGui.framework/Versions/4/Headers -I/usr/include/QtGui -I/usr/include -I. -I. -I../TextEdit -I. -F/Library/Frameworks -o main.o ../TextEdit/main.cpp
now this is what happens after adding it:
g++ -c -pipe -g -gdwarf-2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -Wall -W -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Qt4.8/mkspecs/macx-g++ -I../TextEdit -I/Library/Frameworks/QtCore.framework/Versions/4/Headers -I/usr/include/QtCore -I/Library/Frameworks/QtGui.framework/Versions/4/Headers -I/usr/include/QtGui -I/Library/Frameworks/QtOpenGL.framework/Versions/4/Headers -I/usr/include/QtOpenGL -I/usr/include -I/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/System/Library/Frameworks/AGL.framework/Headers -I. -I. -I../TextEdit -I. -F/Library/Frameworks -o main.o ../TextEdit/main.cpp
Your project has been told where to look for the OpenGL headers.

Qt do not contain OpenGL API definition. It must be present in OS already (usually bundled with GPU driver).
Look for documentation of your gpu driver or OS docs.

Try "GLEW", "glut", "GLU", and "GL" instead of "opengl", I'm not sure how it works with Qt Creator, but these are linker flags for opengl.

Related

QT Executable under windows

I am statically linking an QT application, but for some reason when i open the executable i am getting errors for missing dlls.
Basically i am linking the following libs:
### LINKER FLAGS ###
LDFLAGS = -LC:/Qt/5.15.0/mingw81_32/lib
LDLIBS = -lQt5Widgets -lQt5Gui -lQt5Core
Why am i getting errors for missing dlls when i am statically linking everything?
The compilation and linking passes correctly, the executable is generated also..i have no idea why it needs dlls.
EDIT:
Missing dlls:
libgcc_s_dw2-1.dll,
libstdc++-6.dll,
Qt5Code.dll,
Qt5Widgets.dll
To elaborate on CristiFati's reply, you need a line like this in your project file:
QMAKE_LFLAGS += -static-libstdc++ -static-libgcc
though I'd prefer:
QMAKE_LFLAGS_RELEASE += -static-libstdc++ -static-libgcc
Note that if you want a truly standalone, self-contained binary, you'll need to build yourself a static Qt library. That's kind of a big deal, and unfortunately not as well documented as most of Qt-land is.

Qt5 programs won't compile

I'm trying to get Qt5 working on Ubuntu 12.04. I downloaded the 32-bit installer from http://qt-project.org/downloads and basically just let it install everything in my home folder.
I had to add ~/Qt/5.1.0/gcc/bin to my path for qmake to work, but now when I try to make a hello qt example (using qt4 book), when running make, it cannot find the QApplication or QLabel header. When I replace them with QtWidgets/QApplication, it finds the header, but I get all undefined references when linking.
This is the command make executes:
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../../../Qt/5.1.0/gcc/mkspecs/linux-g++ -I. -I. -I../../../../Qt/5.1.0/gcc/include -I../../../../Qt/5.1.0/gcc/include/QtGui -I../../../../Qt/5.1.0/gcc/include/QtCore -I. -o hello.o hello.C
I managed to figure out that it's qmake -project that is doing something wrong. When I make an app in Qt Creator (which actually works), the .pro file has lines like QT += widgets, that are not there when I run it on the command line.
I found the answer using qmake --help. This actually says:
Note: The created .pro file probably will
need to be edited. For example add the QT variable to
specify what modules are required.
Adding these lines to the .pro file solved the problem:
QT += core gui
QT += widgets
As an alternative to manually editing the .pro file after running qmake -project, you can specify assignments on the command line that will be included in the generated files:
qmake -project "QT = core gui widgets"
See http://qt-project.org/doc/qt-5.1/qtdoc/qmake-running.html

Integrating CUDA with Qt on Ubuntu 12.04

I am trying to integrate Qt with CUDA. I am working on Ubuntu 12.04. I already have CUDA and Qt installed.
I followed the steps here -
'linker input file unused because linking not done' when trying to setup QT creator & Cuda
However it still gives me an error.
Here is how I did it.
I created an empty Qt project called 'CUDA2' in my home directory.
I added the following files
cuda_interface.cu
// CUDA-C includes
#include <cuda.h>
extern "C"
void runCudaPart();
// Main cuda function
void runCudaPart() {
// all your cuda code here *smile*
}
main.cpp
#include <QtCore/QCoreApplication>
extern "C"
void runCudaPart();
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
runCudaPart();
return a.exec();
}
I added the following lines to the .pro file
QT += core
QT -= gui
TARGET = cuda2
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
# Source files for C/C++ compiler
SOURCES += main.cpp
# project build directories
DESTDIR = $$system(pwd)
OBJECTS_DIR = $$DESTDIR/Obj
# and C/C++ flags
QMAKE_CFLAGS_RELEASE =-O3
QMAKE_CXXFLAGS_RELEASE =-O3
# cuda source
CUDA_SOURCES += cuda_interface.cu
# Path to cuda toolkit install
CUDA_DIR = /usr/local/cuda
INCLUDEPATH += $$CUDA_DIR/include
QMAKE_LIBDIR += $$CUDA_DIR/lib
# GPU architecture
CUDA_ARCH = sm_20
# NVCC flags
NVCCFLAGS = --compiler-options -fno-strict-aliasing -use_fast_math --ptxas-options=-v
# Path to libraries
LIBS += -lcudart -lcuda
# join the includes in a line
CUDA_INC = $$join(INCLUDEPATH,' -I','-I',' ')
cuda.commands = $$CUDA_DIR/bin/nvcc -m64 -O3 -arch=$$CUDA_ARCH -c $$NVCCFLAGS $$CUDA_INC $$LIBS ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}
cuda.dependcy_type = TYPE_C
cuda.depend_command = $$CUDA_DIR/bin/nvcc -O3 -M $$CUDA_INC $$NVCCFLAGS ${QMAKE_FILE_NAME}
cuda.input = CUDA_SOURCES
cuda.output = ${OBJECTS_DIR}${QMAKE_FILE_BASE}_cuda.o
# Tell Qt that we want add more stuff to the Makefile
QMAKE_EXTRA_COMPILERS += cuda
I ran qmake to generate the makefile. When I click on 'build', I get this error -
13:33:35: Running build steps for project CUDA2...
13:33:35: Configuration unchanged, skipping qmake step.
13:33:35: Starting: "/usr/bin/make" -w
make: Entering directory `/home/alex/CUDA2'
Makefile:541: warning: overriding commands for target `Obj/main.o'
Makefile:538: warning: ignoring old commands for target `Obj/main.o'
/usr/local/cuda/bin/nvcc -m64 -O3 -arch=sm_20 -c --compiler-options -fno-strict-aliasing -use_fast_math --ptxas-options=-v -I/usr/local/cuda/include -lcudart -lcuda cuda_interface.cu -o Obj/cuda_interface_cuda.o
ptxas info : Compiling entry function '__cuda_dummy_entry__' for 'sm_20'
ptxas info : Used 2 registers, 32 bytes cmem[0]
g++ -c -pipe -O3 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_CORE_LIB - DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore - I/usr/include/qt4 -I/usr/local/cuda/include -I. -o Obj/main.o main.cpp
gcc -c -pipe -O3 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_CORE_LIB - DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore - I/usr/include/qt4 -I/usr/local/cuda/include -I. -o Obj/cuda_interface.o cuda_interface.cu
gcc: warning: cuda_interface.cu: linker input file unused because linking not done
g++ -Wl,-O1 -o cuda2 Obj/cuda_interface_cuda.o Obj/main.o Obj/main.o Obj/cuda_interface.o -L/usr/local/cuda/lib -L/usr/lib/x86_64-linux-gnu -lcudart -lcuda - lQtCore -lpthread
g++: error: Obj/cuda_interface.o: No such file or directory
make: *** [cuda2] Error 1
make: Leaving directory `/home/alex/CUDA2'
13:33:36: The process "/usr/bin/make" exited with code 2.
Error while building project CUDA2 (target: Desktop)
When executing build step 'Make'
I am not sure if the .pro file will work with Linux. Looks like it was made for OSX.
Also, I don't know if the line CUDA_ARCH = sm_20 is correct. Is there a way I can find my gpu architecture? I am using an NVIDIA Quadro FX 380M
What am I doing wrong?
Thanks!
Based on the comments, you had some 'common mistakes' on your .pro file.
First, the CUDA toolkit has separated libraries directories for 32 and 64 bits. So, you need adjust the QMAKE_LIBDIR as follows:
QMAKE_LIBDIR += $$CUDA_DIR/lib64 # for 64bits operating system
or
QMAKE_LIBDIR += $$CUDA_DIR/lib # for 32bits operating system
Do not include the .cu files as SOURCES in the .pro file as they are not compiled with g++. The .pro file of your question does not show this case, but you commented that the .cu file were in the project in Qt remove them.
Finally, to be sure all your changes take effect do the following in the QT Creator IDE menu:
Build -> Clean Project # to clean old stuff
Build -> Run qmake # to take the .pro changes
Build -> Build Project # obvious
PS: As #aland commented, your GPU device compute capability is 1.2, so adjust CUDA_ARCH = sm_12.
This is the problem:
g++: error: Obj/cuda_interface.o: No such file or directory
I would delete all of your CUDA source and libraries, make sure everything is completely clean, and reinstall from scratch.
Here is a good guide:
http://laurencedawson.com/ubuntu-11.10-and-cuda-4.1
<= Assuming you already have Ubuntu and your NVidia drivers installed, then just reinstall and rebuild the CUDA toolkit and source.
Forget Qt and forget your graphical IDE until you get things working.
Please reinstall and rebuild CUDA from the command line.
Make sure you can run "deviceQuery" from the command line before you go back to Qt and the IDE.
Another good resource:
http://developer.download.nvidia.com/compute/DevZone/docs/html/C/doc/CUDA_C_Getting_Started_Linux.pdf

Getting unresolved carbon functions - mac qt 10.4 build

Trying to compile some code that is primarily Qt, but has a bunch of calls into the the Carbon layer.
In Linking, all of the Carbon calls come up unresolved.
My config cmdline build has this: -sdk /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4 -carbon -universal
My project has this: CONFIG += i386 ppc, and
this: QMAKE_CXXFLAGS += -mmacosx-version-min=10.4
As you can see, I'm targeting/using the 10.4u sdk.
All of the Carbon calls come up unresolved. Is there a way to specify the carbon libs directly on the link line?
I have no idea how to set this in Qmake, but linking to the Carbon framework is done via -framework Carbon, e.g.
g++ a.cpp -o a -framework Carbon
Since -framework is a linker option, you could try using it on Qmake’s equivalent of LDFLAGS. Alternatively, you could pass -Wl,-framework,Carbon to the compiler driver.

Precompiled Headers with Qt and g++ (QtCreator)

I'm using precompiled headers on a Qt project to speed up compilation time. I'm using Qt 4.6.2 .When I compile the project using macx-g++ (meaning the g++ compiler) it doesn't include the stdafx.h automatically for each header. When compiling under xcode it will work. I'm using the PRECOMPILED_HEADER qmake constant to point at my stdafx.h.
So is it a limitation from g++? Or there a solution/work around?
Thanks for your help,
Boris -
Sometimes gcc ignores precompiled header if some conditions aren't met. see: gcc documentation page.
Compile headers just like any other files but put the output inside a file with a suffix of .gch:
g++ -c stdafx.h -o stdafx.h.gch
g++ main.cpp
./main.out

Resources