Mac OS X上的gcc:如何链接与MacPorts一起安装的库?

时间:2011-07-18 18:44:19

标签: macos gcc linker g++ macports

我使用macports安装了gcc 4.6。前缀为/opt/local,我得到了预期的包含路径:

#include "..." search starts here:
#include <...> search starts here:  
/opt/local/include/gcc46/c++/  
/opt/local/include/gcc46/c++//x86_64-apple-darwin10  
/opt/local/include/gcc46/c++//backward  
/opt/local/lib/gcc46/gcc/x86_64-apple-darwin10/4.6.1/include  
/opt/local/include  
/opt/local/lib/gcc46/gcc/x86_64-apple-darwin10/4.6.1/include-fixed  
/usr/include  
/System/Library/Frameworks  
/Library/Frameworks End of search list.

但是,/opt/local/lib似乎不在库搜索路径中,因此在命令行上使用g ++时我必须使用-L/opt/local/lib指定它:

Library search paths:
    /opt/local/lib/gcc46/gcc/x86_64-apple-darwin10/4.6.1
    /opt/local/lib/gcc46
    /usr/lib
    /usr/local/lib
Framework search paths:
    /Library/Frameworks/
    /System/Library/Frameworks/

对于安装了macport的其他库,这是一个问题。有没有简单的方法将/opt/local/lib添加到库搜索路径?我试过设置DYLD_LIBRARY_PATH无济于事。我使用的是Mac OS X 10.6.8。

3 个答案:

答案 0 :(得分:7)

你的〜/ .profile中的

添加以下行:

export LDFLAGS="-L/opt/local/lib"

并在终端中运行source ~/.profile以重新加载您的个人资料。

这样,将从gcc / g ++中检测-L开关并自动使用。

答案 1 :(得分:2)

这取决于您是否要将可执行动态或静态链接到库。在OS X下,您可以将库添加为源/目标文件,如下所示:

 Dynamic: g++ -Wall -o myexecutable myfile.cpp /path/to/library.dylib
 Static: g++ -Wall -o myexecutable myfile.cpp /path/to/library.a

最好的方法是使用构建系统,例如CMake(可以从macports安装)。并且可以很容易地找到库,以跨平台的方式创建库。

答案 2 :(得分:0)

我发现最简单的方法是设置C_INCLUDE_PATHLIBRARY_PATH

export C_INCLUDE_PATH=/opt/local/include
export CPLUS_INCLUDE_PATH=/opt/local/include
export LIBRARY_PATH=/opt/local/lib