Question

I'm currently learning OpenCL. Now, when I want to compile my program, I get an error with this command:

g++ -Wall -l OpenCL main.cpp -o main

The errors are mostly undefined references, because the library is not linked, I think (nevertheless I will post the error code at the end).

But with this command everything works fine:

g++ -Wall main.cpp -o main -l OpenCL

So my question is, what do I have to do, to use the -l Flag in front of the command? (The Background is: I want to use Netbeans to compile my programm and when i add the flag under -> properties -> build -> C++ Compiler -> additional options, it will put in in the Position, shown in the first command)

Thanks in advance for your help

Here's the error code:

/tmp/ccmKP4oI.o: In function `cl::detail::ReferenceHandler<_cl_context*>::release(_cl_context*)':
main.cpp:(.text._ZN2cl6detail16ReferenceHandlerIP11_cl_contextE7releaseES3_[_ZN2cl6detail16ReferenceHandlerIP11_cl_contextE7releaseES3_]+0x14): undefined reference to `clReleaseContext'
/tmp/ccmKP4oI.o: In function `cl::detail::ReferenceHandler<_cl_command_queue*>::release(_cl_command_queue*)':
main.cpp:(.text._ZN2cl6detail16ReferenceHandlerIP17_cl_command_queueE7releaseES3_[_ZN2cl6detail16ReferenceHandlerIP17_cl_command_queueE7releaseES3_]+0x14): undefined reference to `clReleaseCommandQueue'
/tmp/ccmKP4oI.o: In function `cl::Platform::getInfo(unsigned int, std::string*) const':
main.cpp:(.text._ZNK2cl8Platform7getInfoEjPSs[_ZNK2cl8Platform7getInfoEjPSs]+0x22): undefined reference to `clGetPlatformInfo'
/tmp/ccmKP4oI.o: In function `cl::Platform::get(std::vector<cl::Platform, std::allocator<cl::Platform> >*)':
main.cpp:(.text._ZN2cl8Platform3getEPSt6vectorIS0_SaIS0_EE[_ZN2cl8Platform3getEPSt6vectorIS0_SaIS0_EE]+0x41): undefined reference to `clGetPlatformIDs'
main.cpp:(.text._ZN2cl8Platform3getEPSt6vectorIS0_SaIS0_EE[_ZN2cl8Platform3getEPSt6vectorIS0_SaIS0_EE]+0xb4): undefined reference to `clGetPlatformIDs'
collect2: error: ld returned 1 exit status
Was it helpful?

Solution

Order of [most] arguments to g++ is very important.

Libraries should go last (at least after source and object files). You can't really change that.

The -l should preferably be glued to the library name:

 g++ -Wall main.cpp -o main -lOpenCL
 #                          ^^^ glue the -l to the library name

You probably want to also pass -g (in addition of -Wall) to the compiler to get a debuggable binary. Use the gdb debugger.

As James Kanze commented, you might want to replace -g with -ggdb if using specifically gdb.

OTHER TIPS

With g++ (and generally under Unix), -l specifies a source of input (either a .a or a .so), and input is processed in order. When the input is a static library (a .a file), it will be scanned for objects which resolve undefined references; if it is a .so, there aren't any object files in it, but it will still only be taken into consideration if it resolves some undefined symbol.

When you put the -l before any object files, there are no undefined symbols yet, so nothing will be incorporated into the program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top