Search code examples
c++objective-cmacoscore-graphicsbuild-error

OSX CGGetActiveDisplayList -> ld: symbol(s) not found for architecture x86_64


I'm trying to write cross-system display management for a nodejs module. I've been successful on windows, but now I'm stuck on osx (linux is next).

I just can't get this short bit of code to work. I'm compiling with "make osx" and that works fine, as long as I don't use CGGetActiveDisplayList. https://developer.apple.com/documentation/coregraphics/1454603-cggetactivedisplaylist

As far as I have researched, I am including the correct header file, but I am doubting this more, but can't find/see any other info.

If I comment out the CGGetActiveDisplayList code, CGDirectDisplayID still gets accepted, so I am assuming it's not completely wrong. It's probably quite simple, but I've been stuck for a few hours and it's pretty frustrating.

I've tried with osx.m (objective_c) and get the same error.

The file is "osx.cc"

#ifdef __APPLE__

#include <ApplicationServices/ApplicationServices.h>

int main(int argc, const char * argv[]){
    printf("\nHello!\n\n");
    CGDirectDisplayID displays[32];
    uint32_t count;//sizeof(displays)/sizeof(displays[0])
    if (CGGetActiveDisplayList(32, displays, &count) != kCGErrorSuccess)
    {
        printf("failed to get display list");
        exit(EXIT_FAILURE);
    }
}

#endif

thanks for your time!

edit:

make osx
c++     osx.cc   -o osx
Undefined symbols for architecture x86_64:
  "_CGGetActiveDisplayList", referenced from:
      _main in osx-b7fd55.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [osx] Error 1

Solution

  • It is not enough to import/include header files, you have to add Core Graphics framework to the linker. The framework name is shown in the manual you referenced. Please look at the answer to the similar question Undefined symbols for architecture x86_64 in Objective-C

    1. Select the project file from the project navigator on the left side of the project window in xcode.
    2. Select the target for where you want to add frameworks in the project settings editor.
    3. Select the “Build Phases” tab, and click the small triangle next to “Link Binary With Libraries” to view all of the frameworks in your application.
    4. To Add frameworks, click the “+” below the list of frameworks.
    5. Select desired frameworks.