Mac OS X 上的 gcc:如何链接使用 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/
对于使用 macports 安装的其他库来说,这是一个问题。有没有一种简单的方法可以将 /opt/local/lib
添加到库搜索路径?我尝试设置DYLD_LIBRARY_PATH
但无济于事。我使用的是 Mac OS X 10.6.8。
I have installed gcc 4.6 using macports. The prefix is /opt/local
, and I get the expected include path:
#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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的 ~/.profile 中添加以下行:
并在终端中运行
source ~/.profile
以重新加载您的配置文件。这样,-L 开关将从 gcc/g++ 中检测到并自动使用。
in your ~/.profile add the following line:
and run
source ~/.profile
in the terminal to reload your profile.In this way, the -L switch will be detected from gcc/g++ and used automaticaly.
这取决于您是否要将可执行文件动态或静态链接到库。在 OS X 下,您可以将库添加为源/目标文件,如下所示:
最好的方法是使用构建系统,例如 CMake(可以从 macports 安装)。并且使得查找库、以跨平台的方式创建库变得非常容易。
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:
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.
我发现最简单的方法是设置
C_INCLUDE_PATH
和LIBRARY_PATH
:I found the easiest way is to set
C_INCLUDE_PATH
andLIBRARY_PATH
: