【问题标题】:CMake cross compile - separate invocation per platform?CMake交叉编译 - 每个平台单独调用?
【发布时间】:2016-09-20 02:22:59
【问题描述】:

我是 CMake 的新手,我用它来交叉编译。

我在 Debian 下一起使用 clang 和 wclang,分别为 Linux 和 Windows 编译。

我的问题 - 因为我是 CMake 的交叉编译过程的新手 - 我是:

(a) 在 CMakeLists.txt 期间的某处编译器之间切换,

(b) 只运行一次cmake,但每个平台都运行make install,或者

(c) 为每个平台运行一次cmakemake install,例如

$ export CC=/usr/bin/clang $ cmake .. $ make install

$ export CC=/usr/bin/wclang $ cmake .. $ make install

(等等)

?

【问题讨论】:

    标签: cmake clang cross-compiling


    【解决方案1】:

    C),每个平台单独调用。我还建议在构建之间清除二进制目录,或者如果您想保留构建,则为每个构建使用单独的目录。

    设置通常在工具链文件中完成,而不是命令行(为了重现性):

    $ cmake --help-variable CMAKE_TOOLCHAIN_FILE
    CMAKE_TOOLCHAIN_FILE
    --------------------
    
    Path to toolchain file supplied to ``cmake(1)``.
    
    This variable is specified on the command line when cross-compiling with CMake.
    It is the path to a file which is read early in the CMake run and which specifies
    locations for compilers and toolchain utilities, and other target platform and
    compiler related information.
    

    一个简单的工具链文件可能如下所示:

    # Name of the target operating system
    set( CMAKE_SYSTEM_NAME Windows )
    
    # Which compilers to use
    find_program( CMAKE_C_COMPILER   NAMES /opt/mxe/usr/bin/x86_64-w64-mingw32.static-gcc )
    find_program( CMAKE_CXX_COMPILER NAMES x86_64-w64-mingw32.static-g++ )
    find_program( CMAKE_RC_COMPILER  NAMES x86_64-w64-mingw32.static-windres )
    
    # Where to look for resources
    set( CMAKE_FIND_ROOT_PATH /opt/mxe/usr/x86_64-w64-mingw32.static/ )
    
    # Adjust find_*() behavior:
    # Headers and libs from the target environment,
    # programs from the host environment.
    set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
    set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
    set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
    

    一些交叉编译环境,例如MXE,带有预制的工具链文件和调用它们的包装器。 (特别是 MXE,您运行 i686-w64-mingw32.static-cmake 而不是标准的 cmake 来配置您的构建。)

    【讨论】:

    • 这种方法 (c) 是否需要为正在构建的每个平台使用单独的目标名称?否则,CMake 只生成一个(一组)makefile,而不是为每个平台生成一个;那时我不知道如何区分构建。
    • @ArcaneEngineer:我不明白你的问题。您创建一个目录,例如project-Linux,并在其中为 Linux 目标构建您的项目。然后你创建一个 不同的 目录,例如project-Windows,并在其中为 Windows 目标构建您的项目。区分构建应该很容易......?如果您cpack 他们,您甚至可以将系统名称作为包文件名的一部分...
    • 啊哈,好的。到目前为止,我使用单个文件夹只是为了让 CMake 工作 per se (您的回答帮助我做到了)。既然如此,我可能应该按照您的建议区分文件夹。再次感谢。
    • @ArcaneEngineer:我仍然不确定我是否能遵循您的思路。标准 CMake 程序是拥有 一个 源代码树,它不会在构建过程中 被触及或添加任何文件。您在单独的目录/目录中进行构建(或多个构建)。
    • @ArcaneEngineer:我可以大胆地宣传JAWS,一个现成的CMake配置框架吗?它还没有完成,但我认为它可以很好地说明 CMake 可以做什么。
    【解决方案2】:

    应为每个目标重复所有步骤(cmake - make - install)。也就是说,您的列表中的“c”。


    一些开发环境,例如Visual Studio,支持多重配置,即项目可以配置一次,每个目标配置只需要构建。这样的项目可以在 Debug 和 Release 模式下编译,或者为 x86 和 x86-64 编译。

    但 Windows 和 Linux 目标在任何情况下都需要不同的配置步骤(cmake 运行)。

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2011-08-24
      • 1970-01-01
      • 2017-05-08
      相关资源
      最近更新 更多