如何从CMakeLists的Clion中切换GCC和Clang。三种使用windows / cygwin

时间:2022-08-19 02:24:17

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.

可能还有其他变量需要改变。尝试向CMAKE_CXX_FLAGS添加-lstdc++ +,但没有什么不同。

Is there a CLion way as opposed to a CMake way, for example?

例如,是否有一种与CMake方式相对的CLion方式?

Thanks.

谢谢。

1 个解决方案

#1


11  

Specifying a compiler with CMake is a bit delicate. Although the method you are using, setting CMAKE_CXX_COMPILER in CMakeLists.txt works, it is the least-recommended way in the CMake FAQ.

使用CMake指定编译器有点复杂。虽然使用的是方法,但是在CMakeLists中设置CMAKE_CXX_COMPILER。txt有效,这是CMake FAQ中最不推荐的方法。

CLion supports method 2 of the CMake FAQ: using -D within the cmake invocation. Setting the variables in CMakeLists.txt has no effect.

CLion支持CMake FAQ的方法2:在CMake调用中使用-D。在CMakeLists中设置变量。txt没有影响。

On Mac, go to Preferences

在Mac上,转到Preferences

On Linux/Windows, go to File | Settings

在Linux/Windows上,转到文件|设置

then Build, Execution, Deployment | CMake | CMake options and enter the text:

然后构建、执行、部署| CMake | CMake选项,输入文本:

-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. This is another path to investigate.

在我写了这个答案之后,CLion增加了对多个构建目录的支持,正如@rubenvb在评论中指出的那样。这是另一种研究方法。

#1


11  

Specifying a compiler with CMake is a bit delicate. Although the method you are using, setting CMAKE_CXX_COMPILER in CMakeLists.txt works, it is the least-recommended way in the CMake FAQ.

使用CMake指定编译器有点复杂。虽然使用的是方法,但是在CMakeLists中设置CMAKE_CXX_COMPILER。txt有效,这是CMake FAQ中最不推荐的方法。

CLion supports method 2 of the CMake FAQ: using -D within the cmake invocation. Setting the variables in CMakeLists.txt has no effect.

CLion支持CMake FAQ的方法2:在CMake调用中使用-D。在CMakeLists中设置变量。txt没有影响。

On Mac, go to Preferences

在Mac上,转到Preferences

On Linux/Windows, go to File | Settings

在Linux/Windows上,转到文件|设置

then Build, Execution, Deployment | CMake | CMake options and enter the text:

然后构建、执行、部署| CMake | CMake选项,输入文本:

-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. This is another path to investigate.

在我写了这个答案之后,CLion增加了对多个构建目录的支持,正如@rubenvb在评论中指出的那样。这是另一种研究方法。