简体   繁体   English

如何在 CMAKE 中制作可执行文件?

[英]How can I make fat execuatable file in CMAKE?

I am newbie on CMAKE. I am trying to make "fat executable file".我是 CMAKE 的新手。我正在尝试制作“胖可执行文件”。

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.我提到的术语 fat executable file 是一个可执行二进制文件,它包含所有共享库并且可以在另一个环境中运行。

Example (Trouble Situation)示例(故障情况)

There is ComputerA which is a computer that I used for developing. ComputerA 是我用来开发的计算机。 There is GCC, Boost libraries, librados etc.有 GCC、Boost 库、librados 等。

And there is ComputerB which is a computer that I wanted to execute the binary that I built. ComputerB 是我想要执行我构建的二进制文件的计算机。

* Of course, Both Computer A and B have some conditions. * 当然,计算机A和B都有一些条件。 Same Intel CPU Arch.相同的 Intel CPU Arch。 (i7), Same CentOS. (i7),同 CentOS。

I built binary file in ComputerA using CMAKE with this command, cmake.. && make .我使用 CMAKE 和这个命令cmake.. && make在 ComputerA 中构建了二进制文件。 And I copied this file to ComputerB and executed.然后我将这个文件复制到 ComputerB 并执行。

When I execute the copied binary file in Computer B, it said like below.当我在计算机 B 中执行复制的二进制文件时,它如下所示。

[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 .所以,我想在这个社区问的是,什么是 CMAKE Command for containing shared libraries

Similar thing:: fat JAR类似的东西::脂肪 JAR

The reason, why I called "fat executable file", is that there is a word "fat JAR" in Java .我之所以称之为“胖可执行文件”,是因为Java 中有一个词“胖 JAR” I am not familiar with Java. But it is commonly used word in Java community. Java我不熟悉,但在Java社区里是常用词。

My CMAKE File我的 CMAKE 文件

It contains some libraries like Boost, Rados, RBD etc. (There will be more according to progress of developing)它包含一些库,如Boost、Rados、RBD等。(根据开发进度会有更多)

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.感谢@HolyBlackCat,我用 static 链接解决了这个问题。

Modified CMAKE修改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 .有额外的(修改过的)关键字, add_libraryset_target_propertiestarget_link_libraries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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