0
点赞
收藏
分享

微信扫一扫

cmake if-else- else-endif 条件语句


​​代码在git​​

CMakeLists.txt

# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# project name and language
project(recipe-04 LANGUAGES CXX)

# introduce a toggle for using a library
set(USE_LIBRARY OFF)

message(STATUS "Compile sources into a library? ${USE_LIBRARY}")

# BUILD_SHARED_LIBS is a global flag offered by CMake
# to toggle the behavior of add_library
set(BUILD_SHARED_LIBS OFF)

# list sources
list(APPEND _sources Message.hpp Message.cpp)

if(USE_LIBRARY)
# add_library will create a static library
# since BUILD_SHARED_LIBS is OFF
add_library(message ${_sources})

add_executable(hello-world hello-world.cpp)

target_link_libraries(hello-world message)
else()
add_executable(hello-world hello-world.cpp ${_sources})
endif()

set(USE_LIBRARY OFF) 默认直接从源码编译,不使用库文件

CMakeCache.txt  CMakeFiles  cmake_install.cmake  hello-world  Makefile

set(USE_LIBRARY ON) add_library(message ${_sources}) 默认是静态库,build 文件夹下有 libmessage.a 静态库文件

CMakeCache.txt  CMakeFiles  cmake_install.cmake  hello-world  libmessage.a  Makefile

set(USE_LIBRARY ON)   set(BUILD_SHARED_LIBS OFF)  编译动态库,build 文件夹下有 libmessage.so 动态库文件

CMakeCache.txt  CMakeFiles  cmake_install.cmake  hello-world  libmessage.so  Makefile


举报

相关推荐

0 条评论