且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使CMake在系统PATH上使用默认编译器?

更新时间:2023-12-03 23:40:22

As is already written in the answer to the other question, CMake prefers the generic compiler names cc and c++ when searching for the C and C++ compilers. These probably refer to GNU version 4.1 compilers on your system.

无论如何,要强制CMake在系统路径上使用默认编译器,请将以下代码添加到最外面的CMakeLists.txt的开头.

Anyway, to force CMake to use the default compilers on the system path, add the following code to the beginning of your outermost CMakeLists.txt.

find_program(CMAKE_C_COMPILER NAMES $ENV{CC} gcc PATHS ENV PATH NO_DEFAULT_PATH)
find_program(CMAKE_CXX_COMPILER NAMES $ENV{CXX} g++ PATHS ENV PATH NO_DEFAULT_PATH)
...
project (Foo C CXX)

find_program调用必须在调用projectenable_language之前发生.

The find_program calls must occur before the call to project or enable_language.