1

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?

2
  • 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. Commented Aug 3, 2011 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. Commented Aug 3, 2011 at 15:27

2 Answers 2

1

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. 
Sign up to request clarification or add additional context in comments.

3 Comments

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
aah yes..i had used greater than and less than symbols..thanks for pointing it out
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.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.