个人随笔 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
前一段时间做Linux下的编译,遇到一个奇怪的问题,明明设定了使用C Compiler, CXX Copmiler,但是cmake时还是报错,找了clang++编译器,觉得很奇怪。
1. 出问题的现象
CMakeLists.txt中的内容
cmake_minimum_required(VERSION 3.1)
project(TestProject VERSION 1.1.9 LANGUAGES C CXX)
SET(CMAKE_C_COMPILER "/usr/local/bin/gcc")
SET(CMAKE_CXX_COMPILER "/usr/local/bin/g++")
...
cmake时报错:
$ cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is Clang 3.4.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/clang++
-- Check for working CXX compiler: /usr/bin/clang++ -- broken
CMake Error at /usr/share/cmake3/Modules/CMakeTestCXXCompiler.cmake:53 (message):
The C++ compiler
"/usr/bin/clang++"
is not able to compile a simple test program.
2. 出问题的解决
百思不得其解,为什么设置的编译器不生效呢,怎么找到clang++来编译,哪边还需要设置吗?
后来偶然尝试,把Set编译器的语句放在project之前,问题解决了。
修改后CMakeLists.txt内容
cmake_minimum_required(VERSION 3.1)
SET(CMAKE_C_COMPILER "/usr/local/bin/gcc")
SET(CMAKE_CXX_COMPILER "/usr/local/bin/g++")
project(TestProject VERSION 1.1.9 LANGUAGES C CXX)
...
3. 问题回顾
基于这个问题来分析看:
对于cmake来说,它所支持的基础语法,像project语法,都是对Compiler有依赖的,需要提前设置,才能让project运行时使用。
对于cmake的语法,cmake_minimum_required,从测试来看,是不依赖compiler的,这个可能是为数不多,不依赖设定的compiler的语句。
个人随笔 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)