CMake交叉编译-每个平台单独调用? [英] CMake cross compile - separate invocation per platform?

查看:290
本文介绍了CMake交叉编译-每个平台单独调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CMake的新手,我正在使用它进行交叉编译。



我正在使用clang和 wclang 一起在Debian下分别针对Linux和Windows进行编译。



我的问题-因为我是CMake交叉编译过程的新手,所以可以这样做吗?



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



(b)仅运行 cmake 一次,但对于每个平台 make install ,或



(c)运行 cmake make install 一次每个平台,例如



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



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



(等等)



解决方案

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



设置通常在工具链文件,而不是命令行(出于可复制性):

  $ cmake --help-变量CMAKE_TOOLCHAIN_FILE 
CMAKE_TOOLCHAIN_FILE
--------------------

提供给``cmake(1 )。

与CMake交叉编译时,在命令行上指定此变量。
这是在CMake运行中早期读取的文件的路径,并指定
用于编译器和工具链实用程序的位置,以及其他目标平台和
编译器相关的信息。

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

 #目标操作系统的名称
set(CMAKE_SYSTEM_NAME Windows)

#哪些编译器使用
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-ming-x static-windres)

#在哪里寻找资源
set(CMAKE_FIND_ROOT_PATH /opt/mxe/usr/x86_64-w64-mingw32.static/)

#调整find _ *()行为:
#目标环境中的标头和库,
#主机环境中的程序。
设置(从未CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
设置(仅限CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
设置(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)

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


I'm new to CMake, which I'm using to cross-compile.

I'm using clang and wclang together, under Debian, to compile for Linux and Windows respectively.

My question - since I'm new to CMake's cross compile process - is do I:

(a) switch between compilers somewhere during CMakeLists.txt,

(b) run cmake only once, but make install for each platform, or

(c) run cmake and make install once for each platform, e.g.

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

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

(etc.)

?

解决方案

C), separate invocation per platform. I would also recommend clearing the binary directory between builds, or using a separate directory for each build if you want to preserve the build(s).

Settings are usually done in a toolchain file, not the command line (for reproducability):

$ 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.

A simple toolchain file can look like this:

# 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 )

Some cross-compiling environments, like MXE, come with pre-made toolchain files and wrappers calling them. (MXE in particular, you run i686-w64-mingw32.static-cmake instead of standard cmake to configure your build.)

这篇关于CMake交叉编译-每个平台单独调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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