gpt4 book ai didi

CMAKE:在 clang/g++ 和 libc++/libstdc++ 之间切换

转载 作者:行者123 更新时间:2023-12-01 11:20:51 24 4
gpt4 key购买 nike

这是我的第一个 cmake 文件。我有一个带有 clang 和 g++ 的 linux 系统。还安装了 libc++。我在 Mac (xcode) 上开发但部署到 linux。
我正在编写一个 cmake 文件,我可以在其中选择 clang 或 g++ 和 libc++ 或 libstdc++。所以有 4 种可能的组合。

我想出了如何选择编译器并在其上强制使用 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 有内置的方式来选择标准库。我所做的是将它传递给链接器标志。例如

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")

但是,CMake 确实支持设置编译器:
set(CMAKE_CXX_COMPILER "clang++")

关于CMAKE:在 clang/g++ 和 libc++/libstdc++ 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43662736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com