简体   繁体   中英

How can I make fat execuatable file in CMAKE?

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
)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM