0

I am running Ubuntu 16.04. I have a C++ source file second.cpp which looks like this:

#include <iostream>
#include <boost/filesystem/operations.hpp>

namespace bfs = boost::filesystem;

int main(){
    bfs::path p("second.cpp");
    if (bfs::exists(p))
        std::cout << p.leaf() << std::endl;
}

When I run $ g++ -o second second.cpp, I get the following:

/tmp/ccyWlRx6.o: In function boost::filesystem::path::leaf() const': second.cpp:(.text._ZNK5boost10filesystem4path4leafEv[_ZNK5boost10filesystem4path4leafEv]+0x2e): undefined reference toboost::filesystem::path::filename() const' /tmp/ccyWlRx6.o: In function boost::filesystem::exists(boost::filesystem::path const&)': second.cpp:(.text._ZN5boost10filesystem6existsERKNS0_4pathE[_ZN5boost10filesystem6existsERKNS0_4pathE]+0x2f): undefined reference toboost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)' collect2: error: ld returned 1 exit status

However, if I run g++ -o second second.cpp -lboost_filesystem, the code compiles with no errors. libboost_filesystem.a, libboost_filesystem.so, and libboost_filesystem.so.1.67.0 are all in /usr/local/lib.

/etc/ld.so.conf looks like this:

include /etc/ld.so.conf.d/*.conf
/usr/local/lib

and I have run sudo ldconfig.

Is this the normal behavior? From what I have read, it seemed to me like the files in /usr/local/lib should automatically be included by the linker. If this is not the case, is there I way that I can achieve this? Thank you for the help!

1
  • 2
    The loader configuration has nothing to do with the linker. Commented Jun 24, 2018 at 6:37

3 Answers 3

3

Is this the normal behavior?

Of course it is. If you do not tell the compiler driver with -l boost_filesystem to even look for the library to link with it, it will not do so and your program which uses the library will not link, as you have seen. Your compiler is not magic. If your program needs to link in a library other than the C/C++/GCC runtime libraries that the compiler knows about by default, you need to tell the compiler about the library.

The title of your question is a falsehood. As you have observed …

I run g++ -o second second.cpp -lboost_filesystem, the code compiles with no errors.

… g++ does find the library in /usr/local/lib: once you do things properly and actually tell it the name of the library to look for.

Also note that discussion of -L is a red herring for the same reason. You didn't use -L and yet g++ found the library anyway; so there is obviously no problem with the directory containing the library not being in the compiler's default library search path.

Since you have not even attempted to run the program, according to your question, whether the dynamic loader finds that library there is unknown. But it probably will.

3
  • Maybe my question was unclear: Is there a way for g++ to find library without passing passing -l or must this be done each time I compile a new program using the library? I should have mentioned that the program does run (although the if block was causing a segmentation fault).
    – lboyd
    Commented Jun 24, 2018 at 14:30
  • As I mentioned in my answer, you need to specify the Linker search path whenever you like to link from a nonstandard directory.
    – schily
    Commented Jun 24, 2018 at 16:43
  • Okay. What exactly does standard mean here? Something like <iostream>?
    – lboyd
    Commented Jun 24, 2018 at 18:45
-1

If you like to let the linker find a library at an unusual location, you need to use a -L option to specify the directory, the library is in.

If you like the runtime linker to find the same library at runtime, use the -R for the linker to add a RUNPATH tag to the binary.

ldconfig was used in the 1980s with the a.out format because it did not have the RUNPATH property that was added to ELF.

-1

I assume it seems you need to create a configuration file under the /etc/ld.so.conf.d
instead of adding the entry into /etc/ld.so.conf.

/etc/ld.so.conf reads all configuration files from /etc/ld.so.conf.d
directory which contains configuration files of library path to library files.
The configuration file name is not defined by system, so you can name it
whatever you want like locallib.conf for example.
So, somthing like this,

/etc/ld.so.conf.d/locallib.conf

Contains
/usr/local/lib

Then after run ldconfig.

Another way.
This is only enabled local environment(not system global).
Adding an entry of library path to your .bashrc.

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LD_LIBRARY_PATH

Enabling the environment variables by
. ./.bashrc.
Please don't forget to check out the variables with followed command.

echo $LD_LIBRARY_PATH
1
  • 1
    The questioner has already stated that the program both compiled and ran without doing any of that.
    – JdeBP
    Commented Jun 26, 2018 at 6:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .