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!
You need to use the
-L/path/to/your/libraryin front of the call to-lboost_systemwhich 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_PATHor utilizing/sbin/ldconfigare illegal.If they’re not illegal, you can either
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/boostin your
~/.bashrcor equivalentor you can
touch /etc/ld.so.conf.d/boost.confvi /etc/ld.so.conf/boost.confput the path to your boost library in here and then save the file. Then run:
/sbin/ldconfigWhich reparses everything in
/etc/ld.so.conf.dand will update your runtime paths.Good luck!