Question

I'm trying to use boost::filesystem::exists function. When I'm trying to link, I'm getting

/usr/local/include/boost/filesystem/operations.hpp:289: undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'

error.

I googled for a while and found only "link-with-boost" answers. In my case -lboost_system and -lboost_filesystem specified for linker, but it doesn't seem to work. Actually I can use boost::filesytem::path (for example), but when I'm trying to use anything, which needs boost/filesystem.hpp header, I'm getting linker errors. Any ideas?

P.S. I'm using gcc-4.6.4 and boost lib installed from repos, but I assume gcc-4.6.4 is default gcc version for my ubuntu 12.04. I guess I don't need to compile boost from source?

Was it helpful?

Solution

There are many options to the linker (ld) to specify the search path to resolve shared libraries, man ld will give you all the options. Suppose you have boost installed in /usr/local/lib, you could add one of these options to gcc to pass along to the linker:

  • -L=/usr/local/lib

    Directories specified on the command line are searched before the default directories. All -L options apply to all -l options, regardless of the order in which the options appear. If searchdir begins with "=", then the "=" will be replaced by the sysroot prefix, a path specified when the linker is configured. The -L option only sets a compile-time library search path; if you want a shared library to be found at runtime then its directory must be known at runtime.

  • -Wl,-rpath,/usr/local/lib

    Add a directory to the runtime library search path. This is used when linking an ELF executable with shared objects. All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at runtime. The -rpath option is also used when locating shared objects which are needed by shared objects explicitly included in the link; see the description of the -rpath-link option. If -rpath is not used when linking an ELF executable, the contents of the environment variable "LD_RUN_PATH" will be used if it is defined.

Another alternative is to add to your LD_LIBRARY_PATH the location of your boost libraries.

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

The linker (ld) uses LD_LIBRARY_PATH as one of the search paths to locate required shared libraries.

You can read more about the linker and shared libraries here.

To fully understand why your installation isn't finding the boost libraries by default you might find this answer at stackexchange informative.

This SO answer suggests using boost m4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top