为什么CMake会忽略导出的CXX和CC环境变量? [英] Why does CMake ignore exported CXX and CC environment variables?

查看:1201
本文介绍了为什么CMake会忽略导出的CXX和CC环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样运行CMake(3.4.3),如 CMake常见问题解答

I am running a CMake (3.4.3) like this as explained in the CMake FAQ's:

export CC="cc_args.py $PWD/../bin/gcc"
export CXX="cc_args.py $PWD/../bin/g++"
cmake -DCMAKE_BUILD_TYPE=Debug ..

但是,当我打印 CMAKE_CXX_COMPILER CMAKE_C_COMPILER 时,它仍然指向<$ c中的系统默认编译器$ c> / usr / bin 。仅当我显式读取以下环境变量时,它才起作用:

However when I print CMAKE_CXX_COMPILER and CMAKE_C_COMPILER it still points to the system's default compilers in /usr/bin. It only works when I explicitly read-in the environment variables like this:

IF (NOT $ENV{CC} STREQUAL "")
  SET(CMAKE_C_COMPILER $ENV{CC})
ENDIF ()
IF (NOT $ENV{CXX} STREQUAL "")
  SET(CMAKE_CXX_COMPILER $ENV{CXX})
ENDIF ()

但是即使如此,建筑仍会失败,并显示以下消息:

But even then the building fails with this message:

/bin/sh: 1: /home/peterg/bin/cc_args.py /home/peterg/Code/build/../bin/g++: not found

但是我确定所有路径都是正确的,因为只执行了两个冒号按预期方式输出:

However I am certain that all paths are correct since executing just the path between the two colons outputs this as expected:

g++: fatal error: no input files
compilation terminated.



更新:



似乎正在编译进程不喜欢编译器路径中的空格。现在,我创建了两个脚本(一个用于GCC,一个用于CC),用于包装命令并传播参数,这似乎可行。但是似乎我仍然在做一些根本错误的事情,因为CMake也不会接受导出的 CC = proxy_script_cc.sh GCC = proxy_script_gcc.sh 本身没有空格的变量。

Update:

It seems the compiling process does not like spaces in the compiler paths. I've now created two scripts (one for GCC and one for CC) which wrap the commands and propagate the arguments and that seems to work. But it still seems I am doing something fundamentally wrong because CMake would also not accept the exported CC=proxy_script_cc.sh and GCC=proxy_script_gcc.sh variables without spaces by itself.

推荐答案

将我的评论变成答案

问题

我已为您提供了编码尝试,可以重现您的问题

I've given you code a try and could reproduce your problem

CMake Error at [...]/cmake-3.5/Modules/CMakeDetermineCXXCompiler.cmake:56 (message):
  Could not find compiler set in environment variable CXX:

  cc_args.py [... PWD ...]/../bin/g++.

如果我看着 CMakeDetermineCXXCompiler.cmake 代码和 get_filename_component() 文档,这只是意味着它没有在系统搜索路径中或相对于二进制输出目录中找不到 cc_args.py

解决方案

因此,当您给二进制输出目录提供完整路径或相对路径并使用诸如

So it does work when you give a full path or a relative path to your binary output dir with something like

export CC="../cc_args.py ../bin/gcc"
export CXX="../cc_args.py ../bin/g++"

替代

CMake确实允许定义启动脚本,例如与 CMAKE_< LANG> _COMPILER_LAUNCHER

CMake does allow to define "launcher scripts" e.g. with CMAKE_<LANG>_COMPILER_LAUNCHER

$ cmake -DCMAKE_BUILD_TYPE=Debug 
        -DCMAKE_C_COMPILER_LAUNCHER=../cc_args.py 
        -DCMAKE_CXX_COMPILER_LAUNCHER=../cc_args.py 
        ..

参考文献

  • How to Use CCache with CMake?
  • Save and reprint warnings for successfully-compiled files on subsequent builds?

这篇关于为什么CMake会忽略导出的CXX和CC环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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