1

I'm trying to compile something to try out openCl, but i'm having a few problems..

Here the code

prova.c

#include <stdio.h>
#include <CL/opencl.h>
#include "Utils\util.h"
#include <malloc.h>

int main(){
    cl_int error = 0;   // Used to handle error codes
    cl_int max_platforms = 1; // The maximum number of platforms
    cl_uint adviable_platforms = 0; //The adviable number of platforms
    cl_platform_id* platform;

    error = clGetPlatformIDs(0, NULL, &adviable_platforms);
    if(adviable_platforms == 0)
    {
        printf("No adviable platforms.\n");
        return -1;
    } else {
        platform = (cl_platform_id*)malloc(adviable_platforms * sizeof(cl_platform_id));
    }

    error = clGetPlatformIDs(adviable_platforms, platform, NULL);


    printf("clGetPlatformIDs: %s\n", clErrorString(error));
    return 0;
}

I'm compiling on win 7 64 with mingw32. The opencl headers are in the include directory of mingw while utils.h(inside the directory Utils inside the directory of prova.c) defines clErrorString(that simply convert the error into a more human readable string).

To compile i use

gcc -L\Utils prova.c

But i always get

C:\[stuff]\ccEjYQbj.o:prova.c:(.text+0x42): undefined reference to 'clGetPlatformIDs@12'
C:\[stuff]\ccEjYQbj.o:prova.c:(.text+0x8d): undefined reference to 'clGetPlatformIDs@12'
C:\[stuff]\ccEjYQbj.o:prova.c:(.text+0x9e): undefined reference to 'clErrorString'

I'm not so good with compilers, so i image i'm missing something, but i really don't know what..

EDIT: Sincerely, i tried every command come to my mind. using -L to include directories, -l to link to files, using ar..

This is the last "script" i tried

set PATH=%PATH%;C:\Python26;C:\MinGW\bin;C:\MinGW\lib

cd Utils
gcc -c util.c -l"C:\Program Files (x86)\AMD APP\lib\x86_64\libOpenCL.a" -o util.o
ar rcs libutil.a util.o

cd..
pause

gcc -c prova.c -l"Utils\libutil.a" -o prova.exe

pause

EDIT2:

@echo off
set PATH=%PATH%;C:\Python26;C:\MinGW\bin;C:\MinGW\lib

cd Utils
gcc -Wall -c util.c -L"C:\Program Files (x86)\AMD APP\lib\x86_64\" -o util.o
ar rcs libutil.a util.o

cd..
pause

gcc -Wall -c prova.c -L"C:\Program Files (x86)\AMD APP\lib\x86_64\" -l"Utils\libutil.a" -o prova.exe

pause

No errors, the only warning is max_platform is unused. Then i find util.o and libutil.a(size 5kb) in Utils and prova.o(size 1kb). If i try to run prova.o, it says that the file version is not compatible with the current windows version, check the system version (x86 or x64) and contact the software distributor

11
  • 4
    That's a linker error, not a compiler error. You need to link in the OpenCL library. Commented Jun 15, 2011 at 22:24
  • 1
    Look in the mingw "lib" directory and see if you can find some sort of CL library (perhaps something named "libcl*.a"). Then pass the name (minus the "lib" and the ".a") to the compiler via "-l<name>". Commented Jun 16, 2011 at 1:00
  • 1
    @Makers_F: Are you sure about your last command? -l takes a library name, not a directory, are you thinking of -L? And you're still not linking against the opencl library. Commented Jun 16, 2011 at 20:08
  • 1
    @Makers: If your last line indicates the correct relative locations, then try gcc -c prova.c "Utils\libutil.a" -o prova.exe. You don't need to use -l for static libraries, you just specify them like ordinary object files. (If you say -lname, the linker looks for a file libname.a or libname.so.) Commented Jun 16, 2011 at 22:09
  • 1
    @Makers_F: What do you mean "it doesn't link"? Do you get any errors, and from which program? Maybe can you post more detailed output of the operations you perform? Commented Jun 17, 2011 at 17:30

1 Answer 1

1

Try something like this:

set PATH=%PATH%;C:\Python26;C:\MinGW\bin;C:\MinGW\lib

cd Utils
gcc -W -Wall -c util.c -o util.o
ar rcs libutil.a util.o

cd..

gcc -W -Wall -c prova.c -o prova.o
gcc -o prova.exe prova.o Utils\libutil.a

# Using a standard library
gcc -o prog.exe myprog.o -lzip  # e.g. /usr/lib/libz.a

# Using a nonstandard library
gcc -o prog.exe myprog.o -lfoo -L/tmp/libfoo  # uses /tmp/libfoo/libfoo.a
gcc -o prog.exe myprog.o /tmp/libfoo/libfoo.a # same effect

In general:

  • Compile single source files with -c:
    gcc -c myfile.c -o myfile.o.
    This creates object files.

  • Link all the object files to an executable (or shared library):
    gcc -o prog.exe myfile.o yourstuff.o sha257.o

  • You can combine object files into a static library, which you treat just like a single object file when linking:
    ar rcs libcoolstuff.a yourstuff.o sha257.o
    gcc -o prog.exe myfile.o libcoolstuff.a
    Alternatively:
    gcc -o prog.exe myfile.o -lcoolstuff
    The latter syntax (automatic library linking with -l) requires either libcoolstuff.a or libcoolstuff.so to be findable in the library path (which you can amend with -L at linktime).
Sign up to request clarification or add additional context in comments.

5 Comments

Really good explanation!!With this i managed to compile a really simple project(that i wrote to test it)that includes a source including another source,and i managed to compile and link them.Than i tried with the openCL program.Compiling went just fine,but at link time i got "undefinde reference". I used gcc -W -Wall -c util.c -o util.o \n cd.. gcc -W -Wall -c prova.c - prova.o \n gcc -o prova.exe prova.o Utils\util.o For sure i'm still missing to add the opencl libraries. But they are in standard location (i include them with <CL\opencl.h>, so what should i add? Btw thank you for the help!
Ps: and thank you for clarifying the use of the gcc commands!!
@Makers_F: I added some examples for using libraries from other paths. If you use the -l syntax, you can specify additional library search paths with -L.
WoooooW| You're so cool! I managed to compile it! In the AMD APP folder i found a libOpenCl.a, and including this, with your (great) help, i managed to run the program. What i don't still understand is why should i link an external library when i already include the openCL headers inside my program(and the preprocessor should add them).. And how do they(AMD) get this library? I can't compile a header file.. Btw, now it works!! Thank you so much for all the time spent with me!!
@Makers_F: You should think about the compilation and linking process a bit. The headers only tell the compiler that a function int foo(); exists somewhere, but it doesn't tell it what this function actually is. The compiler can generate code that uses foo, but that code will still have an unresolved symbol for foo rather than actual code. It's the linker's job to fetch all the code for the unresolved symbols and make one single self-contained executable. And the implementation of the library functions are in the library .a file. There is no actual code in the headers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.