Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a simple OpenCL application with Qt Creator?

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?

like image 854
HelloGoodbye Avatar asked Nov 02 '22 06:11

HelloGoodbye


1 Answers

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
like image 143
HelloGoodbye Avatar answered Nov 16 '22 18:11

HelloGoodbye