5

I'm trying to compile the following C++ code with Qt Creator, on Windows 7 64-bit and with the Intel SDK for OpenCL Applications 2013:

#include <utility>
#define __NO_STD_VECTOR // Use cl::vector instead of STL version
#include <CL/cl.hpp>

int main()
{
    return 0;
}

and here is my .pro file:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/C:/Program1/Intel/OpenCL_SDK/3.0/lib/x64/ -lOpenCL
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/C:/Program1/Intel/OpenCL_SDK/3.0/lib/x64/ -lOpenCLd
else:unix: LIBS += -L$$PWD/C:/Program1/Intel/OpenCL_SDK/3.0/lib/x64/ -lOpenCL

INCLUDEPATH += C:/Program1/Intel/OpenCL_SDK/3.0/include
DEPENDPATH += $$PWD/C:/Program1/Intel/OpenCL_SDK/3.0/include

I had to remove $$PWD/ (which Qt Creator added to the beginning of each path for some unknown reason) in INCLUDEPATH, otherwise the compiler wouldn't find CL/cl.cpp.

Anyway, when trying to compile, I get the following error message:

In file included from ..\opencl_test\main.cpp:3:0:
C:\Program1\Intel\OpenCL_SDK\3.0\include/CL/cl.hpp:680:1: error: expected unqualified-id before '{' token
 {
 ^

followed by a lot of other error messages. Why do I get these errors and how do I resolve them?


Update 1

As prajmus pointed out in a comment, $$PWD is the directory of the .pro file, and hence a space is missing after it, which Qt Creator missed to put there. In the LIBS specification, -L also has to be added before C:.

I fixed that, but I still get the same error message.


Update 2

If I comment out the line #define __NO_STD_VECTOR, the error messages disappear, but instead I get

c:/program1/qt/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lOpenCLd

Changing -lOpenCLd to -lOpenCL gets rid of that warning, but instead gives me

00:38:14: Running steps for project opencl_test...
00:38:14: Configuration unchanged, skipping qmake step.
00:38:14: Starting: "C:\Program1\Qt\Tools\mingw48_32\bin\mingw32-make.exe" 
C:\Program1\Qt\5.2.0\mingw48_32\bin\qmake.exe -spec win32-g++ CONFIG+=debug CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ..\opencl_test\opencl_test.pro
C:/Program1/Qt/Tools/mingw48_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'E:/Work/Programs/Test_programs/opencl_test/build-opencl_test-Desktop_Qt_5_2_0_MinGW_32bit-Debug'
g++ -Wl,-subsystem,console -mthreads -o debug\opencl_test.exe debug/main.o  -LE:/Work/Programs/Test_programs/opencl_test/opencl_test/ -LC:/Program1/Intel/OpenCL_SDK/3.0/lib/x64/ -lOpenCL
debug/main.o: In function `getPlatformVersion':
C:/Program1/Intel/OpenCL_SDK/3.0/include/CL/cl.hpp:1689: undefined reference to `clGetPlatformInfo@20'

**Now, why does it say ``clGetPlatformInfo@20'is undefined?** In [this question][1], the answer seems to be that-lOpenCLcomes before the source file. But in my case,-lOpenCL` is the very last argument of the build line. On the other hand, I can't see that main.cpp is built anywhere, so what happens to that file?

Community
  • 1
  • 1
HelloGoodbye
  • 3,624
  • 8
  • 42
  • 57
  • `OpenCLd` is the library with debugging symbols (thus the d at the end), `$$PWD` is the directory of your .pro file (so your project directory most of the time). You're missint a space after `$$PWD/` maybe it didn't work earlier for that reason. – prajmus Jan 20 '14 at 19:11
  • @prajmus: Ah! Yes, that makes sense, it's wired though that Qt Creator generates code that doesn't work. I put back the `$$PWD/` and added a space after each place it existed in the .pro file, and now it finds cl.hpp. I'm still getting the same error message, though. – HelloGoodbye Jan 20 '14 at 22:46
  • 1
    You are using a 64 Bit Windows with a 64 Bit SDK and a 32 Bit MinGW? Then this answer might help you: http://stackoverflow.com/a/12438831/1669328 – Matthias Holzapfel Jan 21 '14 at 00:44
  • @Matthias: Yes, only a 32-bit version of Qt 5.2 is available on Windows if you want to use the MinGW compiler. I don't know why. So the 32-bit version of MinGW can only link against 32-bit libraries? (I guess then it can only compile 32-bit programs as well) Why?? So if you want to to compile your application in both a 32-bit version and a 64-bit version, you need to have both the 32-bit version and the 64-bit version, of both the compiler and all of all the libraries you want to link against? – HelloGoodbye Jan 21 '14 at 18:38
  • @Matthias: Anyway, thank you! Linking against the x86 version of the OpenCL library instead of the x64 version solved the undefined reference problem. – HelloGoodbye Jan 21 '14 at 18:39
  • Since the pointer sizes differ, you cannot mix 32 and 64 bit code "just like that". You can, however, google for compiling 64 bit libraries of Qt yourself if you need the full support. Worked for me once (but with 4.8) – Matthias Holzapfel Jan 22 '14 at 19:43
  • @Matthias: Where is the mixing of 32-bit and 64-bit? My application doesn't become 32-bit automatically just because I compile it with a 32-bit compiler, right? – HelloGoodbye Jan 23 '14 at 08:57
  • 1
    Unless you perform a cross compile, the compiler uses its own architechture. See also here: http://stackoverflow.com/questions/5619341/mingw-w32-vs-mingw – Matthias Holzapfel Jan 24 '14 at 13:14

1 Answers1

2

Thanks to @Matthias, I managed to put the final piece of the puzzle. So, in conclusion:

  • The 32-bit (x86) version of MinGW can't link your application against the 64-bit (x64) version of the OpenCL library. Attempting this will give rise to undefined reference errors.
  • I had to comment away the line #define __NO_STD_VECTOR or I would get a compilation error.
  • When adding a library using the built-in "Add Library" function in Qt Creator, it will put $$PWD before the paths and put no space in between. This is supposed to be a separate folder, so there has to be a space between $$PWD and the path you specified. For the LIBS specification, there also has to be an extra -L after the space.
  • Make sure there is no d added in the ent of the library name if you have no such library file. There is an option "Add "d" suffix for debug version" when adding an internal or external library that I had missed to uncheck.

The code that finally compiled:

main.cpp:

#include <utility>
//#define __NO_STD_VECTOR // Use cl::vector instead of STL version
#include <CL/cl.hpp>

int main()
{
    return 0;
}

Qt project file:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -LC:/Program1/Intel/OpenCL_SDK/3.0/lib/x86/ -lOpenCL
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -LC:/Program1/Intel/OpenCL_SDK/3.0/lib/x86/ -lOpenCL
else:unix: LIBS += -L$$PWD/ -LC:/Program1/Intel/OpenCL_SDK/3.0/lib/x86/ -lOpenCL

INCLUDEPATH += $$PWD/ C:/Program1/Intel/OpenCL_SDK/3.0/include
DEPENDPATH += $$PWD/ C:/Program1/Intel/OpenCL_SDK/3.0/include
HelloGoodbye
  • 3,624
  • 8
  • 42
  • 57