Windows下CLion配置clang++使用MinGW-w64替代VC编译方案
Hey there, based on your setup details, here's a step-by-step guide to get your CMake projects up and running smoothly in CLion, whether you want to use GCC (MinGW-w64) or Clang.
First, Confirm CLion's Toolchain Detection
Since you've added MinGW-w64's 64-bit binaries to your PATH, CLion should automatically pick it up. Let's double-check:
- Open CLion, go to
File > Settings > Build, Execution, Deployment > Toolchains - You should see a
MinGWentry listed. If not, click the+button, selectMinGW, and point it to your MinGW-w64 installation directory (e.g.,C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0) - Make sure the
C compiler,C++ compiler, andDebuggerfields are populated correctly (they should point togcc.exe,g++.exe, andgdb.exerespectively)
Setting Up a GCC (MinGW-w64) CMake Project
This is straightforward since CLion works seamlessly with MinGW. Here's a minimal CMakeLists.txt to start with:
cmake_minimum_required(VERSION 3.10) project(MyGCCProject) # Set C++ standard (adjust as needed) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Add your source files add_executable(MyGCCProject main.cpp)
When you create a new project in CLion, select CMake as the project type, and choose the MinGW toolchain you confirmed earlier. CLion will generate a basic project structure, and you can build/run it directly—no extra flags needed here since MinGW is properly configured in PATH.
Setting Up a Clang CMake Project
Since you need to pass the -target x86_64-pc-mingw64 flag to Clang to avoid VC toolchain issues, we'll need to configure that in the CMakeLists.txt. Here's how:
Step 1: Configure CLion to Use Clang
- Go to
File > Settings > Build, Execution, Deployment > Toolchains - Click
+and selectClang - For the
C compiler, browse to your Clang installation'sclang.exe(e.g.,C:\Program Files\LLVM\bin\clang.exe) - For the
C++ compiler, selectclang++.exefrom the same directory - Set the debugger to use MinGW's
gdb.exe(since Clang on Windows doesn't come with its own debugger by default)
Step 2: Update CMakeLists.txt to Add the Target Flag
Modify your CMakeLists.txt to include the required compiler flag with a conditional check (so it only applies when using Clang):
cmake_minimum_required(VERSION 3.10) project(MyClangProject) # Set C++ standard set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Add the target flag for Clang to avoid VC toolchain conflicts if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target x86_64-pc-mingw64") endif() # Add your source files add_executable(MyClangProject main.cpp)
This way, you can switch between GCC and Clang toolchains in CLion without modifying the CMakeLists.txt every time.
Testing the Setup
Create a simple main.cpp to verify everything works:
#include <iostream> int main() { std::cout << "Hello from " << __VERSION__ << std::endl; return 0; }
- For GCC: Build and run—you should see output like
Hello from 7.3.0 - For Clang: Build and run—you should see output like
Hello from Clang 6.0.0
Troubleshooting Tips
- If CLion doesn't detect the toolchains automatically, restart CLion after ensuring the PATH entries are correct (sometimes the PATH doesn't update for running processes until restarted)
- If you get linker errors with Clang, make sure the MinGW libraries are in your PATH—Clang will use them when targeting MinGW
- If you need to build 32-bit binaries, add
-m32to the compiler flags and select the 32-bit MinGW toolchain in CLion (if you have it configured)
内容的提问来源于stack exchange,提问作者Accident2ly

