GCC: Compiling an OpenCL host on Windows

I just wanted to try out using OpenCL under Windows.

Abstract: I got an "undefined reference to" error when I tried to compile (using the command gcc my.o -o my.exe -L "C:\Program Files (x86)\AMD APP\lib\x86_64" -l OpenCL).


My Code

#include <CL/cl.h>
#include <stdio.h>

int main(void) {   
    cl_platform_id platform;
    int err;

    err = clGetPlatformIDs(1, &platform, NULL);
    if(err < 0) {
        perror("There's No Platform!");
        exit(1);
    }

    /* Some more code... */

    system("PAUSE");
}

Makefile

all: addition

addition:
    gcc -c -I "C:\Program Files (x86)\AMD APP\include" my.c -o my.o
    gcc my.o -o my.exe -L "C:\Program Files (x86)\AMD APP\lib\x86_64" -l OpenCL

Used Programs

  • MinGW's gcc
  • Visual Studio's nmake
  • AMD's OpenCL™ APP SDK (that's where I took the OpenCL library and the CL.h file from)

The Structure of APP SDK's Folders

%>tree /F "C:\Program Files (x86)\AMD APP\lib\x86_64"
Auflistung der Ordnerpfade
Volumeseriennummer : D2DC-D765
C:\PROGRAM FILES (X86)\AMD APP\LIB\X86_64
    libOpenCL.a
    OpenCL.lib
    OpenVideo64.lib

Es sind keine Unterordner vorhanden


%>tree /F "C:\Program Files (x86)\AMD APP\include"
Auflistung der Ordnerpfade
Volumeseriennummer : D2DC-D765
C:\PROGRAM FILES (X86)\AMD APP\INCLUDE
├───CAL
       cal.h
       calcl.h
       cal_ext.h
       cal_ext_counter.h
       cal_ext_d3d10.h
       cal_ext_d3d9.h

├───CL
       cl.h
       cl.hpp
       cl_d3d10.h
       cl_ext.h
       cl_gl.h
       cl_gl_ext.h
       cl_platform.h
       opencl.h

└───OpenVideo
        OpenVideo.h
        OVDecode.h
        OVDecodeTypes.h
        OVEncode.h
        OVEncodeTypes.h

Error Message

        gcc addition.o -o addition.exe -L "C:\Program Files (x86)\AMD APP\lib\x86_64" -l OpenCL
addition.o:addition.c:(.text+0x2d): undefined reference to `clGetPlatformIDs@12'
addition.o:addition.c:(.text+0x83): undefined reference to `clGetDeviceIDs@24'
addition.o:addition.c:(.text+0xc2): undefined reference to `clGetDeviceIDs@24'
collect2: ld returned 1 exit status
NMAKE : fatal error U1077: "C:\prog-x86\MinGW\bin\gcc.EXE": Rückgabe-Code "0x1"
Stop.

My Questions

My questions are simple:

  • Why doesn't my code compile how it is expected to?
  • What can I do to get rid of this problem?

Thanks.


UPDATE: The error message after dropping the spaces like described in @codaddict 's answer.

(Makefile)

all: addition

addition:
  gcc -c -I "C:\prog-x86\AMD-APP\include" addition.c -o addition.o
  gcc addition.o -o addition.exe -LC:\prog-x86\AMD-APP\lib\x86_64 -lOpenCL

(Shelldata)

%>nmake

Microsoft (R) Program Maintenance Utility, Version 11.00.50727.1
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.

        gcc -c -I "C:\Program Files (x86)\AMD APP\include" addition.c -o addition.o
addition.c: In function 'main':
addition.c:14:9: warning: incompatible implicit declaration of built-in function 'exit' [enabled by d
efault]
addition.c:23:9: warning: incompatible implicit declaration of built-in function 'exit' [enabled by d
efault]
        gcc addition.o -o addition.exe -LC:\prog-x86\AMD-APP\lib\x86_64 -lOpenCL
addition.o:addition.c:(.text+0x2d): undefined reference to `clGetPlatformIDs@12'
addition.o:addition.c:(.text+0x83): undefined reference to `clGetDeviceIDs@24'
addition.o:addition.c:(.text+0xc2): undefined reference to `clGetDeviceIDs@24'
collect2: ld returned 1 exit status
NMAKE : fatal error U1077: "C:\prog-x86\MinGW\bin\gcc.EXE": Rückgabe-Code "0x1"
Stop.

%>
share | improve this question
 
 
Can you try duplicating the backslashes in the gcc parameters? Like "C:\\Program Files (x86)\\..."? Otherwise they might get escaped and break the path. Unsure this is the reason but you never know.  –  Thomas   Sep 15 '12 at 15:03  
 
@Thomas Thanks for your idea, but that didn't change anything.  –    fridojet   Sep 15 '12 at 15:13
 
Does it say something different if you drop the -L parameter and instead give it the full path to OpenCL.lib? So remove -Lpath and add an unqualified "C:\Program Files (x86)\...\x86_64\OpenCL.lib" to tell gcc "hey here's a file you might find useful"?  –    Thomas   Sep 15 '12 at 15:16
 
@Thomas Did you mean something like gcc addition.o -o addition.exe "C:\Program Files (x86)\AMD APP\lib\x86_64\OpenCL.lib" -l OpenCL?  –    fridojet   Sep 15 '12 at 15:24
1  
Ok, never mind then, the fact that it returned that means it correctly finds the OpenCL library the way you're doing now - it just fails to link against it. Try -static flag...  –    Thomas   Sep 15 '12 at 15:36
show 4 more comments

2 Answers

up vote 4 down vote accepted

The default MinGW distribution only ships tools for building x86 applications. You cannot link against the x64 version of the OpenCL library. So you either have to use MinGW-w64 or use the x86 version (change the library path to the x86 subfolder of the APP SDK).

share | improve this answer
 
 
That really helped.  –    fridojet   Sep 15 '12 at 15:59
add comment

Try changing

-l OpenCL

to

-lOpenCL

in the compile line.

The -l option of gcc expects the name of the library (without the lib prefix) right next to it without any space. Remember using -lm in school to link to libm?

share | improve this answer
 
 
Thanks for your proposal, but it didn't change anything.  –    fridojet   Sep 15 '12 at 14:44  
 
@fridojet: Can you also drop the space after -L?  –    codaddict   Sep 15 '12 at 14:47
 
I tried that too, but it didn't help either. Thanks for your idea - Any other one?  –    fridojet   Sep 15 '12 at 14:49
 
edited my question text in order to describe the reactions on running the Makefile after dropping the spaces.  –    fridojet   Sep 15 '12 at 14:55  
 
You have also changed the library path to C:\prog-x86\AMD-APP\lib\x86_64. Are you sure the libraries are there ?  –    codaddict   Sep 15 '12 at 15:00
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值