外部 C++库链接问题

发布于 2024-11-27 20:43:57 字数 139 浏览 3 评论 0原文

在将我的 C++ 项目与外部库(例如 Boost、wxWidgets、Gtkmm)链接时,我时常遇到困难。有没有办法将这些外部库合并到编译器中(在我的例子中是 GNU G++,winXP SP3),以便编译器可以将它们作为其一部分,就像使用 C++ STL 一样?

I have been facing difficulties, now and then, linking my C++ projects with external libraries (e.g. Boost, wxWidgets, Gtkmm). Is there a way to incorporate these external libraries into the compiler (GNU G++ in my case, winXP SP3) so that the compiler can take them as part of it just like with C++ STL?

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

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

发布评论

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

评论(2

看春风乍起 2024-12-04 20:43:57

链接库(以 Boost Libs 和 g++ 编译器为例):

使用正确的包含文件编译源代码

1) g++ -I /path/to/boost_dir -c code.cpp

2) g++ -L/path/to/your/boost/shared/libs -lboost_regex -o 可执行代码.o

对于链接部分,我以 boost 正则表达式库为例

A full example ::
1) Consider your boost directory is at /usr/include/boost.
2)within this we have multiple header files and directories, So if you want to use the lambda functionality of boost, then include it in your code as below::

#include< boost/lambda.hpp >
#include< boost/regex >
using namespace boost::lambda;

3) Compile as "g++ -I /usr/include -c code.cpp"
Then 
4) g++ -L /usr/lib -lboost_regex -o executable code.o

I have assumed that the boost shared objects are present at /usr/lib path.

To link with a library (Taking example of Boost Libs and g++ compiler):

Compiling the source with correct include files

1) g++ -I /path/to/boost_dir -c code.cpp

2) g++ -L/path/to/your/boost/shared/libs -lboost_regex -o executable code.o

For the linking part I have taken example of boost regex library

A full example ::
1) Consider your boost directory is at /usr/include/boost.
2)within this we have multiple header files and directories, So if you want to use the lambda functionality of boost, then include it in your code as below::

#include< boost/lambda.hpp >
#include< boost/regex >
using namespace boost::lambda;

3) Compile as "g++ -I /usr/include -c code.cpp"
Then 
4) g++ -L /usr/lib -lboost_regex -o executable code.o

I have assumed that the boost shared objects are present at /usr/lib path.
甜警司 2024-12-04 20:43:57

据我所知,这样做的方法是告诉编译器在哪里搜索库(-L)以及它们的名称是什么(-l)。

但我不是 gcc 专家,我不知道如何破解/配置编译器,以始终假设它们必须链接到除 C 标准库和 STL 之外的某些库(使用 g++ 时)。

很高兴知道您是否可以做到这一点(以及如何做到),因为知识就是力量:)

As far as I know the way to do that is telling the compiler where to search for libraries (-L) and what their names are (-l).

But I'm not a gcc expert and I'm unaware of ways to hack/configure the compiler to always assume they have to link to certain libraries besides C standard library and STL (when using g++).

Would be nice to know if you can do that (and how) because, well, knowledge is power :)

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