我正在嘗試構建靜態可執行文件,但沒有成功構建。在添加以下行之前它工作正常:使用CMAKE編譯靜態可執行文件
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "-static")
我想將它轉換爲靜態可執行文件。任何幫助或建議?
這是我的cmake代碼:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -Werror -lpthread")
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "-static")
add_executable(${PROJECT_NAME} MACOSX_BUNDLE
main.cc
audiofile.cc
button.cc
entity.cc
entity_group.cc
filter.cc
splash_screen_state.cc
sprite.cc
signal.cc
signal_chunk.cc
sound_edit_state.cc
splash_screen_state.cc
sprite.cc
state.cc
state_stack.cc
waveform_chunk_display.cc
waveform_chunk_select_button.cc
waveform_chunk_select_display.cc
waveform_component_display.cc
waveform_display.cc
wwest_app.cc
play_original_button.cc
low_pass_button.cc
high_pass_button.cc
mid_pass_button.cc
reset_button.cc
play_modified_button.cc
speed_up_button.cc
slow_down_button.cc)
find_package(PkgConfig REQUIRED)
pkg_search_module(SDL2 REQUIRED sdl2)
pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image>=2.0.0)
pkg_search_module(SDL2_TTF REQUIRED SDL2_ttf)
pkg_search_module(NFD REQUIRED sdl2)
find_package(OpenGL REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR}
${OPENGL_INCLUDE_DIRS}
${NFD_INCLUDE_DIRS}
${GTK3_INCLUDE_DIRS})
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES}
${OPENGL_LIBRARIES}
${NFD_LIBRARIES}
${GTK3_LIBRARIES}
nfd
pthread)
我試圖合併的步驟從鏈接,但沒有工作,得到以下錯誤:
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libGLU.so' collect2: error: ld returned 1 exit status src/CMakeFiles/wwest-outreach-app.out.dir/build.make:774: recipe for target 'bin/wwest-outreach-app.out' failed make[2]: * [bin/wwest-outreach-app.out] Error 1 CMakeFiles/Makefile2:85: recipe for target 'src/CMakeFiles/wwest-outreach-app.out.dir/all' failed make[1]: * [src/CMakeFiles/wwest-outreach-app.out.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2
這裏是我試過下面的鏈接: Compiling a static executable with CMake
注意:我添加了下面一行,它編譯但不是靜態的,因爲我仍然無法在其他linux上打開輸出文件(.out)機器。
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
是的,我可以在我的系統上找到libGLU.a。我嘗試添加上面提到的行,但仍然出現相同的錯誤。 我沒有完全理解緩存部分。我如何確保清除它? –
找到'CMakeCache.txt'並將其刪除。這將消除構建中的任何緩存的cmake變量,然後重新運行cmake以重新生成。 –
我做到了。我添加了下面的代碼並編譯完成。但它仍然不是靜態的,因爲我不能在其他沒有庫的linux上運行輸出文件。 (CMAKE_EXE_LINKER_FLAGS「-static-libgcc -static-libstdC++」) 更新了問題。 –