GCC没有将库链接到非默认路径

时间:2022-05-26 02:14:23

I have boost C++ libraries already installed on my Fedora10 machine but I want to use a newer version that I keep at some location in my home folder. I want g++ to use include and library files from my home folder location instead of default (/usr/include and /usr/lib64).

我已经在我的Fedora10机器上安装了C ++库,但我想使用一个更新版本,我保留在我的主文件夹中的某个位置。我希望g ++使用来自我的主文件夹位置的包含和库文件而不是默认值(/ usr / include和/ usr / lib64)。

For that matter, I also have declared CPLUS\_INCLUDE\_PATH and LIBRARY\_PATH environment variables in my ~/.bashrc file as explained here.

就此而言,我也在我的〜/ .bashrc文件中声明了CPLUS \ _INCLUDE \ _PATH和LIBRARY \ _PATH环境变量,如下所述。

Now when I run,

现在我跑的时候

g++ -o hello.so -fPIC hello.cpp -shared -lboost_python

The preprocessor uses include files from my home folder location, overriding the default location (as it should, because CPLUS\_INCLUDE\_PATH has a higher precedence in the search path). But the linker does not seem to follow the same precedence rule. It always uses libboost_python.so from the default location /usr/lib64 instead of first searching LIBRARY\_PATH. It only links to the libboost\_python.so library in my home folder when I explicitly specify with -L switch. This is really inconvenient.

预处理器使用来自我的主文件夹位置的包含文件,覆盖默认位置(因为它应该,因为CPLUS \ _INCLUDE \ _PATH在搜索路径中具有更高的优先级)。但链接器似乎没有遵循相同的优先级规则。它始终使用默认位置/ usr / lib64中的libboost_python.so,而不是首先搜索LIBRARY \ _PATH。当我使用-L开关明确指定时,它只链接到我的主文件夹中的libboost \ _python.so库。这真的很不方便。

1 个解决方案

#1


The -L switch is the standard way of telling the compiler where to find the libraries. Write a makefile that builds your compiler/linker switches - you'll find it's worth investing your time. You can do something like:

-L开关是告诉编译器在哪里找到库的标准方法。编写一个构建编译器/链接器开关的makefile - 你会发现值得投入时间。你可以这样做:

MY_LIBPATH += -L$(BOOST_LIB_PATH)
MY_INCPATH += -I$(BOOST_INC_PATH)
hello.so: hello.cpp
    g++ -o $@ -fPIC $(MY_INCPATH) $(MY_LIBPATH) hello.cpp -shared -lboost_python

And then you can control this via environment (of course there could be many variations on how to structure the makefile.)

然后你可以通过环境来控制它(当然,如何构造makefile可能会有很多变化。)

#1


The -L switch is the standard way of telling the compiler where to find the libraries. Write a makefile that builds your compiler/linker switches - you'll find it's worth investing your time. You can do something like:

-L开关是告诉编译器在哪里找到库的标准方法。编写一个构建编译器/链接器开关的makefile - 你会发现值得投入时间。你可以这样做:

MY_LIBPATH += -L$(BOOST_LIB_PATH)
MY_INCPATH += -I$(BOOST_INC_PATH)
hello.so: hello.cpp
    g++ -o $@ -fPIC $(MY_INCPATH) $(MY_LIBPATH) hello.cpp -shared -lboost_python

And then you can control this via environment (of course there could be many variations on how to structure the makefile.)

然后你可以通过环境来控制它(当然,如何构造makefile可能会有很多变化。)