在OS X上安装Boehm GC

问题描述

我想在MacOS上安装Boehm garbage collector垃圾收集器。我查看了this指南,但没有帮助;调用brew install libgc无济于事。这是我尝试运行的示例代码

#include <gc/gc.h>

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

不幸的是,我收到此错误

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)

有人知道一种无需从源代码构建GC的好方法吗?

解决方法

当我像您一样在Mac上安装libgc时,文件已安装到/usr/local/Cellar/bdw-gc/。然后,当需要编译我的代码时,我必须运行:

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

安装libgc时,它不包含在系统路径中。您需要显式添加它。

我在我的代码中也使用过:

#include "gc.h"

而不是<gc/gc.h>