CMake: How to specify the compiler in CMakeLists.txt.
28 Nov 2022Setting the compiler for a CMake project is a couple lines in the CMakeLists.txt file, and they have to go in a very specific place – before the project command.
set(CMAKE_C_COMPILER "gcc-4.2")
set(CMAKE_CXX_COMPILER "/usr/bin/g++-4.2")
project("YourProjectName")
CMake-ians will likely object to this method, since there are other command line methods to set the compiler. When I am debugging something, and particularly when that something depends on a specific compiler, I don’t want to have to remember all the details – I want the details baked into the project.
Troubleshooting with cmake: delete everything in the build directory and try running CMake configuration and generation again.
More details, GitLab about methods for setting the compiler.