2

From my terminal session:

Go Trojans >make all
g++ -static -I/usr/include/boost -I/usr/include/boost/filesystem get_sys_info.cpp
/tmp/cc6nK9EV.o: In function `__static_initialization_and_destruction_0(int, int)':
get_sys_info.cpp:(.text+0x13a): undefined reference to `boost::system::generic_category()'
get_sys_info.cpp:(.text+0x146): undefined reference to `boost::system::generic_category()'
get_sys_info.cpp:(.text+0x152): undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
Go Trojans >

The C++ code importing the Boost C++ Filesystem library:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

// Include Boost C++ libraries
#include <boost/filesystem.hpp>
using namespace boost::filesystem;


using namespace std;

int main() {
    string my_str = "This is a string.";

    cout << my_str << endl;
/*
    my_str = system("pwd");
    my_str.append("\b\b\b\b\b\b\b\b extra");
    cout << my_str << "a\b\b\b\b\b\b=" << endl;
*/

    path p(".");
    cout << p << "==" << endl;



    return 0;
}

Snippets from the Terminal session at the directory where my Boost C++ libraries are located.

Go Trojans >pwd
/usr/include/boost
Go Trojans >ls -al
total 1308
drwxr-xr-x  86 root root 12288 Jan 29 09:30 .
drwxr-xr-x 119 root root 20480 Feb  4 08:08 ..
...
drwxr-xr-x   5 root root  4096 Jan 29 09:30 filesystem
-rw-r--r--   1 root root  1340 Jan  5  2012 filesystem.hpp

How do I resolve the undefined references? Am I importing the Boost C++ Filesystem Library correctly? Am I also compiling the code correctly?

What are my mistakes? Can you please kindly help me?

Thank you very much, and have an awesome day! Ciao!

Giovanni
  • 101
  • 3
  • 13
  • Simonc, I used the "-lboost_filesystem -lboost_system" sequence to link the aforementioned Boost C++ libraries. However, I still have linking errors. – Giovanni Feb 08 '13 at 16:04
  • Go Bears >make all g++ -L/usr/lib -lboost_filesystem -lboost_system get_sys_info.cpp /tmp/cchfM9Y3.o: In function `__static_initialization_and_destruction_0(int, int)': get_sys_info.cpp:(.text+0x13f): undefined reference to `boost::system::generic_category()' get_sys_info.cpp:(.text+0x14b): undefined reference to `boost::system::generic_category()' get_sys_info.cpp:(.text+0x157): undefined reference to `boost::system::system_category()' – Giovanni Feb 08 '13 at 16:05
  • /tmp/cchfM9Y3.o: In function `boost::filesystem3::path::codecvt()': get_sys_info.cpp:(.text._ZN5boost11filesystem34path7codecvtEv[boost::filesystem3::path::codecvt()]+0x5): undefined reference to `boost::filesystem3::path::wchar_t_codecvt_facet()' collect2: ld returned 1 exit status make: *** [all] Error 1 Go Bears > – Giovanni Feb 08 '13 at 16:06
  • Did I fail to include an appropriate header file in my C++ code, or did I fail to include another Boost C++ Library? How can I tell what is wrong, where did I go wrong, and figure out how can I resolve this? Can you please kindly share your wisdom and insights with me? Thank you very much, and have an awesome day! – Giovanni Feb 08 '13 at 16:07
  • I, the C++ dingbat, finally managed to figure it out. I had to use: g++ get_sys_info.cpp -L/usr/lib -lboost_filesystem -lboost_system Thank you all so much for helping me... I appreciate it. – Giovanni Feb 08 '13 at 16:28

1 Answers1

3

You need to use the -L/path/to/your/library in front of the call to -lboost_system which will tell the compiler where to find the shared object. However, even if you can compile the code, it will not run because the runtime cannot locate the file anyway, so you're forced to update your path.

I'm assuming you're on a school computer so editing the LD_LIBRARY_PATH or utilizing /sbin/ldconfig are illegal.

If they're not illegal, you can either

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/boost

in your ~/.bashrc or equivalent

or you can

touch /etc/ld.so.conf.d/boost.conf

vi /etc/ld.so.conf/boost.conf

put the path to your boost library in here and then save the file. Then run:

/sbin/ldconfig

Which reparses everything in /etc/ld.so.conf.d and will update your runtime paths.

Good luck!

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
  • From the Terminal: `Go Trojans >export` `LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/include/boost` `Go Trojans >echo $LD_LIBRARY_PATH` `:/usr/include/boost` `Go Trojans >make all` `export LD_LIBRARY_PATH=D_LIBRARY_PATH:/usr/include/boost` `g++ -L/usr/include/boost -lboost_system get_sys_info.cpp` `/usr/bin/ld: cannot find -lboost_system` `collect2: ld returned 1 exit status` `make: *** [all] Error 1` `Go Trojans >` I tried exporting the $LD_LIBRARY_PATH in the shell environment, and it didn't work. – Giovanni Feb 06 '13 at 15:06
  • From my makefile: `all:` ` export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/include/boost` ` g++ -L/usr/include/boost -lboost_system get_sys_info.cpp` ` ./a.out` – Giovanni Feb 06 '13 at 15:08
  • Are your libraries in `/usr/include/boost`? Typically only headers are included in `/usr/include/boost`, while the libraries you want are probably in somewhere like `/usr/lib/boost`. You need to tell your compiler where to find the _libraries_ – Tyler Jandreau Feb 06 '13 at 15:23
  • From my terminal `-rw-r--r-- 1 root root 122280 Jan 2 2012 libboost_filesystem.so.1.46.1` `-rw-r--r-- 1 root root 324192 Jan 2 2012 libboost_graph.so.1.46.1` `-rw-r--r-- 1 root root 101592 Jan 2 2012 libboost_iostreams.so.1.46.1` `-rw-r--r-- 1 root root 414280 Jan 2 2012 libboost_program_options.so.1.46.1` `-rw-r--r-- 1 root root 311168 Jan 2 2012 libboost_python-py27.so.1.46.1` `-rw-r--r-- 1 root root 307072 Jan 2 2012 libboost_python-py32.so.1.46.1` `-rw-r--r-- 1 root root 1054456 Jan 2 2012 libboost_regex.so.1.46.1` – Giovanni Feb 06 '13 at 15:54
  • `-rw-r--r-- 1 root root 446584 Jan 2 2012 libboost_serialization.so.1.46.1` `-rw-r--r-- 1 root root 80600 Jan 2 2012 libboost_signals.so.1.46.1` `-rw-r--r-- 1 root root 14568 Jan 2 2012 libboost_system.so.1.46.1` `-rw-r--r-- 1 root root 101304 Jan 2 2012 libboost_thread.so.1.46.1` `-rw-r--r-- 1 root root 323128 Jan 2 2012 libboost_wserialization.so.1.46.1` `Go Trojans >pwd` `/usr/lib` `Go Trojans >` I still have compilation errors. – Giovanni Feb 06 '13 at 15:56
  • `Go Trojans >make all` `export LD_LIBRARY_PATH=/usr/lib/libboost_system.so.1.46.1` `g++ -L/usr/lib/libboost_system.so.1.46.1 -L/libboost_filesystem.so.1.46.1 -lboost_system get_sys_info.cpp` `/usr/bin/ld: cannot find -lboost_system` `collect2: ld returned 1 exit status` `make: *** [all] Error 1` `Go Trojans >` So, can you please kindly tell me where and what is my error? Thank you very much. Oh, thank you so much for pointing out the difference between the location of the header files and the library files. That advice/question is awesome. – Giovanni Feb 06 '13 at 15:57
  • Ok, I see your problem. The `-L` flag is for a _path_ only. The `-l` is for the library. When including libraries, you'll want to do something like this: `-L/usr/lib -lboost_system`. You do not need to include the `lib` prefix or `.so.1.46.` whatever suffix. – Tyler Jandreau Feb 06 '13 at 16:03
  • `Go Trojans >make all` `g++ -L/usr/lib -lboost_system get_sys_info.cpp` `/usr/bin/ld: cannot find -lboost_system` `collect2: ld returned 1 exit status`0 `make: *** [all] Error 1` `Go Trojans >` `all:` ` g++ -L/usr/lib -lboost_system get_sys_info.cpp` ` ./a.out` I am sorry for bothering you yet another time, but I cannot get it to work. Do you know why? Thank you for taking some of your precious time for helping me. – Giovanni Feb 06 '13 at 16:17
  • Show me your makefile. I'll edit it and post it. – Tyler Jandreau Feb 06 '13 at 16:25
  • all: g++ -L/usr/lib -lboost_system get_sys_info.cpp ./a.out Is there a way in which I can attach a file? – Giovanni Feb 06 '13 at 16:32
  • Just do the `g++ -L/usr/lib -lboost_system get_sys_info.cpp` from command line and show me the output. – Tyler Jandreau Feb 06 '13 at 16:34
  • `Go Trojans >g++ -L/usr/lib -lboost_system get_sys_info.cpp` `/usr/bin/ld: cannot find -lboost_system` `collect2: ld returned 1 exit status` `Go Trojans >` I am sorry for the late response. I fell asleep. I have pasted the output from the Terminal above. Once again, thank you for taking some of your precious time to help me. – Giovanni Feb 06 '13 at 20:46