- 1 year ago
cmake fails to pick up clang++ and always use visual studio instead on Windows
When CMake fails to pick up the clang++
compiler and defaults to Visual Studio on Windows, it's likely because CMake cannot locate the clang++
executable in the system PATH or the CMake cache is set to use Visual Studio by default. To resolve this issue, you can follow these steps:
Check Clang Installation:
First, verify that Clang is installed on your system and its bin directory is added to the system PATH. You can check by opening a command prompt and running:bashclang++ --version
If you get a version output, Clang is installed correctly and available in the PATH.
Specify Clang as Generator:
When generating the build files using CMake, you can explicitly specify the generator to use Clang. Use the-G
option followed by the generator name:bashcmake -S <path-to-source> -B <path-to-build> -G "Unix Makefiles" -DCMAKE_CXX_COMPILER=clang++
This command tells CMake to use the "Unix Makefiles" generator and set
clang++
as the C++ compiler.Clean CMake Cache:
If you previously ran CMake with the default Visual Studio generator, the configuration might have been cached. Clear the CMake cache by deleting the contents of the CMake build directory or use a new build directory to force CMake to reconfigure the project with the new generator.Use CMake-GUI:
If you prefer using CMake GUI, follow these steps:- Open CMake GUI and specify the source directory and build directory.
- Click on the "Configure" button and choose the "Unix Makefiles" generator.
- Set the
CMAKE_CXX_COMPILER
variable to the path ofclang++
.
Environment Variable:
You can also set theCC
andCXX
environment variables to specify the default C and C++ compilers to use during CMake configuration. Set them to the paths ofclang
andclang++
, respectively.bashset CC=clang
set CXX=clang++
cmake -S <path-to-source> -B <path-to-build>
By following these steps, CMake should now use clang++
as the C++ compiler instead of Visual Studio on Windows. If CMake still defaults to Visual Studio, double-check that clang++
is correctly installed and available in the system PATH. Also, ensure that you are running CMake from a fresh build directory to avoid any conflicts from the previous configuration.