外部C ++库链接问题

时间:2011-08-03 15:08:19

标签: c++ libraries

我一直面临着困难,偶尔将我的C ++项目与外部库(例如Boost,wxWidgets,Gtkmm)联系起来。有没有办法将这些外部库合并到编译器(在我的情况下是GNX G ++,winXP SP3)中,以便编译器可以将它们作为它的一部分,就像使用C ++ STL一样?

2 个答案:

答案 0 :(得分:1)

链接库(以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 executable code.o

对于链接部分,我已经举了boost regex库的例子

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.

答案 1 :(得分:0)

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

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

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

相关问题