有没有一种智能方法可以知道编译时要链接的库的名称? (Linux/库班图)

发布于 2024-09-14 09:02:15 字数 6013 浏览 4 评论 0原文

请耐心听我说。我真的很想知道,因为我对答案很好奇:

是否有一种智能方法可以知道编译时要链接到的库的名称?

请允许我举一个例子来完美地说明我提出问题的原因。

我是一个 C++ 新手。我正在学习编译、链接和像 boost 这样的库。我刚刚发现了 boost/filesystem 并想尝试一下。由于编译遇到问题,我使用了以下简约代码:


// file boost_example.cpp
 #include "boost/filesystem.hpp"
#include <iostream>

int main() { std::cout<<"你好"; 返回0; }

我尝试编译它,但作为一个新手,我犯了一个新手错误:我忘记链接到正确的库!
<代码>

     g++ boost_example.cpp -o run

To be precise, I experienced the problem explained in this chapter of this fine book.
I knew I had boost installed (I was told to install it to compile another project). I tried to copy from the Makefile of that other project, but the following didn't work:
     g++ boost_example.cpp -o run -lbooster
     g++ boost_example.cpp -o run -lboost
Trying to make an educated guess from the #include line in the code, I tried the following to no avail:
     g++ boost_example.cpp -o run -lboost_filesystem
By that time, I had started to search the web frantically. I hate search engines because most of the time, you don't find what you're looking. I found the following but they didn't help but to waste my time:
Boost library link problem under kubuntu
C++/Boost linker errors

由于我是 RTFM 类型的人,因此我实际上检查了我想要使用的库的官方文档: http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/index.htm 但我没有找到任何编译信息。

在某个阶段,我受到启发,检查了我的系统上实际安装的内容: <代码>

$ locate boost_file
/usr/lib/libboost_filesystem-mt.a
/usr/lib/libboost_filesystem-mt.so
/usr/lib/libboost_filesystem-mt.so.1.38.0
Thus, I found the proper name of the library to link. The following worked:
     g++ boost_example.cpp -o run -lboost_filesystem-mt

现在,除了使用(可能)智能猜测和搜索网络之外,是否有更智能的方法来查找要链接的库的名称?给定标题“boost/filesystem.hpp”,我永远不会猜到库名称 boost_filesystem-mt

更糟糕的是:

再说一次,我总是在提出问题之前先强调一下 RTFM,我发现 上述书的这一章,所以我检查了我可以在我的系统上在 /usr/lib/ 找到的内容:
<代码>

$ ls /usr/lib/boost
/usr/lib/libboost_date_time-mt.a           /usr/lib/libboost_prg_exec_monitor-mt.so.1.38.0
/usr/lib/libboost_date_time-mt.so          /usr/lib/libboost_program_options-mt.a
/usr/lib/libboost_date_time-mt.so.1.38.0   /usr/lib/libboost_program_options-mt.so
/usr/lib/libbooster.a                      /usr/lib/libboost_program_options-mt.so.1.38.0
/usr/lib/libbooster.so                     /usr/lib/libboost_python-mt.a
/usr/lib/libbooster.so.0                   /usr/lib/libboost_python-mt-py25.a
/usr/lib/libbooster.so.0.0.0               /usr/lib/libboost_python-mt-py25.so
/usr/lib/libboost_filesystem-mt.a          /usr/lib/libboost_python-mt-py25.so.1.38.0
/usr/lib/libboost_filesystem-mt.so         /usr/lib/libboost_python-mt-py26.a
/usr/lib/libboost_filesystem-mt.so.1.38.0  /usr/lib/libboost_python-mt-py26.so
/usr/lib/libboost_graph-mt.a               /usr/lib/libboost_python-mt-py26.so.1.38.0
/usr/lib/libboost_graph-mt.so              /usr/lib/libboost_python-mt.so
/usr/lib/libboost_graph-mt.so.1.38.0       /usr/lib/libboost_regex-mt.a
/usr/lib/libboost_iostreams-mt.a           /usr/lib/libboost_regex-mt.so
/usr/lib/libboost_iostreams-mt.so          /usr/lib/libboost_regex-mt.so.1.38.0
/usr/lib/libboost_iostreams-mt.so.1.38.0   /usr/lib/libboost_serialization-mt.a
/usr/lib/libboost_math_c99f-mt.a           /usr/lib/libboost_serialization-mt.so
/usr/lib/libboost_math_c99f-mt.so          /usr/lib/libboost_serialization-mt.so.1.38.0
/usr/lib/libboost_math_c99f-mt.so.1.38.0   /usr/lib/libboost_signals-mt.a
/usr/lib/libboost_math_c99l-mt.a           /usr/lib/libboost_signals-mt.so
/usr/lib/libboost_math_c99l-mt.so          /usr/lib/libboost_signals-mt.so.1.38.0
/usr/lib/libboost_math_c99l-mt.so.1.38.0   /usr/lib/libboost_system-mt.a
/usr/lib/libboost_math_c99-mt.a            /usr/lib/libboost_system-mt.so
/usr/lib/libboost_math_c99-mt.so           /usr/lib/libboost_system-mt.so.1.38.0
/usr/lib/libboost_math_c99-mt.so.1.38.0    /usr/lib/libboost_thread-mt.a
/usr/lib/libboost_math_tr1f-mt.a           /usr/lib/libboost_thread-mt.so
/usr/lib/libboost_math_tr1f-mt.so          /usr/lib/libboost_thread-mt.so.1.38.0
/usr/lib/libboost_math_tr1f-mt.so.1.38.0   /usr/lib/libboost_unit_test_framework-mt.a
/usr/lib/libboost_math_tr1l-mt.a           /usr/lib/libboost_unit_test_framework-mt.so
/usr/lib/libboost_math_tr1l-mt.so          /usr/lib/libboost_unit_test_framework-mt.so.1.38.0
/usr/lib/libboost_math_tr1l-mt.so.1.38.0   /usr/lib/libboost_wave-mt.a
/usr/lib/libboost_math_tr1-mt.a            /usr/lib/libboost_wave-mt.so
/usr/lib/libboost_math_tr1-mt.so           /usr/lib/libboost_wave-mt.so.1.38.0
/usr/lib/libboost_math_tr1-mt.so.1.38.0    /usr/lib/libboost_wserialization-mt.a
/usr/lib/libboost_prg_exec_monitor-mt.a    /usr/lib/libboost_wserialization-mt.so
/usr/lib/libboost_prg_exec_monitor-mt.so   /usr/lib/libboost_wserialization-mt.so.1.38.0

现在我非常苦恼,因为我真的不知道如何将上述内容与 关联起来boost 库列表

我对这个冗长的例子表示歉意,但它完美地说明了我的问题:

除了猜测、搜索网络、询问其他人、反复试验等之外......是否有更智能的方法来查找名称应该链接到的库?难道这些东西不应该记录在某处吗?

我当然有兴趣知道关于 boost 的答案,但我的问题更通用,适用于任何库。

顺便说一句,我对 Linux 软件包的名称(无论是 .deb 还是 .rpm)有一个非常类似的问题。如果我被告知要编译这样或那样的软件,例如我需要 PRCE 或 FooBar,我如何准确地知道要安装的软件包的名称?我知道如何使用 apt-cache (对于 Kubuntu debs),但有些包命名并不直观,我经常最终安装我并不真正需要的包......

Please bear with me. I really want to know as I am curious about the answer:

Is there an intelligent way to know the name of the library to link to at compile time?

Allow me to give you an example that illustrates perfectly the reason for my question.

I am a C++ newbie. I am learning about compiling, linking and libraries like boost. I just discovered boost/filesystem and wanted to try it. Having had troubles compiling, I used the following minimalist code:


// file boost_example.cpp
 #include "boost/filesystem.hpp"
#include <iostream>

int main() { std::cout<<"Hello"; return 0; }

I tried to compile it, but being a newbie, I made a newbie mistake: I forgot to link to the proper library!

     g++ boost_example.cpp -o run


To be precise, I experienced the problem explained in this chapter of this fine book.
I knew I had boost installed (I was told to install it to compile another project). I tried to copy from the Makefile of that other project, but the following didn't work:

     g++ boost_example.cpp -o run -lbooster


     g++ boost_example.cpp -o run -lboost


Trying to make an educated guess from the #include line in the code, I tried the following to no avail:

     g++ boost_example.cpp -o run -lboost_filesystem


By that time, I had started to search the web frantically. I hate search engines because most of the time, you don't find what you're looking. I found the following but they didn't help but to waste my time:
Boost library link problem under kubuntu
C++/Boost linker errors

Since I am a RTFM kind of guy, I had actually checked the official documentation for the library I wanted to use: http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/index.htm but I didn't find any compile information.

At some stage, I got inspired to check what I had actually installed on my system:

$ locate boost_file
/usr/lib/libboost_filesystem-mt.a
/usr/lib/libboost_filesystem-mt.so
/usr/lib/libboost_filesystem-mt.so.1.38.0


Thus, I found the proper name of the library to link. The following worked:

     g++ boost_example.cpp -o run -lboost_filesystem-mt

Now, beside using (possibly) intelligent guess work and searching the web, is there a more intelligent way to find the name of the library to link to? I would never have guessed the library name boost_filesystem-mt given the header "boost/filesystem.hpp".

What's worse: boost_filesystem-mt is not mentioned anywhere on the official site! (I'm guessing it's distro/packaging dependent).

Again, I always make a point of RTFM before asking a question, and I found this chapter of the aforementioned book, so I checked what I could find on my system at /usr/lib/:

$ ls /usr/lib/boost
/usr/lib/libboost_date_time-mt.a           /usr/lib/libboost_prg_exec_monitor-mt.so.1.38.0
/usr/lib/libboost_date_time-mt.so          /usr/lib/libboost_program_options-mt.a
/usr/lib/libboost_date_time-mt.so.1.38.0   /usr/lib/libboost_program_options-mt.so
/usr/lib/libbooster.a                      /usr/lib/libboost_program_options-mt.so.1.38.0
/usr/lib/libbooster.so                     /usr/lib/libboost_python-mt.a
/usr/lib/libbooster.so.0                   /usr/lib/libboost_python-mt-py25.a
/usr/lib/libbooster.so.0.0.0               /usr/lib/libboost_python-mt-py25.so
/usr/lib/libboost_filesystem-mt.a          /usr/lib/libboost_python-mt-py25.so.1.38.0
/usr/lib/libboost_filesystem-mt.so         /usr/lib/libboost_python-mt-py26.a
/usr/lib/libboost_filesystem-mt.so.1.38.0  /usr/lib/libboost_python-mt-py26.so
/usr/lib/libboost_graph-mt.a               /usr/lib/libboost_python-mt-py26.so.1.38.0
/usr/lib/libboost_graph-mt.so              /usr/lib/libboost_python-mt.so
/usr/lib/libboost_graph-mt.so.1.38.0       /usr/lib/libboost_regex-mt.a
/usr/lib/libboost_iostreams-mt.a           /usr/lib/libboost_regex-mt.so
/usr/lib/libboost_iostreams-mt.so          /usr/lib/libboost_regex-mt.so.1.38.0
/usr/lib/libboost_iostreams-mt.so.1.38.0   /usr/lib/libboost_serialization-mt.a
/usr/lib/libboost_math_c99f-mt.a           /usr/lib/libboost_serialization-mt.so
/usr/lib/libboost_math_c99f-mt.so          /usr/lib/libboost_serialization-mt.so.1.38.0
/usr/lib/libboost_math_c99f-mt.so.1.38.0   /usr/lib/libboost_signals-mt.a
/usr/lib/libboost_math_c99l-mt.a           /usr/lib/libboost_signals-mt.so
/usr/lib/libboost_math_c99l-mt.so          /usr/lib/libboost_signals-mt.so.1.38.0
/usr/lib/libboost_math_c99l-mt.so.1.38.0   /usr/lib/libboost_system-mt.a
/usr/lib/libboost_math_c99-mt.a            /usr/lib/libboost_system-mt.so
/usr/lib/libboost_math_c99-mt.so           /usr/lib/libboost_system-mt.so.1.38.0
/usr/lib/libboost_math_c99-mt.so.1.38.0    /usr/lib/libboost_thread-mt.a
/usr/lib/libboost_math_tr1f-mt.a           /usr/lib/libboost_thread-mt.so
/usr/lib/libboost_math_tr1f-mt.so          /usr/lib/libboost_thread-mt.so.1.38.0
/usr/lib/libboost_math_tr1f-mt.so.1.38.0   /usr/lib/libboost_unit_test_framework-mt.a
/usr/lib/libboost_math_tr1l-mt.a           /usr/lib/libboost_unit_test_framework-mt.so
/usr/lib/libboost_math_tr1l-mt.so          /usr/lib/libboost_unit_test_framework-mt.so.1.38.0
/usr/lib/libboost_math_tr1l-mt.so.1.38.0   /usr/lib/libboost_wave-mt.a
/usr/lib/libboost_math_tr1-mt.a            /usr/lib/libboost_wave-mt.so
/usr/lib/libboost_math_tr1-mt.so           /usr/lib/libboost_wave-mt.so.1.38.0
/usr/lib/libboost_math_tr1-mt.so.1.38.0    /usr/lib/libboost_wserialization-mt.a
/usr/lib/libboost_prg_exec_monitor-mt.a    /usr/lib/libboost_wserialization-mt.so
/usr/lib/libboost_prg_exec_monitor-mt.so   /usr/lib/libboost_wserialization-mt.so.1.38.0

And now I am scratching my head very hard, because I really don't know how to correlate the above to the list of boost libraries.

I apologize for this long-winded example, but it perfectly illustrate my problem:

Beside guesswork, searching the web, asking other people, trial and error, etc... is there a more intelligent way to find the name of the library one should link to? Aren't those kind of things supposed to be documented somewhere?

I'm certainly interested in knowing the answer as far as boost is concerned, but my question is more generic and is applicable to any library.

Incidentally, I have a very similar question regarding the name of linux packages (be they .deb or .rpm). If I am told that to compile such or such software I need, say, PRCE or FooBar, how do I know precisely the name of the package to install? I know how to user apt-cache (for Kubuntu debs) but some package naming are not intuitive and I often end up installing packages I don't really need...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

满身野味 2024-09-21 09:02:15

我有时会使用一种强力方法,但您必须知道在哪些目录中查找所需的库(/lib、/usr/lib 和 /usr/local/lib 是通常的嫌疑对象)。我创建了一个名为“gnm”(它使用的两个实用程序“grep nm”的缩写)的 shell 脚本,其中包含以下内容。如果您创建这样的文本文件,请记住使其可执行(chmod +x gnm)。

#!/bin/sh
if [ $# -lt 2 ] ; then
  echo Usage: $0 pattern file[s]
  exit
fi
pattern=$1
shift
while [ $# -gt 0 ] ; do
  nm $1 | grep $pattern > /dev/null
  if [ $? -eq 0 ] ; then
    echo $1
  fi
  shift
done

当我搜索定义特定符号的库时,我发出类似以下的命令:

gnm symbol /usr/lib/*.a 

例如,您提到的源给了我以下链接错误:

boost_example.cpp:(.text+0x38): undefined reference to `boost::system::get_system_category()'
boost_example.cpp:(.text+0x44): undefined reference to `boost::system::get_generic_category()'
boost_example.cpp:(.text+0x50): undefined reference to `boost::system::get_generic_category()'
boost_example.cpp:(.text+0x5c): undefined reference to `boost::system::get_generic_category()'
boost_example.cpp:(.text+0x68): undefined reference to `boost::system::get_system_category()'

所以我使用命令:

gnm get_system_category /usr/lib/*.a

报告:

/usr/lib/libboost_filesystem.a
/usr/lib/libboost_system.a

尝试这些结果中的第一个出现同样的错误,但第二个有效:

g++ boost_example.cpp -lboost_system -o run

我不知道为什么我需要您需要文件系统的系统库;也许是不同版本的Boost。

There is a brute-force method I sometimes use, but you do have to know what directories to look in for the library you need (/lib, /usr/lib and /usr/local/lib are the usual suspects). I've created a shell script I call "gnm," (short for "grep nm," the two utilities it uses) with the below contents. If you create such a text file, remember to make it executable (chmod +x gnm).

#!/bin/sh
if [ $# -lt 2 ] ; then
  echo Usage: $0 pattern file[s]
  exit
fi
pattern=$1
shift
while [ $# -gt 0 ] ; do
  nm $1 | grep $pattern > /dev/null
  if [ $? -eq 0 ] ; then
    echo $1
  fi
  shift
done

When I'm searching for a library that defines a particular symbol, I issue a command something like:

gnm symbol /usr/lib/*.a 

For example, the source you mentioned gives me the following link errors:

boost_example.cpp:(.text+0x38): undefined reference to `boost::system::get_system_category()'
boost_example.cpp:(.text+0x44): undefined reference to `boost::system::get_generic_category()'
boost_example.cpp:(.text+0x50): undefined reference to `boost::system::get_generic_category()'
boost_example.cpp:(.text+0x5c): undefined reference to `boost::system::get_generic_category()'
boost_example.cpp:(.text+0x68): undefined reference to `boost::system::get_system_category()'

so I use the command:

gnm get_system_category /usr/lib/*.a

which reports:

/usr/lib/libboost_filesystem.a
/usr/lib/libboost_system.a

Trying the first of these results in the same errors, but the second one works:

g++ boost_example.cpp -lboost_system -o run

I don't know why I needed the system library where you needed filesystem; maybe different versions of Boost.

嘴硬脾气大 2024-09-21 09:02:15

我刚刚找到了(在我的系统上)最智能和最官方的方式来找出链接标志。
以下内容仅适用于 Debian 或 Debian 衍生发行版(如我的例子中的 Kubuntu)上的 boost。对于任何系统上的任何库,其他答复可能更通用。

根据安装的 boost 版本,您的 Debian 类发行版上可能有以下文件:
/usr/share/doc/libboost1.38-doc/README.Debian
其中一部分内容如下:

--------

The following table shows which components use a library (shared or
static) and the corresponding "-l" flag.  Note that only the
multithreaded version of the libraries is shipped.

  Component              Link Flag                    Library Type
  ---------              ---------                    ------------
  Boost.Date_Time        -lboost_date_time-mt           static  shared
  Boost.Filesystem       -lboost_filesystem-mt          static  shared
  Boost.Graph            -lboost_graph-mt               static  shared
  Boost.IOStreams        -lboost_iostreams-mt           static  shared
  Boost.Math             -lboost_math_c99-mt            static  shared
                         -lboost_math_c99f-mt           static  shared
                         -lboost_math_c99l-mt           static  shared
  Boost.MPI              -lboost_mpi-mt                 static  shared
  Boost.Program_options  -lboost_program_options-mt     static  shared
  Boost.Python           -lboost_python-mt-py24         static  shared
                         -lboost_python-mt-py25         static  shared
  Boost.Regex            -lboost_regex-mt               static  shared
  Boost.Serialization    -lboost_serialization-mt       static  shared
                         -lboost_wserialization-mt      static  shared
  Boost.Signals          -lboost_signals-mt             static  shared
  Boost.System           -lboost_system-mt              static  shared
  Boost.Test             -lboost_prg_exec_monitor-mt    static  shared
                         -lboost_unit_test_framework-mt static  shared
  Boost.Thread           -lboost_thread-mt              static  shared
  Boost.Wave             -lboost_wave-mt                static  shared


只需找到文档的正确位置即可!

I just found the most intelligent and most official way (on my system) to figure out the link flag.
What follows is only valid for boost on a Debian or Debian derivative distribution (like Kubuntu in my case). The other replies might be more generic for any library on any system.

Depending on the boost version installed, you may have the following file on your Debian-like distribution:
/usr/share/doc/libboost1.38-doc/README.Debian
part of which reads:

--------

The following table shows which components use a library (shared or
static) and the corresponding "-l" flag.  Note that only the
multithreaded version of the libraries is shipped.

  Component              Link Flag                    Library Type
  ---------              ---------                    ------------
  Boost.Date_Time        -lboost_date_time-mt           static  shared
  Boost.Filesystem       -lboost_filesystem-mt          static  shared
  Boost.Graph            -lboost_graph-mt               static  shared
  Boost.IOStreams        -lboost_iostreams-mt           static  shared
  Boost.Math             -lboost_math_c99-mt            static  shared
                         -lboost_math_c99f-mt           static  shared
                         -lboost_math_c99l-mt           static  shared
  Boost.MPI              -lboost_mpi-mt                 static  shared
  Boost.Program_options  -lboost_program_options-mt     static  shared
  Boost.Python           -lboost_python-mt-py24         static  shared
                         -lboost_python-mt-py25         static  shared
  Boost.Regex            -lboost_regex-mt               static  shared
  Boost.Serialization    -lboost_serialization-mt       static  shared
                         -lboost_wserialization-mt      static  shared
  Boost.Signals          -lboost_signals-mt             static  shared
  Boost.System           -lboost_system-mt              static  shared
  Boost.Test             -lboost_prg_exec_monitor-mt    static  shared
                         -lboost_unit_test_framework-mt static  shared
  Boost.Thread           -lboost_thread-mt              static  shared
  Boost.Wave             -lboost_wave-mt                static  shared


One only had to find the proper place for the documentation!

冷心人i 2024-09-21 09:02:15

http://www.boost.org/doc/libs /1_53_0/doc/html/bbv2/installation.html
在那里阅读有关安装的信息,一切都非常清楚。

http://www.boost.org/doc/libs/1_53_0/doc/html/bbv2/installation.html
Read about the installation there, all very clear.

若有似无的小暗淡 2024-09-21 09:02:15
  • 一种可能是检查 rpm/deb 包的文件列表。
    在 Ubuntu 系统上,这可以通过发出以下命令来完成:

dpkg -L libboost-文件系统-dev

  • 另一种可能性是使用 pkg-config。它是一个帮助确定某些库的编译标志的程序(尽管它不支持 Ubuntu 上的 boost)

  • 您可以使用 Autoconf - 可以找到一个检查 boost 的宏此处。使用 Autoconf 有助于保持源平台/发行版独立。

  • One possibility is to check the file list of the rpm/deb package.
    On a Ubuntu system this could be done by issuing the following command:

dpkg -L libboost-filesystem-dev

  • Another possibility is to use pkg-config. It is a program that helps determine the compile flags for certain libraries (eventhough it does not support boost on Ubuntu)

  • You could use Autoconf - a macro that checks for boost can be found here. Using Autoconf helps keeping your sources platform/distribution independent.

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