Nm shows symbol in shared library, but undefined symbol when linking

  amd-gpu, c++, linker, linker-errors, macos

As a mental exercise, I’m trying to write a program which links directly against the GPU driver of my Macbook Pro rather than using Apple’s Metal framework. Some exploration led me to this file (presumably specific to my particular hardware):

/System/Library/Extensions/AMDRadeonX6000MTLDriver.bundle/Contents/MacOS/AMDRadeonX6000MTLDriver

Running file on it confirms this is a Mach-O 64-bit dynamically linked shared library.
Running nm on it tells me it’s a superset of AMD’s ROCr runtime. One symbol in particular that interests me is this one:

$ nm -gCD AMDRadeonX6000MTLDriver | grep "hsa_init"
00000000001cca20 T HSA::hsa_init()

So I wrote this simple program (rocr_test.cpp):

typedef int hsa_status_t;

namespace HSA {
    hsa_status_t hsa_init();
}

int main() {
    HSA::hsa_init();
    return 0;
}

And compiled it like so:

$ clang++ rocr_test.cpp /System/Library/Extensions/AMDRadeonX6000MTLDriver.bundle/Contents/MacOS/AMDRadeonX6000MTLDriver
Undefined symbols for architecture x86_64:
  "HSA::hsa_init()", referenced from:
      _main in rocr_main-95c854.o
ld: symbol(s) not found for architecture x86_64
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

Why am I seeing this linker error, when nm shows that this symbol clearly exists in the shared library?

Source: Windows Questions C++

LEAVE A COMMENT