Want to make standalone program with cmake

杀马特。学长 韩版系。学妹 提交于 2020-01-14 12:17:31

问题


My program uses giblib and Imlib2 library and it is built with cmake.

It works perfectly in my computer but not in other's.

I guess the reason is I installed every library what my program needs but other's doesn't.

My goal is making standalone program. (don't need to install any other library addtionally)

What should I add in cmake file?


projectDef.cmake source code

file (GLOB PLATFORM RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
    X11/[^.]*.cpp
    X11/[^.]*.h
    X11/[^.]*.cmake
    )

SOURCE_GROUP(X11 FILES ${PLATFORM})

add_definitions(
)

set (SOURCES
    ${SOURCES}
    ${PLATFORM}
    )

add_x11_plugin(${PROJECT_NAME} SOURCES)

target_link_libraries(${PROJECT_NAME}
    ${PLUGIN_INTERNAL_DEPS}
    )

include_directories(/usr/include/giblib)
include_directories(/usr/include/X11)

target_link_libraries(MyProject X11)
target_link_libraries(MyProject Imlib2)
target_link_libraries(MyProject giblib)

CMakeList.txt source code

cmake_minimum_required (VERSION 2.6)
set (CMAKE_BACKWARDS_COMPATIBILITY 2.6)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static")

Project(${PLUGIN_NAME})

file (GLOB GENERAL RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
    [^.]*.cpp
    [^.]*.h
    [^.]*.cmake
    )

include_directories(${PLUGIN_INCLUDE_DIRS})


SET_SOURCE_FILES_PROPERTIES(
    ${GENERATED}
    PROPERTIES
        GENERATED 1
    )

SOURCE_GROUP(Generated FILES
    ${GENERATED}
    )

SET( SOURCES
    ${GENERAL}
    ${GENERATED}
    )

include_platform()


回答1:


Try starting with this options in your root CMakeLists.txt:

set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-static")

BUILD_SHARED_LIBS only needed if your project has own libraries (add_library).

With the -static linker flag, you need static libs for all your additional libraries too! One common problem when static linking is do avoid circular dependencies.



来源:https://stackoverflow.com/questions/7101862/want-to-make-standalone-program-with-cmake

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