Macos Mac OS X上的gcc:如何链接安装了MacPorts的库?
我已经使用macports安装了GCC4.6。前缀是Macos Mac OS X上的gcc:如何链接安装了MacPorts的库?,macos,gcc,linker,g++,macports,Macos,Gcc,Linker,G++,Macports,我已经使用macports安装了GCC4.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++//backwa
/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.
对于安装了macports的其他库来说,这是一个问题。有没有一种简单的方法可以将
/opt/local/lib
添加到库搜索路径?我已尝试设置DYLD\u LIBRARY\u路径
,但无效。我使用的是Mac OS X 10.6.8。这取决于是否要将可执行文件动态或静态链接到库。在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安装)。并且可以很容易地找到库,以跨平台的方式创建库。在~/.配置文件中添加以下行:
export LDFLAGS="-L/opt/local/lib"
然后在终端中运行source~/.profile
,重新加载您的配置文件
这样,将从gcc/g++中检测到-L开关并自动使用。我发现最简单的方法是设置
C\u INCLUDE\u PATH
和LIBRARY\u PATH
:
export C_INCLUDE_PATH=/opt/local/include
export CPLUS_INCLUDE_PATH=/opt/local/include
export LIBRARY_PATH=/opt/local/lib
我的问题是关于如何在gcc的库搜索路径中设置
/opt/local/lib
。Gcc是内置的/opt/local,所以我希望它位于搜索路径中,但它不是。DYLD\u LIBRARY\u路径
用于在运行时动态加载,而不是编译时。您是如何构建的?如果我只使用命令行g++,那么这个环境变量没有效果。应该使用LIBRARY_PATH变量,不带“-L”
export C_INCLUDE_PATH=/opt/local/include
export CPLUS_INCLUDE_PATH=/opt/local/include
export LIBRARY_PATH=/opt/local/lib