3

我正在开发一个小型模拟软件,它依赖于两个库,GSL 和 libconfig。作为构建系统,我使用 CMake。对于 GSL 和 libconfig,我找到了 cmake 文件并将它们复制到cmake/我的项目目录中。

场景如下:我希望项目有几种不同的构建类型,比如,等等debugrelease还有一个自定义的cluster,称为假设存在。-static.a

到目前为止我的CMakeLists.txt样子是这样的:

# version
SET(PACKAGE_VERSION "1.0")
SET(PACKAGE_NAME "INTERFACE")

PROJECT(interface C CXX)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)


# dirs -----------------------------------------------------

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

FIND_PACKAGE(GSL REQUIRED)
INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIRS})
SET(LIBS ${LIBS} ${GSL_LIBRARIES})

FIND_PACKAGE(LibConfig REQUIRED)
INCLUDE_DIRECTORIES(${LIBCONFIG_INCLUDE_DIRS})
SET(LIBS ${LIBS} ${LIBCONFIG_LIBRARIES})

CONFIGURE_FILE("res/config.h.in" "config.h")
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})

SET_DIRECTORY_PROPERTIES(PROPERTIES 
    ADDITIONAL_MAKE_CLEAN_FILES "config.h"
)


# compilation ----------------------------------------------

ADD_EXECUTABLE( interface
    interface.c interface.h config.h
    helpers.c 
    output.c
    lattice.c 
    genetic.c
)

# optional CFLAGS
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -std=c99")
SET(CMAKE_C_FLAGS_RELEASE "-O3 -ffast-math")
SET(CMAKE_C_FLAGS_CLUSTER "-O3 -ffast-math -static")
SET(CMAKE_C_FLAGS_DEBUG "-g")
SET(CMAKE_C_FLAGS_PROFILE "-g -ffast-math -pg")

TARGET_LINK_LIBRARIES(interface m ${LIBS})



# installation --------------------------------------------

INSTALL(TARGETS interface DESTINATION bin)

-static我使用-DCMAKE_BUILD_TYPE=cluster. 问题是,它仍然链接.so到库的版本,这导致 gcc 抛出错误。至少FindLibConfig.cmake脚本设置了我可以使用的 aLIBCONFIG_LIBRARY和变量。LIBCONFIG_STATIC_LIBRARY

达到我的目标的最优雅或最聪明的方式是什么?

4

2 回答 2

9

根据 cmake文档,cmake 使用变量 BUILD_SHARED_LIBS 来确定 add_library() 的默认值。

如果您设置为 ON,cmake 将假定所有add_library()调用都为

add_library(target SHARED lib1 lib2 ...)

例如,cmake 命令行上的 -DBUILD_SHARED_LIBS=ON 之类的东西可能会满足您的要求。

于 2013-12-17T20:53:46.590 回答
7

我是这样解决的:

用户可以指定一个附加变量-DSTATIC_LINKING=TRUE。然后,脚本看起来像这样。(只显示了静态链接和编译的重要部分!)

# determine, whether we want a static binary
SET(STATIC_LINKING FALSE CACHE BOOL "Build a static binary?")

# do we want static libraries?
# When STATIC_LINKING is TRUE, than cmake looks for libraries ending
# with .a. This is for linux only!
IF(STATIC_LINKING)
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
ENDIF(STATIC_LINKING)

# set -static, when STATIC_LINKING is TRUE and set LINK_SEARCH_END_STATIC
# to remove the additional -bdynamic from the linker line.
IF(STATIC_LINKING)
    SET(CMAKE_EXE_LINKER_FLAGS "-static")
    SET_TARGET_PROPERTIES(surface PROPERTIES 
        LINK_SEARCH_END_STATIC 1)
ENDIF(STATIC_LINKING)
于 2013-03-23T11:12:53.677 回答