背景
项目生成lib引入,在生成exe过程中无法正常运行
处理办法
让链接器静态链接GCC和C++标准库
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
主CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
project(speech)
# 编译版本
set(CMAKE_CXX_STANDARD 17)
# 让链接器静态链接GCC和C++标准库
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
# 设置可执行文件输出路径
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)
# 自定义变量
set(M_LIB mlib)
set(BASE_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(BASE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(BASE_LIB ${CMAKE_CURRENT_SOURCE_DIR}/lib)
# 增加子模块
add_subdirectory(m_lib)
# 头文件包含目录
include_directories(${BASE_INCLUDE})
# 源文件包含目录
aux_source_directory(${BASE_SRC} SRC_LIST)
# 链接静态库
link_directories(${BASE_LIB})
link_libraries(${M_LIB})
# 输出配置
add_executable(speech main.cpp)
子CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(${M_LIB})
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
include_directories(${BASE_INCLUDE})
set(LIBRARY_OUTPUT_PATH ${BASE_LIB})
add_library(${M_LIB} STATIC my_lib.cpp)
标签:set,CMAKE,LIB,自定义,CURRENT,SOURCE,BASE,exe,cmake
From: https://www.cnblogs.com/paylove/p/18338143