CMAKE:在clang/g ++和libc ++/libstdc ++之间切换 [英] CMAKE: switch between clang / g++ and libc++ /libstdc++

查看:774
本文介绍了CMAKE:在clang/g ++和libc ++/libstdc ++之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my first cmake file. I have a linux system with both clang and g++. Also libc++ is installed. I develop on Mac (xcode) but deploy to linux. I am writing a cmake file in which I can pick either clang or g++ and libc++ or libstdc++. So 4 possible combinations.

我想出了如何选择编译器并在其上强制使用c ++ 11,但是我不知道如何指定标准库.有什么建议吗?

I figured out how to select the compiler and force c++11 on it, but I can't figure out how to specify the standard library. Any suggestions?

这是我到目前为止所拥有的:

This is what I have so far:

## 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})

我在控制台上运行此命令

I run this command on a console

../Project/Build$ rm -r *; rm -r ../Products/*; cmake ..; make VERBOSE=1;

我的文件夹结构是:

Project
    Source
    Build
    Products
    CMakeLists.txt

我还注意到,有时会忽略编译器标志,并且仅在我运行cmake并进行第二次操作时使用.

I have also noticed that the compiler flags are sometimes ignored and only used when I run cmake and make a second time.

此外,我在代码中使用了很多#warning todo 如何在编译过程中禁用这些警告?

Also, I use a lot of #warning todo in my code How can I disable these warnings during compilation?

推荐答案

这有点棘手,因为我不认为CMake具有选择标准库的内置方式.我要做的就是将其传递给链接器标志.例如

It's a little tricky because I don't believe CMake has a built in way to select a standard library. What I do is pass it into the linker flags. e.g.

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

CMake 确实,但是支持设置编译器:

CMake does, however, support setting the compiler:

set(CMAKE_CXX_COMPILER "clang++")

这篇关于CMAKE:在clang/g ++和libc ++/libstdc ++之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆