C++ 使用 CMake 编译静态可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24648357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:47:12  来源:igfitidea点击:

Compiling a static executable with CMake

c++opencvcompilationcmakecgal

提问by Andrea

for a project I need to create an executable that includes all the libraries that I used (opencv, cgal) in order to execute it on a computer that has not those libraries. Currently, this is my CMakeLists.txt (I use linux).

对于一个项目,我需要创建一个可执行文件,其中包含我使用的所有库(opencv、cgal),以便在没有这些库的计算机上执行它。目前,这是我的 CMakeLists.txt(我使用 linux)。

cmake_minimum_required(VERSION 2.8)
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -O2")
project( labeling )
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
add_library(OpenCV STATIC IMPORTED)
add_library(CGAL STATIC IMPORTED COMPONENTS Core)
add_library(GMP STATIC IMPORTED)
find_package(OpenCV REQUIRED)
find_package(CGAL QUIET COMPONENTS Core )
find_library(GMP_LIBRARY gmp /usr/lib)

include(src)
include( ${CGAL_USE_FILE} )
include( CGAL_CreateSingleSourceCGALProgram )


set(EXECUTABLE_OUTPUT_PATH ../bin)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

include_directories( src )
include_directories( ${OpenCV_INCLUDE_DIRS} )

file(GLOB_RECURSE nei_SRC "src/*.cpp")
add_executable( nei_segmentation ${nei_SRC})
target_link_libraries( nei_segmentation ${OpenCV_LIBS} ${GMP_LIBRARY})

In such a way only GMP and some other c++ libraries are included in my executable. My question is: How can I create a makefile in order to automatically including all the libraries in a static manner and creating only a "big" executable that contains all the libraries? Can you help me?

通过这种方式,我的可执行文件中只包含 GMP 和其他一些 C++ 库。我的问题是:如何创建一个 makefile 以便以静态方式自动包含所有库并仅创建一个包含所有库的“大”可执行文件?你能帮助我吗?

回答by madduci

As global CMake settings, add these lines before add_executable, valid for gcc/clang:

作为全局 CMake 设置,在 add_executable 之前添加这些行,对 gcc/clang 有效:

SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBS OFF)
SET(CMAKE_EXE_LINKER_FLAGS "-static")

On Modern CMake(3.x+ - target_link_libraries doc), you can apply the flag to specific targets, in this way:

Modern CMake(3.x+ - target_link_libraries doc) 上,您可以通过以下方式将该标志应用于特定目标:

target_link_libraries(your_target_name -static)

If you're using MSVC, you have to set the compiler and linker flags:

如果您使用MSVC,则必须设置编译器和链接器标志:

target_compile_options(your_target_name PUBLIC/PRIVATE /MT)
target_link_options(your_target_name PUBLIC/PRIVATE /INCREMENTAL:NO /NODEFAULTLIB:MSVCRT)

and if you are using MFC, you need to specify the flag to 1see here:

如果您使用的是 MFC,则需要指定标志以1在此处查看

 set(CMAKE_MFC_FLAG 1) 

回答by Logicae

Add these lines after add_executable(MyExec "main.c")(for exemple) :

add_executable(MyExec "main.c")(例如)之后添加这些行:

target_link_libraries(MyExec PUBLIC "-static")

target_link_libraries(MyExec PUBLIC "-static")

or before : link_libraries("-static")

或之前: link_libraries("-static")