CMake: How to specify the compiler in CMakeLists.txt.

c c-plus-plus cmake

Setting 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")


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.

© Amy Tabb 2018 - 2023. All rights reserved. The contents of this site reflect my personal perspectives and not those of any other entity.