简体   繁体   English

如何使用windows / cygwin从CMakeLists.txt中切换Clion中的GCC和Clang

[英]How to switch between GCC and Clang in Clion from within CMakeLists.txt using windows/cygwin

I put 我放

 set(CMAKE_CXX_COMPILER           "/usr/bin/clang.exe")

Run/Clean, Run/Build. 运行/清洁,运行/构建。

I get link errors like: 我收到链接错误,如:

undefined reference to `std::ios_base::Init::~Init()'
: undefined reference to `__gxx_personality_v0'

Presumably there are other variables to change. 据推测,还有其他变量需要改变。 Tried adding -lstdc++ to CMAKE_CXX_FLAGS , but no different. 尝试将-lstdc++添加到CMAKE_CXX_FLAGS ,但没有区别。

Is there a CLion way as opposed to a CMake way, for example? 例如,是否存在CLion方式而不是CMake方式?

Thanks. 谢谢。

Specifying a compiler with CMake is a bit delicate. 使用CMake指定编译器有点微妙。 Although the method you are using, setting CMAKE_CXX_COMPILER in CMakeLists.txt works, it is the least-recommended way in the CMake FAQ . 虽然您使用的方法,在CMakeLists.txt中设置CMAKE_CXX_COMPILER,但它是CMake常见问题解答中最不推荐的方法。

CLion supports method 2 of the CMake FAQ : using -D within the cmake invocation. CLion支持CMake FAQ的方法2:在cmake调用中使用-D Setting the variables in CMakeLists.txt has no effect. 在CMakeLists.txt中设置变量无效。

On Mac, go to Preferences 在Mac上,转到Preferences

On Linux/Windows, go to File | Settings 在Linux / Windows上,转到File | Settings File | Settings

then Build, Execution, Deployment | CMake | CMake options 然后Build, Execution, Deployment | CMake | CMake options Build, Execution, Deployment | CMake | CMake options Build, Execution, Deployment | CMake | CMake options and enter the text: Build, Execution, Deployment | CMake | CMake options并输入文字:

-D CMAKE_C_COMPILER=/path/to/c_compiler
-D CMAKE_CXX_COMPILER=/path/to/c++_compiler

See the CLion FAQ for details. 有关详细信息,请参阅CLion FAQ

Note also that when you change compilers, you will have to invalidate the CLion cmake cache and restart, see my answer on How to clear CMake cache in Clion? 另请注意,当您更改编译器时,您必须使CLion cmake缓存无效并重新启动,请参阅我的答案如何在Clion中清除CMake缓存? .

EDIT 编辑

After I wrote this answer, CLion added support for multiple build directories, as pointed out by @rubenvb in the comments. 在我写完这个答案后,CLion添加了对多个构建目录的支持,正如@rubenvb在评论中指出的那样。 This is another path to investigate. 这是另一条调查途径。

In fact the latest version of Clions 2018.2 running on windows 10 environment work with LLVM clang version 6 /6.0.1 or even 7.0 along with GCC mingw x64 win32 specific variant. 实际上,在Windows 10环境中运行的最新版本的Clions 2018.2与LLVM clang版本6 /6.0.1甚至7.0一起使用以及GCC mingw x64 win32特定变体。

linker default is set to GCC not visual studio 链接器默认设置为GCC而不是visual studio

I guess it should work on cygwin too with the same setup as the following also tested to work on a number of popular c++ IDE also. 我想它也应该在cygwin上使用相同的设置,因为以下也经过测试,也适用于许多流行的c ++ IDE。

x64 or 32 specific GCC mingw version tested to work on Clions 2018.2 测试x64或32特定GCC mingw版本在Clions 2018.2上工作

\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0 or mingw-w64\\i686-8.1.0-win32-dwarf-rt_v6-rev0 \\ mingw-w64 \\ x86_64-8.1.0-win32-seh-rt_v6-rev0或mingw-w64 \\ i686-8.1.0-win32-dwarf-rt_v6-rev0

CMake build setup as below CMake构建设置如下

cmake_minimum_required(VERSION 3.10)
project(project_name )

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_C_COMPILER "c:/llvm/bin/clang.exe")
set(CMAKE_CXX_COMPILER "c:/llvm/bin/clang++.exe")
// target i686-pc-windows-gnu for 32bit 
set(CL_COVERAGE_COMPILE_FLAGS "-v -target x86_64-pc-windows-gnu -Wall -Wextra -std=gnu++17")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${CL_COVERAGE_COMPILE_FLAGS}" )
add_executable(project_name   yourcpp.cpp)

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

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