背景
專案生成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)