美文网首页
Cmake指定编译器

Cmake指定编译器

作者: Domibaba | 来源:发表于2023-08-28 13:12 被阅读0次

默认情况下,Cmake会从系统中寻找合适的编译器来构建我们的程序,但是对于某些场景(例如交叉编译,在Linux上使用特定的编译器,编译出来的版本在Windows上运行),可能需要控制编译器相关的内容,例如:使用哪种编译器,编译器的版本、编译器支持的语言标准、编译选项、链接选项等。Cmake提供了一些方式让我们可以指定编译器及相关选项,本文主要介绍Cmake如何选择编译器以及我们如何指定特定的编译器。

本文使用的相关软件环境信息如下:

软件名称 软件版本
Linux操作系统 Ubuntu 22.04 LTS(X64)
cmake 3.22.1

一个简单示例

在开始介绍具体相关变量和函数之前,我们先通过Cmake构建出一个最简单的程序来看看默认情况下Cmake是怎么处理与编译器相关的内容。

新建一个cmake_compile目录,在该目录下新建CMakeLists.txtmain.cpp两个文件,文件内容分别如下:

  • main.cpp文件内容

    #include <iostream>
    int main(int argc, char** argv)
    {
        std::cout << "Hello world!" << std::endl;
        return 0;
    }
    
  • CMakeLists.txt文件内容

    PROJECT(cmake_compile)
    ADD_EXECUTABLE(main main.cpp)
    

cmake_compile目录下运行cmake .make VERBOSE=1,可以得到类似下面的输出:

  • 执行命令:cmake .

    -- The C compiler identification is GNU 11.4.0
    -- The CXX compiler identification is GNU 11.4.0
    ……
    -- Check for working C compiler: /usr/bin/cc - skipped
    ……
    -- Check for working CXX compiler: /usr/bin/c++ - skipped
    
  • 执行命令:make VERBOSE=1VERBOSE=1会将编译过程的具体信息输出出来,方便我们查看编译过程。

    ……
    [ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
    /usr/bin/c++    -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /XXX/cmake_compile/main.cpp
    [100%] Linking CXX executable main
    

构建完成后,在cmake_compile目录下会生成可执行文件main,执行./main即可输出打印“Hello world!”。

从上面的输出过程我们可以知道,Cmake默认选择了位于/usr/bin/c++的编译器,在本文所使用的系统上,这个文件就是一个软连接,最终指向的是g++,对于g++大家都很熟悉了,就不多介绍了。那默认情况下Cmake是如何选择编译器的呢?

默认编译器的选择

默认情况下,Cmake按照一定的顺序去系统中搜索一些已知的编译。

对于C编译器,会按照如下顺序去搜索:

cc, gcc, cl, bcc, xlc, clang

类似的,对于C++编译器,会按照如下顺序去搜索:

c++, g++, cl, bcc, clang++

根据操作系统的不同可能不一样,当在系统中找不到时候,就会提示错误。接下来做一些验证,看下Cmake默认的选择顺序是否如上所述。

首先,我们查看c++的位置,输入which c++可以看到它位于/usr/bin/c++

$ which c++
/usr/bin/c++

然后,我们将这个文件修改一下名字,让Cmake无法在系统中找到c++,输入sudo mv /usr/bin/c++ /usr/bin/back_c++(验证完成后记得恢复原样),然后在cmake_compile目录下运行cmake .,注意:Cmake只会在第一次构建的时候确认编译器,并将编译器的信息记录到缓存文件CMakeCache.txt,后续即使变更了编译器相关的环境/变量,也不会生效,需要先删除CMakeCache.txt文件后重新构建。输出如下:

-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done

可以看到,C++编译器目前使用的是/usr/bin/g++,同样,继续修改让系统找不到g++,输入sudo mv /usr/bin/g++ /usr/bin/back_g++,然后继续运行cmake .(每次执行前,记得先删除CMakeCache.txt文件,后文不再提示):

-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:2 (PROJECT):
  No CMAKE_CXX_COMPILER could be found.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!

出错了,系统找不到C++能用的编译器,因为当前我们系统中也没有clbccclang++等可用。可以执行命令sudo ln -s /usr/bin/g++-11 /usr/bin/cl创建一个cl,本质上还是链接到本系统的g++编译器上,执行cmake .看看效果:

-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/cl - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done

可以看到,Cmake在没有c++g++的情况下,成功的找到了cl。同样,执行sudo ln -s /usr/bin/g++-11 /usr/bin/bccsudo ln -s /usr/bin/g++-11 /usr/bin/clang++,执行cmake .,发现还是使用的cl,说明查找的顺序目前也是按照c++->g++->cl

我们继续删除cl,执行cmake .

-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/bcc - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done

输出信息表明找到的是bcc,查找的顺序目前也是按照c++->g++->cl->bcc。继续删除bcc,执行cmake .

-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done

输出信息表明,Cmake成功的找到clang++,表明Cmake默认查找编译器的顺序是c++->g++->cl->bcc->clang++

如何为Cmake指定编译器

上一小节讲述的是Cmake查找默认编译器的方法,有时候需要指定使用特定的编译器,有两种方法可以为Cmake指定特定的编译器:通过环境变量或Cmake编译器变量。

  • 通过环境变量指定编译器

    Cmake会查找环境变量CC(对应C编译器)和CXX(对应C++编译器),如果环境变量指定了值,会使用该值,该值的优先级高于Cmake默认编译器的选择。

    使用sudo ln -s /usr/bin/g++-11 /usr/bin/clang++模拟一个clang++编译器,命令行输入export CXX=/usr/bin/clang++临时设置环境变量CXX,然后执行cmake .(请先删除CMakeCache.txt再执行):

    -- The C compiler identification is GNU 11.4.0
    -- The CXX compiler identification is GNU 11.4.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /usr/bin/cc - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /usr/bin/clang++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    

    可以看到已经使用了CXX环境变量指定的编译器。

  • 通过编译器相关变量指定编译器

    Cmake也提供了CMAKE_C_COMPILER(指定C编译器)和CMAKE_CXX_COMPILER(指定C++编译器)两个内置的变量,用于设置指定的编译器。有两种方式可以通过该变量设置。优先级高于环境变量指定。

    方式一:命令行中通过-D指定,输入cmake -DCMAKE_CXX_COMPILER=/usr/bin/g++ .

    -- The C compiler identification is GNU 11.4.0
    -- The CXX compiler identification is GNU 11.4.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /usr/bin/cc - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /usr/bin/g++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    

    从输出可以看到,Cmake使用了命令行指定的编译器。

    方式二:CMakeLists.txt中使用CmakeSET()命令,但是要注意,设置编译器的命令必须在PROJECT()ENABLE_LANGUAGE()命令之前。

    步骤:

    使用sudo ln -s /usr/bin/g++-11 /usr/bin/clang++sudo ln -s /usr/bin/g++-11 /usr/bin/bcc模拟两个编译器。

    CXX环境变量通过export CXX=/usr/bin/g++设置。

    命令行使用cmake -DCMAKE_CXX_COMPILER=/usr/bin/bcc .

    CMakeLists.txt文件内容:

    CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
    SET(CMAKE_CXX_COMPILER /usr/bin/clang++)
    PROJECT(cmake_compile)
    

    执行结果:

    -- The C compiler identification is GNU 11.4.0
    -- The CXX compiler identification is GNU 11.4.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /usr/bin/cc - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /usr/bin/clang++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    

    从输出结果可以看到:最终使用的是SET()指定的值,也就是意味着优先级为:

SET() > 命令行-D指定 > Cmake默认选择

相关文章

网友评论

      本文标题:Cmake指定编译器

      本文链接:https://www.haomeiwen.com/subject/ntgvmdtx.html