Take the 2-minute tour ×

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?

share|improve this question
If you're having a problem when linking with a library you should probably ask a specific question here like "why do I get linker errors when I do this...". Libraries are not incorporated into the compiler, the linker has an idea about what libraries to link by default and the default libraries tend to be installed in a path alongside the compiler, but the linking process is the same for both default and external libraries. They need to be in the correct format and the linker needs to know their path and name. –  tinman Aug 3 '11 at 15:18
@tinman: You are right, but i do face such outputs complaining of undefined reference to a lot of things, not only with one library. I just wanted to know if there is any attempt of that kind, which i still think someone should give it a try. –  Amani Aug 3 '11 at 15:27
add comment

2 Answers

up vote 1 down vote accepted

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"
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.
share|improve this answer
These options you can include in your IDE such as in eclipse. But you will have to specify them explicitly if you are compiling from command line. Experts, Correct me if I am wrong –  ArunMu Aug 3 '11 at 15:28
you messed up the -L option in your second point –  J T Aug 3 '11 at 15:29
aah yes..i had used greater than and less than symbols..thanks for pointing it out –  ArunMu Aug 3 '11 at 15:30
Does the -I option garantees that the subfolders in /path/to/boost_dir will be searched incase the include file is not found in the boost_dir? I ask this because often time the compiler complains of not finding a file even when you know its there. –  Amani Aug 3 '11 at 15:36
@Amani : Please check the edit. Hope it is clear now. –  ArunMu Aug 3 '11 at 15:43
add comment

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 :)

share|improve this answer
add comment

Your Answer


