OpenCL header inclusion with relative path issue in C++

后端 未结 2 1162
抹茶落季
抹茶落季 2021-02-09 20:24

I am trying to run an OpenCL C++ sample on Eclipse CTD that (on Mac) includes the OpenCL header as follows:

#include 

The fi

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-09 21:27

    I recommend you read Apple's documentation on Frameworks.

    The basic story, however, is that OS X resolves library and header search paths based on the frameworks you compile against. For example, to compile a program using the OpenCL SDK on a Macintosh, you'd compile like this:

    clang -framework OpenCL main.c
    

    This tells clang (or gcc, or llvm-gcc, depending on your choice of compiler) to search the System OpenCL SDK for both headers (for compilation) and libraries (for linking). Give it a try:

    // compile me with: clang -framework OpenCL -o test test.c
    
    #include 
    #include 
    #include 
    
    int main(int argc, char* const argv[]) {
        cl_uint num_devices, i;
        clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
    
        cl_device_id* devices = calloc(sizeof(cl_device_id), num_devices);
        clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
    
        char buf[128];
        for (i = 0; i < num_devices; i++) {
            clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 128, buf, NULL);
            fprintf(stdout, "Device %s supports ", buf);
    
            clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, 128, buf, NULL);
            fprintf(stdout, "%s\n", buf);
        }
    
        free(devices);
    }
    

    Note that I've include OpenCL/opencl.h here. This is the preferred header to include to get access to CL on the Mac. If you look at the header, you will see that it includes cl.h for you, as well as cl_platform.h. You can just include OpenCL/cl.h if you'd like; it will still work fine.

提交回复
热议问题