Mac OS X 上的 gcc:如何链接使用 MacPorts 安装的库?

发布于 2024-11-24 20:11:08 字数 1046 浏览 3 评论 0原文

我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城月光淡如水﹏ 2024-12-01 20:11:08

在您的 ~/.profile 中添加以下行:

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

并在终端中运行 source ~/.profile 以重新加载您的配置文件。

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

in your ~/.profile add the following line:

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

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.

梦过后 2024-12-01 20:11:08

这取决于您是否要将可执行文件动态或静态链接到库。在 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 安装)。并且使得查找库、以跨平台的方式创建库变得非常容易。

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:

 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.

浮生未歇 2024-12-01 20:11:08

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

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

I found the easiest way is to set C_INCLUDE_PATH and LIBRARY_PATH:

export C_INCLUDE_PATH=/opt/local/include
export CPLUS_INCLUDE_PATH=/opt/local/include
export LIBRARY_PATH=/opt/local/lib
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文