Compiling a static executable with CMake(使用 CMake 编译静态可执行文件)
问题描述
对于一个项目,我需要创建一个可执行文件,其中包含我使用的所有库(opencv、cgal),以便在没有这些库的计算机上执行它.目前,这是我的 CMakeLists.txt(我使用 linux).
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).
通过这种方式,我的可执行文件中只包含 GMP 和其他一些 C++ 库.我的问题是:如何创建一个 makefile 以便以静态方式自动包含所有库并仅创建一个包含所有库的大"可执行文件?你能帮我吗?
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?
推荐答案
作为全局 CMake 设置,在 add_executable 之前添加这些行,对 gcc/clang 有效:
As global CMake settings, add these lines before add_executable, valid for gcc/clang:
关于现代 CMake (3.x+ - target_link_libraries doc),您可以将标志应用于特定目标,方式如下:
On Modern CMake (3.x+ - target_link_libraries doc), you can apply the flag to specific targets, in this way:
如果您使用 MSVC,则必须设置编译器和链接器标志:
If you're using MSVC, you have to set the compiler and linker flags:
如果您使用的是 MFC,则需要将标志指定为 1
见这里:
and if you are using MFC, you need to specify the flag to 1
see here:
这篇关于使用 CMake 编译静态可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!