简体   繁体   English

基于CMake平台的Clang

[英]Clang based on platform in CMake

I'm setting up a cross platform build environment. 我正在建立一个跨平台的构建环境。 For that reason I've chosen to go for clang, for the, correct me if I'm wrong reasons: 出于这个原因,我选择了c,因此,如果我做错了原因,请纠正我:

  • Using the "same" compiler for different platform would potentially reduce the quirks that arise when getting gcc code to work with MSVC 在不同平台上使用“相同”编译器可能会减少使gcc代码与MSVC一起使用时出现的怪癖。
  • Clang with its MSVC compatible ABI would let me work with the WINAPI as if I were using MSVC. Clang及其与MSVC兼容的ABI使我可以像使用MSVC一样使用WINAPI。

My question is: How do I get CMake to, based on the platform, find the correct clang to use, that is clang-cl in Windows and clang on *nix platforms ? 我的问题是:如何基于平台找到CMake,以找到要使用的正确的clang,即Windows中的clang-cl和* nix平台上的clang

Please note that I'll be using Ninja generator for CMake, and the project is Qt based, if that matters. 请注意,我将使用Ninja Generator用于CMake,并且该项目基于Qt(如果重要)。

You (the project author) don't get to choose the compiler, the user that invokes CMake chooses the compiler. 您(项目作者)不会选择编译器,调用CMake的用户会选择编译器。

The only thing you can do from within the CMakeLists is abort with an error if you are not happy with the choice that the user made. 如果您对用户所做的选择不满意,那么从CMakeLists唯一可以做的就是中止并返回错误。

There are a couple of ways how the user can specify to CMake which compiler to use. 用户可以通过两种方式向CMake指定要使用的编译器。 Popular examples include setting the respective environment variables , passing it via -DCMAKE_CXX_COMPILER on the first CMake run (and only on the first run; you cannot change this later without re-configuring from scratch!), or by using a toolchain file . 流行的示例包括设置相应的环境变量 ,在第一次CMake运行时( 在第一次运行时)通过-DCMAKE_CXX_COMPILER传递该变量 ,或者稍后使用工具链文件进行重新配置!

On Visual Studio, you can also use the -T option to select a build toolchain. 在Visual Studio上,您还可以使用-T选项选择构建工具链。 This allows you to build with clang from Visual Studio. 这使您可以使用Visual Studio中的clang进行构建。

You should document in your project's Readme which compiler you expect your users to configure for CMake and maybe give them a small example how to do this via one of the methods above. 您应该在项目的自述文件中记录希望用户为CMake配置的编译器,并可能给他们一个小的示例,说明如何通过上述方法之一执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM