How to use C++ Boost library with pkg-config? - c++

I successfully compiled and installed the latest version of the Boost library onto my linux machine.
Now, I would like to be able to use pkg-config to ease the process of providing linking paremeters with GCC.
Since I am too lazy for hand-coding my own .pc file, is there a script/tool which would automatically generate the needed .pc file or in some other way update pkg-config with boost flags?
(If someone already has that .pc file, a share would be welcome as well.)

What you're looking for seems to be a bit complicated, and a long-requested feature, as indicated in this 3 year old post https://svn.boost.org/trac/boost/ticket/1094 on Boost's trac. Reading through it shows that the feature was repeatedly postponed and never implemented (as of 1.4.3). The cause of the inability to generate a .pc file usable by pkg-config happens to do with boost's inconsistency in naming their library versions / build variants.
FWIW, an alternative for "automating" your building process is to use autotools (autoconf/automake). There's a link that might be of use to you (which I can't post because SO thinks I'm a spammer instead of a newcomer!), just google "tsuna boost m4 github" and it should take you there :)

Was facing a similar issue with boost. Wrote simple python script to generate a .pc file. Saved me the pain of having write all the linker commands. I've posted it on https://github.com/nmante/pkg-config-generator.
Essentially, you give the script a directory where the library files are (.so, .a, .dylib files) and it will generate the linker commands (e.g. -lboost_graph). Feel free to tweak and fork to your needs.
Here's a sample boost.pc file I generated for my machine (Mac OS X). You can tweak it manually, or you can use my github program to generate it on your machine.
# Package Information for pkg-config
prefix=/usr/local/Cellar/boost/1.60.0_2
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/boost
includedir_new=${prefix}/include
Name: Boost
Description: Boost is awesome
Version: 1.60.0
Libs: -L${exec_prefix}/lib -lboost_prg_exec_monitor-mt
-lboost_math_c99f-mt -lboost_unit_test_framework-mt
-lboost_container-mt -lboost_log_setup -lboost_math_tr1l
-lboost_graph-mt -lboost_wserialization-mt -lboost_log-mt
-lboost_math_c99f -lboost_type_erasure -lboost_signals-mt
-lboost_test_exec_monitor -lboost_filesystem -lboost_thread-mt
-lboost_math_tr1f-mt -lboost_date_time -lboost_timer
-lboost_math_tr1f -lboost_test_exec_monitor-mt -lboost_container
-lboost_math_tr1 -lboost_type_erasure-mt
-lboost_program_options-mt -lboost_graph -lboost_log_setup-mt
-lboost_random -lboost_system -lboost_system-mt -lboost_locale-mt
-lboost_wserialization -lboost_regex -lboost_exception
-lboost_timer-mt -lboost_signals -lboost_filesystem-mt
-lboost_math_c99-mt -lboost_math_tr1-mt -lboost_serialization-mt
-lboost_serialization -lboost_prg_exec_monitor -lboost_exception-mt
-lboost_coroutine -lboost_math_c99 -lboost_iostreams-mt
-lboost_random-mt -lboost_program_options -lboost_atomic-mt
-lboost_date_time-mt -lboost_math_c99l -lboost_math_tr1l-mt
-lboost_context-mt -lboost_regex-mt -lboost_coroutine-mt
-lboost_log -lboost_chrono-mt -lboost_wave-mt
-lboost_iostreams -lboost_chrono -lboost_unit_test_framework
-lboost_math_c99l-mt
Cflags: -I${includedir_old} -I${includedir_new}

Related

Using c++ library on linux [duplicate]

This question already has answers here:
How to install c++ library on linux
(2 answers)
Closed 4 years ago.
I'm new to c++ and don't understand how to install a library on Linux (Mint). I want to use the GNU GMP library:https://en.wikipedia.org/wiki/GNU_Multiple_Precision_Arithmetic_Library
I downloaded the tar.lz file and installed it with
./configure
make
sudo make install
If I try to compile it, I get the error message that the header file "gmpxx.h" wasn't found. Where can I find this file? How do I compile it with the -lgmpxx -lgmp flags? I tried something like:
g++ test.cpp -o test -lgmpxx -lgmp
If the library is using the Autoconf system (which your does) then the default installation prefix is /usr/local.
That means libraries are installed in /usr/local/lib, and header files in /usr/local/include. Unfortunately few Linux systems have those added for the compiler to search by default, you need to explicitly tell the compiler to do it.
Telling the compiler to add a header-file path is done using the -I (upper-case i) option. For libraries the option is -L.
Like so:
g++ test.cpp -I/usr/local/include -L/usr/local/lib -lgmpxx -lgmp
The above command will allow your program to build, but it's unfortunately not enough as you most likely won't be able to run the program you just built. That's because the run-time linker and program loader doesn't know the path to the (dynamic) libraries either. You need to add another linker-specific flag -rpath telling the build-time linker to embed the path inside your finished program. The front-end program g++ doesn't know this option, so you need to use -Wl,-rpath:
g++ test.cpp -I/usr/local/include -L/usr/local/lib -lgmpxx -lgmp -Wl,-rpath=/usr/local/lib
The options can be found in the GCC documentation (for the -I and -L and -Wl options), and the documentation for ld (the compile-time linker) for the -rpath option.
If you install a lot of custom-build libraries, you might add the path /usr/local/lib to the file /etc/ld.so.conf and then run the ldconfig command (as root). Then you don't need the -rpath option.
Now with all of that said, almost all libraries you would usually use for development will be available in your distributions standard repository. If you use them the libraries will be installed with paths that means you don't have to add flags.
So I recommend you install your distributions development packages for the libraries instead.

Boost library issues

I am trying to compile a simple c++ program using g++ which contains boost includes but i am getting the following error. I installed it from the tar file found in the boost site. I get the following error only with the asio library.
$ g++ -std=c++03 -Wall -pedantic -g -O2 tcp.cpp -lboost_system -lboost_date_time -lboost_thread -lboost_asio
/usr/bin/ld: cannot find -lboost_asio
collect2: error: ld returned 1 exit status
Boost Asio is header only.
Drop -lboost_asio
ASIO doesn't have a library built for it, so indicating -lboost_asio as an option doesn't really make sense.
However, ASIO does have a dependency on boost.system, which does require a library be built for it.
Make sure you've properly built the boost library in the first place (correct optimization flags, correct Runtime-Library linking, correct architecture, and so on), and that the generated library files (the .so files, in your case) are accessible to the executable.

Need some help figuring out compile-time error: 'Undefined symbols for architecture x86_64: "boost::system::system_category()"' [duplicate]

I'm trying to compile a program on Ubuntu 11.10 that uses the Boost libraries. I have the 1.46-dev Boost libraries from the Ubuntu Repository installed, but I get an error when compiling the program.
undefined reference to boost::system::system_category()
What is it that I do wrong?
The boost library you are using depends on the boost_system library. (Not all of them do.)
Assuming you use gcc, try adding -lboost_system to your compiler command line in order to link against that library.
Linking with a library that defines the missing symbol (-lboost_system) is the obvious solution, but in the particular case of Boost.System, a misfeature in the original design makes it use boost::system::generic_category() and boost::system::system_category() needlessly. Compiling with the flag -DBOOST_SYSTEM_NO_DEPRECATED disables that code and lets a number of programs compile without requiring -lboost_system (that link is of course still needed if you explicitly use some of the library's features).
Starting from Boost 1.66 and this commit, this behavior is now the default, so hopefully fewer and fewer users should need this answer.
As noticed by #AndrewMarshall, an alternative is to define BOOST_ERROR_CODE_HEADER_ONLY which enables a header-only version of the code. This was discouraged by Boost as it can break some functionality. However, since 1.69, header-only seems to have become the default, supposedly making this question obsolete.
Another workaround for those who don't need the entire shebang: use the switch
-DBOOST_ERROR_CODE_HEADER_ONLY.
If you use CMake, it's add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY).
The above error is a linker error... the linker a program that takes one or more objects generated by a compiler and combines them into a single executable program.
You must add -lboost_system to you linker flags which indicates to the linker that it must look for symbols like boost::system::system_category() in the library libboost_system.so.
If you have main.cpp, either:
g++ main.cpp -o main -lboost_system
OR
g++ -c -o main.o main.cpp
g++ main.o -lboost_system
When using CMAKE and find_package, make sure it is :
find_package(Boost COMPONENTS system ...)
and not
find_package(boost COMPONENTS system ...)
Some people may have lost hours for that ...
I got the same Problem:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-1_47 -lboost_filesystem-mgw45-mt-1_47
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj:main_filesystem.cpp:(.text+0x54):
undefined reference to `boost::system::generic_category()
Solution was to use the debug-version of the system-lib:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-d-1_47 -lboost_filesystem-mgw45-mt-1_47
But why?
When I had this, problem, the cause was the ordering of the libraries. To fix it, I put libboost_system last:
g++ mingw/timer1.o -o mingw/timer1.exe -L/usr/local/boost_1_61_0/stage/lib \
-lboost_timer-mgw53-mt-1_61 \
-lboost_chrono-mgw53-mt-1_61 \
-lboost_system-mgw53-mt-1_61
This was on mingw with gcc 5.3 and boost 1.61.0 with a simple timer example.
in my case, adding -lboost_system was not enough, it still could not find it in my custom build environment. I had to use the advice at Get rid of "gcc - /usr/bin/ld: warning lib not found" and change my ./configure command to:
./configure CXXFLAGS="-I$HOME/include" LDFLAGS="-L$HOME/lib -Wl,-rpath-link,$HOME/lib" --with-boost-libdir=$HOME/lib --prefix=$HOME
for more details see Boost 1.51 : "error: could not link against boost_thread !"
...and in case you wanted to link your main statically, in your Jamfile add the following to requirements:
<link>static
<library>/boost/system//boost_system
and perhaps also:
<linkflags>-static-libgcc
<linkflags>-static-libstdc++

undefined reference to boost::system::system_category() when compiling

I'm trying to compile a program on Ubuntu 11.10 that uses the Boost libraries. I have the 1.46-dev Boost libraries from the Ubuntu Repository installed, but I get an error when compiling the program.
undefined reference to boost::system::system_category()
What is it that I do wrong?
The boost library you are using depends on the boost_system library. (Not all of them do.)
Assuming you use gcc, try adding -lboost_system to your compiler command line in order to link against that library.
Linking with a library that defines the missing symbol (-lboost_system) is the obvious solution, but in the particular case of Boost.System, a misfeature in the original design makes it use boost::system::generic_category() and boost::system::system_category() needlessly. Compiling with the flag -DBOOST_SYSTEM_NO_DEPRECATED disables that code and lets a number of programs compile without requiring -lboost_system (that link is of course still needed if you explicitly use some of the library's features).
Starting from Boost 1.66 and this commit, this behavior is now the default, so hopefully fewer and fewer users should need this answer.
As noticed by #AndrewMarshall, an alternative is to define BOOST_ERROR_CODE_HEADER_ONLY which enables a header-only version of the code. This was discouraged by Boost as it can break some functionality. However, since 1.69, header-only seems to have become the default, supposedly making this question obsolete.
Another workaround for those who don't need the entire shebang: use the switch
-DBOOST_ERROR_CODE_HEADER_ONLY.
If you use CMake, it's add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY).
The above error is a linker error... the linker a program that takes one or more objects generated by a compiler and combines them into a single executable program.
You must add -lboost_system to you linker flags which indicates to the linker that it must look for symbols like boost::system::system_category() in the library libboost_system.so.
If you have main.cpp, either:
g++ main.cpp -o main -lboost_system
OR
g++ -c -o main.o main.cpp
g++ main.o -lboost_system
When using CMAKE and find_package, make sure it is :
find_package(Boost COMPONENTS system ...)
and not
find_package(boost COMPONENTS system ...)
Some people may have lost hours for that ...
I got the same Problem:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-1_47 -lboost_filesystem-mgw45-mt-1_47
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj:main_filesystem.cpp:(.text+0x54):
undefined reference to `boost::system::generic_category()
Solution was to use the debug-version of the system-lib:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-d-1_47 -lboost_filesystem-mgw45-mt-1_47
But why?
When I had this, problem, the cause was the ordering of the libraries. To fix it, I put libboost_system last:
g++ mingw/timer1.o -o mingw/timer1.exe -L/usr/local/boost_1_61_0/stage/lib \
-lboost_timer-mgw53-mt-1_61 \
-lboost_chrono-mgw53-mt-1_61 \
-lboost_system-mgw53-mt-1_61
This was on mingw with gcc 5.3 and boost 1.61.0 with a simple timer example.
in my case, adding -lboost_system was not enough, it still could not find it in my custom build environment. I had to use the advice at Get rid of "gcc - /usr/bin/ld: warning lib not found" and change my ./configure command to:
./configure CXXFLAGS="-I$HOME/include" LDFLAGS="-L$HOME/lib -Wl,-rpath-link,$HOME/lib" --with-boost-libdir=$HOME/lib --prefix=$HOME
for more details see Boost 1.51 : "error: could not link against boost_thread !"
...and in case you wanted to link your main statically, in your Jamfile add the following to requirements:
<link>static
<library>/boost/system//boost_system
and perhaps also:
<linkflags>-static-libgcc
<linkflags>-static-libstdc++

Linking a static library into Boost Python (shared library) - Import Error

I am building a Boost Python module (.so shared library file) which depends on another external library (STXXL)
While I can build and import the example Boost Python modules, I run into problems when STXXL is thrown into the mix. Specifically when running import fast_parts in python
I get ImportError: ./fast_parts.so: undefined symbol: _ZN5stxxl10ran32StateE
This says to me that the STXXL library isn't being linked, but I am not sure how that could be as I am linking against it and the linker isn't giving me any errors. It's worth noting I can successfully build and run standalone programs using STXXL and as far as I know the libraries are stored in a .a archive in the lib directory shown below. I have reduced my Makefile down to a single command as follows:
g++ -I/home/zenna/Downloads/stxxl-1.3.0/include -include stxxl/bits/defines.h -I/home/zenna/local/include -I/usr/include/python2.6 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -O3 -Wall -g -DFOO=BAR -pthread -L/home/zenna/Downloads/stxxl-1.3.0/lib/ -lstxxl -L/home/zenna/local/lib/ -lboost_python -lpython2.6 -fPIC -shared -o fast_parts.so partition.cpp
Any advice?
I'm assuming Linux, please comment if this is incorrect. What does the ldd output for libfast_parts.so look like? Does it indicate libstxxl.so is not found?
You might need to add /home/zenna/Downloads/stxxl-1.3.0/lib/ in your LD_LIBRARY_PATH or the rpath for libfast_parts.so.
-Wl,-rpath,/home/zenna/Downloads/stxxl-1.3.0/lib -L/home/zenna/Downloads/stxxl-1.3.0/lib -lstxxl