1

I am newbie on CMAKE. I am trying to make "fat executable file".

The term, fat executeable file, I mentioned, is a executable binary file which contains all shared libraries and can be just run in another environment.

Example (Trouble Situation)

There is ComputerA which is a computer that I used for developing. There is GCC, Boost libraries, librados etc.

And there is ComputerB which is a computer that I wanted to execute the binary that I built.

* Of course, Both Computer A and B have some conditions. Same Intel CPU Arch. (i7), Same CentOS.

I built binary file in ComputerA using CMAKE with this command, cmake .. && make. And I copied this file to ComputerB and executed.

When I execute the copied binary file in Computer B, it said like below.

[root@ComputerB ~]# ./binaryFile 123.123.123.123
./binaryFile: error while loading shared libraries: libboost_json.so.1.80.0: cannot open shared object file: No such file or directory

I understand what this error message say. There is no shared library which it needed. So, what I want to ask in this community is, What is CMAKE Command for containing shared libraries.

Similar thing :: fat JAR

The reason, why I called "fat executable file", is that there is a word "fat JAR" in Java. I am not familiar with Java. But it is commonly used word in Java community.

My CMAKE File

It contains some libraries like Boost, Rados, RBD etc. (There will be more according to progress of developing)

cmake_minimum_required(VERSION 3.2.0)
project(BinaryHelper VERSION 0.1.0)

find_package(Boost REQUIRED COMPONENTS json)
include_directories(${Boost_INCLUDE_DIR})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lrados -std=c++17")

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_executable(
    ${PROJECT_NAME}
    main.cpp
    utils/binary.cpp
    utils/message.cpp
    utils/header.cpp
)

target_link_libraries (
    ${PROJECT_NAME}
    ${Boost_LIBRARIES}
    rados
    rbd
    Threads::Threads
)
TyeolRik
  • 466
  • 2
  • 25
  • [Fat binary](https://en.wikipedia.org/wiki/Fat_binary) means something else. You either need static linking, or copy the libraries separately. You can install them as packages, or copy them manually. – HolyBlackCat Oct 07 '22 at 05:09
  • @HolyBlackCat Oh Thanks. I am searching with the keyword "boost static link" and trying. However, like ```rados```, how can I copy them manually? – TyeolRik Oct 07 '22 at 05:12
  • You just copy them. Get the list of libs usind `ldd`. You can place them in `/usr/local/lib` for them to be found automatically, or in a custom directory if you set `LD_LIBRARY_PATH` or rpath. – HolyBlackCat Oct 07 '22 at 05:17
  • @HolyBlackCat Well, actually I just want to send only ONE file and it is execuable well. In your answer, There is no solution that I wanted. (for rados, rbd or something else) right? – TyeolRik Oct 07 '22 at 05:25
  • 1
    Then try static linking? – HolyBlackCat Oct 07 '22 at 07:08
  • @HolyBlackCat Thanks. It works. I am going to write the solution in my question. – TyeolRik Oct 07 '22 at 08:06
  • For more complex applications, there's always [exodus](https://github.com/intoli/exodus) or similar application packer tools. – Botje Oct 07 '22 at 09:33

1 Answers1

0

Thanks to @HolyBlackCat, I solved the problem with static link.

Modified CMAKE

cmake_minimum_required(VERSION 3.2.0)
project(BinaryHelper VERSION 0.1.0)

find_package(Boost REQUIRED COMPONENTS json)
include_directories(${Boost_INCLUDE_DIR})

set(Boost_USE_STATIC_LIBS   ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lrados -std=c++17")

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_library(
    boost_json
    STATIC
    IMPORTED
)

set_target_properties(
    boost_json
    PROPERTIES
    IMPORTED_LOCATION /usr/local/lib/libboost_json.a
)

add_executable(
    ${PROJECT_NAME}
    main.cpp
    utils/binary.cpp
    utils/message.cpp
    utils/header.cpp
)

# target_link_libraries (
#     ${PROJECT_NAME}
#     ${Boost_LIBRARIES}
#     rados
#     rbd
#     Threads::Threads
# )

target_link_libraries(
    ${PROJECT_NAME}
    boost_json
    rados
    rbd
    Threads::Threads
)

There is additional(modified) keywords, add_library, set_target_properties, target_link_libraries.

TyeolRik
  • 466
  • 2
  • 25