macos Mac OS X 上的 gcc:如何链接用 MacPorts 安装的库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6737738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gcc on Mac OS X: how to link libraries installed with MacPorts?
提问by juanchopanza
I have installed gcc 4.6 using macports. The prefix is /opt/local
, and I get the expected include path:
我已经使用 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.
However, /opt/local/lib
doesn't seem to be in the library search path, so I have to specify it with -L/opt/local/lib
when using g++ on command line:
但是,/opt/local/lib
似乎不在库搜索路径中,因此-L/opt/local/lib
在命令行上使用 g++ 时我必须指定它:
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/
This is a problem for other libraries installed with macports. Is there an easy way to add /opt/local/lib
to the library search path? I have tried setting DYLD_LIBRARY_PATH
to no avail. I am using Mac OS X 10.6.8.
这对于与 macports 一起安装的其他库来说是一个问题。有没有一种简单的方法可以添加/opt/local/lib
到库搜索路径中?我试过设置DYLD_LIBRARY_PATH
无济于事。我使用的是 Mac OS X 10.6.8。
回答by Michael Xu
in your ~/.profile add the following line:
在您的 ~/.profile 添加以下行:
export LDFLAGS="-L/opt/local/lib"
and run source ~/.profile
in the terminal to reload your profile.
并source ~/.profile
在终端中运行以重新加载您的配置文件。
In this way, the -L switch will be detected from gcc/g++ and used automaticaly.
这样,-L 开关将从 gcc/g++ 中检测到并自动使用。
回答by Jerry Jacobs
It depends if you want to link your executable dynamic or static against a library. Under OS X you add the libraries as source/object files like this:
这取决于您是否要将可执行文件动态或静态链接到库。在 OS X 下,您将库添加为源/目标文件,如下所示:
Dynamic: g++ -Wall -o myexecutable myfile.cpp /path/to/library.dylib
Static: g++ -Wall -o myexecutable myfile.cpp /path/to/library.a
The best way is to use a build system, for example CMake (which can be installed from macports). And makes it very easy to find libraries, create libraries in a crossplatform way.
最好的方法是使用构建系统,例如 CMake(可以从 macports 安装)。并且可以很容易地找到库,以跨平台的方式创建库。
回答by nwellnhof
I found the easiest way is to set C_INCLUDE_PATH
and LIBRARY_PATH
:
我发现最简单的方法是设置C_INCLUDE_PATH
和LIBRARY_PATH
:
export C_INCLUDE_PATH=/opt/local/include
export CPLUS_INCLUDE_PATH=/opt/local/include
export LIBRARY_PATH=/opt/local/lib