roguepnz
roguepnz

Reputation: 113

Can't statically link boost libraries with cmake

I've boost signals library, builded with:

./b2 address-model=32 link=static --build-type=complete --with-signals --layout=tagged

simple code:

#include <iostream>
#include "boost/signal.hpp"

void onSignal() {
 std::cout << "Hello from slot" << std::endl;
}

int main() {
  boost::signal<void()> sig;
  sig.connect(&onSignal);
  sig();
 return 0;
}

CmakeLists.txt:

project(test)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -static")
aux_source_directory(. SRC_LIST)
include_directories(/home/koshchiy/dev/boost/boost_1_51_0)
link_directories(/home/koshchiy/dev/boost/boost_1_51_0/stage)

add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} libboost_signals-mt-s.a)

It builds without errors, but while runnig, I'm got:

bash: ./test: No such file or directory

seems to be, that cmake produces dynamic linking instead of static:

koshchiy@koshchiy-lin-NB:~/dev/projects/test/test-build-release$ ldd test
linux-gate.so.1 =>  (0xf778b000)
libstdc++.so.6 => /usr/lib32/libstdc++.so.6 (0xf768e000)
libc.so.6 => /lib32/libc.so.6 (0xf74e7000)
libm.so.6 => /lib32/libm.so.6 (0xf74bc000)
/usr/lib/libc.so.1 => /lib/ld-linux.so.2 (0xf778c000)
libgcc_s.so.1 => /usr/lib32/libgcc_s.so.1 (0xf749e000)

Building without cmake seems to be ok:

g++ -static main.cpp -o test -I... -l...

I'm using ubuntu 12.04 x64 and g++-4.7 compiler.

Upvotes: 1

Views: 2163

Answers (1)

Patrick B.
Patrick B.

Reputation: 12353

Your manual build is done without the -m32 flag that you are using in the CMakeLists.txt in the CMAKE_CXX_FLAGS .

Thus the build with cmake results in a 32-bit executable, which fails to work with "No such file" even though the file exists, because you're missing the binary support for 32-bits on your system.

Either remove the -m32 of your CMakeFiles.txt or try to install the gcc-multilib support for your system and your gcc-version.

If you want to force static linking to your cmake-build check if this answer can help you.

Upvotes: 3

Related Questions