3

I am trying to build static executable but having no success in building up. It was working fine before adding following lines:

SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "-static")

I want to convert it into static executable. Any help or suggestions?

Here is my cmake Code :

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)

I tried merging steps from the link, but not working and getting following error:

/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

Here is the link that I tried following: Compiling a static executable with CMake

Note: I added following line and it compiled but not statically as I still cant open the output file (.out) on other linux machines.

set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

1 Answer 1

2

Can you locate libGLU.a on your system? The linker cannot link a dynamic lib (libGLU.so) to a statically built program.

If you can find libGLU.a on your system, make sure it is available in CMAKE_LIBRARY_PATH.

If you /still/ have problems with cmake picking the dynamic library over the static archive, try adding this to your CMakeLists.txt above the find command:

set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})

And last but not least, a reminder to clear your cmake cache so it isn't hanging onto libGLU.so.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I can locate libGLU.a on my system. I tried adding the line that you mentioned above and still getting same error. I didn't fully understand the cache part. How do I make sure that its being cleared out?
Find CMakeCache.txt and delete it. That will wipe out any cached cmake variables from the build, then rerun cmake to regenerate.
I did that. I added following lines and it compiled. But it is still not static as I cant run the output file on other linux which dont have libraries. set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") Updated the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.