如何使CMake在系统PATH上使用默认编译器? [英] How to get CMake to use the default compiler on system PATH?

查看:502
本文介绍了如何使CMake在系统PATH上使用默认编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里是相同的问题和答案. 问题是答案似乎是错误的(实际上不是所提问题的答案).我可以再问一个问题吗? 问题:

There is the same question and the answer. The problem is that the answer seems to be wrong (actually is not the answer to the asked question). Can I re-ask the question? The problem:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ whereis gcc
cc: /usr/bin/gcc /usr/lib/gcc /usr/local/bin/gcc /usr/libexec/gcc
$ which gcc
/usr/local/bin/gcc
$ /usr/bin/gcc -v
gcc version 4.1.2
$ /usr/local/bin/gcc -v
gcc version 4.8.4
$ gcc -v
gcc version 4.8.4
$ cmake .
-- The C compiler identification is GNU 4.1.2
...
...

本地GCC版本(如果为4.8.4)和系统的默认版本为"4.1.2".所有其他工具链均遵循PATH环境变量,并使用本地(较新的)GCC版本.除CMAKE以外的所有内容. 设置CC并不是一个好主意,因为可能还会使用其他二进制工具. 在脚本的开头设置CMAKE_PROGRAM_PATH和CMAKE_PREFIX_PATH不会帮助检测编译器.
有什么办法强迫CMAKE遵守PATH变量?

The local GCC version if 4.8.4 and the system's default version is '4.1.2'. All other tool-chains respects the PATH environment variable and use the local (newer) GCC version. All except of CMAKE. Setting CC is not a good idea, because there might be other binary tools which could also be used. Setting CMAKE_PROGRAM_PATH and CMAKE_PREFIX_PATH in the beginning of the script doesn't help with detection of the compilers.
Is there any way to force CMAKE to respect the PATH variable?

推荐答案

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.

这篇关于如何使CMake在系统PATH上使用默认编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆