Compiling a static executable with CMake

♀尐吖头ヾ 提交于 2019-11-27 12:51:51

As global CMake settings, add these lines before add_executable:

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:

target_link_libraries(your_target_name -static)

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

target_link_libraries(MyExec PUBLIC "-static")

or before : link_libraries("-static")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!