Search code examples
c++linuxboostboost-filesystem

how to convert a string into a directory? boost on Linux


I am having trouble converting a path into a directory on Linux using a boost. below is my code, this directory exists in my folder but somehow not recognised as directory as it always print out PATH DOES NOT EXISTS

i am guessing this is a linux problem because the same code works fine on windows visual studio 2015

#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
int main() 
{
    boost::filesystem::path p ("/home/dj/tut");
    cout <<p.generic_string()<<endl;
    if (boost::filesystem::is_directory(p)) { cout << "PATH EXISTS " << endl; }else { cout << "PATH DOES NOT EXISTS" << endl; }
}

how i compile with

g++ -I /home/dj/boost_1_65_1/boost script.cpp -o test -std=c++11 -lboost_filesystem -lboost_system

and then do the following to run:

./test

with ldd test i get :

linux-vdso.so.1 => (0x00007ffc8cdb9000) 
libboost_filesystem.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.58.0 (0x00007fef36573000)
libboost_system.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0 (0x00007fef3636f000) 
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fef35fed000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fef35dd7000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fef35a0d000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fef357f0000) 
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fef354e7000) /lib64/ld-linux-x86-64.so.2 (0x00007fef3678b000) 

Solution

  • Here you specify the include file search path only, without -L to have matching libraries linked:

    g++ -I /home/dj/boost_1_65_1/boost script.cpp -o test -std=c++11 -lboost_filesystem -lboost_system
    

    If that compiles, then linker and runtime is using Ubuntu's boost libraries. Now I have no idea how this could produce the effect you get, but funny things can happen when you mix versions like this.

    Try compiling something like this (fix paths as needed) to make linker search your own version of the libs:

    g++ -I /home/dj/boost_1_65_1/boost script.cpp -o test -std=c++11 -L/home/dj/boost_1_65_1/boost -lboost_filesystem -lboost_system
    

    Then to use correct library at run time, you can use this (note, single command line):

    LD_LIBRARY_PATH=/home/dj/boost_1_65_1/boost ./test
    

    Or something like that, you get the idea I hope.