Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to force compilation of Boost to use -fPIC

Tags:

The team on which I work produces a shared library for use in Python. This library is entirely C++ and we use Boost to expose to python. Because we cannot guarantee that our clients have the Boost libraries installed, we pull in the functionality needed from Boost to the shared object file statically. The final stage in compilation will look familiar to many

g++ -o <output> <objects> -Wl,-Bstatic -lboost_python -lboost_regex ... -Wl,-Bdynamic -shared <other_opts> 

We've traditionally used our own build of Boost: 1.47. This version is now quite old and so we wish to update. However, oddly, when I install the necessary objects using yum on my CentOS 7 system, I get the following error from gcc:

relocation R_X86_64_32 against '.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC 

Well, I thought I'd simply download the latest boost (CentOS 7 installs Boost 1.53) and do my own build. This, after all, has always worked for us. I follow the instructions here but I got the same error. How do I force the use of -fPIC for even the static libraries that it builds?

like image 300
Andrew Falanga Avatar asked Jan 08 '15 19:01

Andrew Falanga


People also ask

Does Boost need to be compiled?

Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking. The only Boost libraries that must be built separately are: Boost.

How do I use Boost library in code blocks?

Download either the zip or the 7zip package of boost. Extract the contents to extract_dir. If the minGW\bin folder (can be found in CodeBlocks installatoin folder) is not in the path variable add it. Open the file extract_dir\project-config.

How do I link my Boost library to Visual Studio?

6.1 Link From Within the Visual Studio IDE Right-click example in the Solution Explorer pane and select Properties from the resulting pop-up menu. In Configuration Properties > Linker > Additional Library Directories, enter the path to the Boost binaries, e.g. C:\Program Files\boost\boost_1_55_0\lib\.


2 Answers

I believe boost automatically uses -fPIC when compiling a shared library (.so file), but the below command uses -fPIC when compiling a static library (.a file) too.

This worked for me on boost 1.46.1:

sudo ./bjam cxxflags=-fPIC cflags=-fPIC -a ... install 

The ... is where you add additional flags like threading=multi or --layout=tagged, and optionally the list of projects to build (for example: --with-regex).

Note: using both cflags and cxxflags is unnecessary, only one is needed. See comments below.

Reference links:

  • https://cmake.org/Wiki/TubeTK/Build_Instructions#Boost_.28optional.29
  • http://lists.boost.org/boost-users/2010/07/60682.php
like image 108
Brad Cupit Avatar answered Sep 28 '22 11:09

Brad Cupit


Just for convenience, I combined previous answer and comments to it:

sudo ./bjam cxxflags=-fPIC -a --with-system install 

--with-system is not necessary, but it's a place where you can add other boost compile options

It works for me at CentOS 7 with boost 1.67

like image 39
Nikolay Pakudin Avatar answered Sep 28 '22 10:09

Nikolay Pakudin