我正在开发一个小型仿真软件,它依赖于两个库,GSL和libconfig。作为构建系统,我使用CMake。对于GSL和libconfig,我找到了cmake文件并将它们复制到我项目的cmake/
目录中。
方案如下:我希望项目有几种不同的构建类型,例如debug
,release
等,还有一个名为cluster
的自定义构建类型, -static
到GCC标志和链接到GSL的.a
库和libconfig,我认为它存在。
我的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
添加到编译器。问题是,它仍然链接到lib的.so
版本,这会导致gcc抛出错误。至少FindLibConfig.cmake
脚本设置了LIBCONFIG_LIBRARY
和LIBCONFIG_STATIC_LIBRARY
变量,我可以使用它。
达到目标的最优雅或最聪明的方法是什么?
答案 0 :(得分:7)
根据cmake documentation,cmake使用变量BUILD_SHARED_LIBS来确定add_library()的默认值。
如果您设置为开启,则cmake将假设所有add_library()
来电均为
add_library(target SHARED lib1 lib2 ...)
例如,cmake命令行上的-DBUILD_SHARED_LIBS = ON可能会按照您的要求进行操作。
答案 1 :(得分:5)
用户可以指定其他变量-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)