13
votes

Is it possible to compile OpenCL using Mingw and Nvidia SDK?

I'm aware that its not officially supported but that just doesn't make sense. Aren't the libraries provided as a statically linked libraries? I mean once compiled with whatever compiler that may be, and linked successfully, whats should be the problem?

I managed to compile and successfully link my code to OpenCL libraries provided with Nvidia's SDK, however the executable throws Segmentation Fault at clGetPlatformIDs which is the first OpenCL call in my code.

Here is my compilation command

x86_64-w64-mingw32-g++ -std=c++11 File.cpp \
-L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\lib\x64" \
-I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include" \
-gcoff -lOpenCL -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 \
-lshell32 -lole32 -loleaut32 \
-luuid -lodbc32 -lodbccp32 -luser32 -lgdi32 -lmingwex -m64 \
-DWIN32 -D_DEBUG -D_CONSOLE

Note that these libraries were taken from the .sln file of the sample OpenCL code.

I tried various combinations of these libraries and nothing seems to work.

Also is there a way to compile using Mingw and link using something else? I'm not sure what the something else might be.

2
Why don't you use VC++ Express, to build the host code? Also are you using 32 bit or 64bit libs? There is a confusing mix of CUDA\v5.0\lib\x64 and -luse32 -lcomdlg32 -ladvai32 ... -m64 are all these libs necessary? Simplify your command line.Tim Child
64-bit, my C++ code uses C++11 features so VC++ is no go. I tried removing and adding libs, dead end.omarzouk

2 Answers

15
votes

We have been able to do this by using the MinGW reimp and dlltool utilities to create an import library "libOpenCL.a" for the "OpenCL.dll" DLL. Here are the steps we followed:

  • step 1: Make sure you have mingw-utils installed (for reimp and dlltool). If you don't, at a MinGW prompt, type:

    mingw-get install mingw-utils

  • step 2: Next, type the following command:

    reimp OpenCL.lib

This should yield an OpenCL.def and libOpenCL.a file. If the result is something along the lines of "invalid or corrupt import library", the alternative step 2 should work.

  • alternative step 2: Use dlltool together with a .def file that you can find here: http://pastebin.com/f2ac38b2f . The command becomes:

    dlltool -l libOpenCL.a -d OpenCL.def -A -k

You can now link against the newly created libOpenCL.a. If you still see missing symbols, add them to the .def file and repeat the dlltool command.

We were able to get this all working thanks to the information found in following posts, for reference:

  1. http://oscarbg.blogspot.com/2009/12/opencl-with-mingw.html
  2. http://www.mingw.org/wiki/CreateImportLibraries (I can only paste 2 links)
3
votes

OpenCL libraries are just stubs to the OpenCL.dll. I recomend therefore using a dynamic loading technique. It will simply solve all your problems. Or do you really need some of the nVIDIA tweaks of OpenCL?

It is not only safer at runtime, but also easyer to compile since you need no linker nor defines, nor anything. Just a simple cpp/hpp files added to your proyect.

A very good one to use is CLEW. However I don't agree completely with that library, because C++ wrappers are broken. So maybe you will need some tweaking. Also I typically disable the "atexit" code, since it can be problematic.