Search code examples
cgarbage-collectionclangldboehm-gc

Installing the Boehm GC on OS X


I want to install the Boehm garbage collector garbage collector on MacOS. I looked at this guide but it did not help; invoking brew install libgc did nothing. Here is my example code that I am trying to run:

#include <gc/gc.h>

int main() {
    void* eight_bytes = GC_MALLOC(8);
}

Unfortunately, I get this error:

Undefined symbols for architecture x86_64:
  "_GC_malloc", referenced from:
      _main in boehm_invocation-369838.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Does anyone know of a good way to install this GC without building it from source?


Solution

  • When I installed libgc on mac, as you did, the files were installed to /usr/local/Cellar/bdw-gc/. Then, when it came time to compile my code I had to run:

    $ LIBGC=/usr/local/Cellar/bdw-gc/VERSION/
    $ gcc ... -I$LIBGC/include/ ... $LIBGC/lib/libgc.a other.a ...
    

    When you install libgc, its not included in your system path. You need to explicitly add it.

    Also in my code I used:

    #include "gc.h"
    

    And not <gc/gc.h>.