CMAKE:在 clang / g++ 和 libc++ /libstdc++ 之间切换
CMAKE: switch between clang / g++ and libc++ /libstdc++
这是我的第一个 cmake 文件。我有一个带有 clang 和 g++ 的 linux 系统。还安装了 libc++。我在 Mac (xcode) 上开发,但部署到 linux。
我正在编写一个 cmake 文件,我可以在其中选择 clang 或 g++ 以及 libc++ 或 libstdc++。所以有 4 种可能的组合。
我想出了如何 select 编译器并在其上强制使用 c++11,但我不知道如何指定标准库。有什么建议吗?
这是我目前拥有的:
## cmake ###
cmake_minimum_required (VERSION 3.5)
#set project directories
set(ProjectDirectory ${CMAKE_SOURCE_DIR}) #.../Project/
set(BuildDirectory ${ProjectDirectory}/Build)
set(ProductDirectory ${ProjectDirectory}/Products)
set(sourceDirectory ${ProjectDirectory}/Source)
#print project directories
message(${ProjectDirectory})
message(${BuildDirectory})
message(${ProductDirectory})
#configure cmake
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_VERBOSE_MAKEFILE on)
#compiler and standard library settings
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -stdlib=libc++")
#libstdc++ #linux
#libc++ #OS X
#compiler flags
SET( CompilerFlags " ")
#SET( CompilerFlags "${CompilerFlags} -stdlib=libc++" )
SET( CompilerFlags "${CompilerFlags} -Wno-unknown-pragmas" )
SET( CompilerFlags "${CompilerFlags} -Wall" )
SET( CompilerFlags "${CompilerFlags} -Wextra" )
set (CMAKE_CXX_FLAGS ${CompilerFlags})
#linker flags
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
#message(${CMAKE_CXX_FLAGS})
##################
### Libraries ###
################
### common library ###
project(common)
#message(STATUS ${CMAKE_CXX_FLAGS})
#source files
#set (commonSource ${sourceDirectory}/Object/object.cpp) #specify specific files
file(GLOB_RECURSE commonSource ${sourceDirectory}/Object/*.cpp) # recursive
#targets
add_library (common STATIC ${commonSource})
#target_compile_options (common PUBLIC ${CompilerFlags})
#####################
### Applications ###
###################
### Hello project ###
project (hello)
#source files
#set (helloSource ${sourceDirectory}/main.cpp)
#targets
#add_executable (hello ${helloSource})
#linking
#target_link_libraries (hello common)
#install(TARGETS common DESTINATION ${ProductDirectory})
我在控制台上运行这个命令
../Project/Build$ rm -r *; rm -r ../Products/*; cmake ..; make VERBOSE=1;
我的文件夹结构是:
Project
Source
Build
Products
CMakeLists.txt
我还注意到编译器标志有时会被忽略,仅在我 运行 cmake 和 make 第二次时使用。
此外,我在代码中使用了很多 #warning todo
如何在编译期间禁用这些警告?
这有点棘手,因为我不相信 CMake 有内置的方法 select 标准库。我所做的是将它传递到链接器标志中。例如
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
CMake 支持,但是支持设置编译器:
set(CMAKE_CXX_COMPILER "clang++")
这是我的第一个 cmake 文件。我有一个带有 clang 和 g++ 的 linux 系统。还安装了 libc++。我在 Mac (xcode) 上开发,但部署到 linux。 我正在编写一个 cmake 文件,我可以在其中选择 clang 或 g++ 以及 libc++ 或 libstdc++。所以有 4 种可能的组合。
我想出了如何 select 编译器并在其上强制使用 c++11,但我不知道如何指定标准库。有什么建议吗?
这是我目前拥有的:
## cmake ###
cmake_minimum_required (VERSION 3.5)
#set project directories
set(ProjectDirectory ${CMAKE_SOURCE_DIR}) #.../Project/
set(BuildDirectory ${ProjectDirectory}/Build)
set(ProductDirectory ${ProjectDirectory}/Products)
set(sourceDirectory ${ProjectDirectory}/Source)
#print project directories
message(${ProjectDirectory})
message(${BuildDirectory})
message(${ProductDirectory})
#configure cmake
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_VERBOSE_MAKEFILE on)
#compiler and standard library settings
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -stdlib=libc++")
#libstdc++ #linux
#libc++ #OS X
#compiler flags
SET( CompilerFlags " ")
#SET( CompilerFlags "${CompilerFlags} -stdlib=libc++" )
SET( CompilerFlags "${CompilerFlags} -Wno-unknown-pragmas" )
SET( CompilerFlags "${CompilerFlags} -Wall" )
SET( CompilerFlags "${CompilerFlags} -Wextra" )
set (CMAKE_CXX_FLAGS ${CompilerFlags})
#linker flags
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
#message(${CMAKE_CXX_FLAGS})
##################
### Libraries ###
################
### common library ###
project(common)
#message(STATUS ${CMAKE_CXX_FLAGS})
#source files
#set (commonSource ${sourceDirectory}/Object/object.cpp) #specify specific files
file(GLOB_RECURSE commonSource ${sourceDirectory}/Object/*.cpp) # recursive
#targets
add_library (common STATIC ${commonSource})
#target_compile_options (common PUBLIC ${CompilerFlags})
#####################
### Applications ###
###################
### Hello project ###
project (hello)
#source files
#set (helloSource ${sourceDirectory}/main.cpp)
#targets
#add_executable (hello ${helloSource})
#linking
#target_link_libraries (hello common)
#install(TARGETS common DESTINATION ${ProductDirectory})
我在控制台上运行这个命令
../Project/Build$ rm -r *; rm -r ../Products/*; cmake ..; make VERBOSE=1;
我的文件夹结构是:
Project
Source
Build
Products
CMakeLists.txt
我还注意到编译器标志有时会被忽略,仅在我 运行 cmake 和 make 第二次时使用。
此外,我在代码中使用了很多 #warning todo
如何在编译期间禁用这些警告?
这有点棘手,因为我不相信 CMake 有内置的方法 select 标准库。我所做的是将它传递到链接器标志中。例如
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
CMake 支持,但是支持设置编译器:
set(CMAKE_CXX_COMPILER "clang++")