这是从cmake官网上摘录下来的交叉编译教程,详细解释了各个变量的作用和使用方法。
CMake Cross Compiling
Cross compiling is supported by CMake starting with version 2.6.0.
Cross compiling means that the software is built for a different system than the one which does the build. This means
- CMake cannot autodetect the target system
- Usually the executables don't run on the build host
- The build process has to use a different set of include files and libraries for building, i.e. not the native ones
Contents
[hide]- 1 Setting up the system and toolchain
- 2 Searching and finding external software
- 3 The toolchain file
- 4 System introspection
- 5 Using executables in the build created during the build
- 6 Cross compilation for Windows CE
- 7 Information how to set up various cross compiling toolchains
- 8 How to cross compile specific projects
- 9 FAQ/Potential Problems
Setting up the system and toolchain
When cross compiling, CMake cannot guess the target system, so you have to preset some CMake variables, e.g. using a toolchain file. The following variables have to be preset:
-
CMAKE_SYSTEM_NAME
- this one is mandatory, it is the name of the target system, i.e. the same as CMAKE_SYSTEM_NAME would have if CMake would run on the target system. Typical examples are "Linux" and "Windows". This variable is used for constructing the file names of the platform files like Linux.cmake or Windows-gcc.cmake. If your target is an embedded system without OS set CMAKE_SYSTEM_NAME to "Generic". If CMAKE_SYSTEM_NAME is preset, the CMake variable CMAKE_CROSSCOMPILING is automatically set to TRUE, so this can be used for testing in the CMake files. CMAKE_SYSTEM_VERSION
- optional, version of your target system, not used very much. CMAKE_SYSTEM_PROCESSOR
- optional, processor (or hardware) of the target system. This variable is not used very much except for one purpose, it is used to load a CMAKE_SYSTEM_NAME-compiler-CMAKE_SYSTEM_PROCESSOR.cmake file, which can be used to modify settings like compiler flags etc. for the target. You probably only have to set this one if you are using a cross compiler where every target hardware needs special build settings.
Since CMake cannot guess the target system, it also cannot guess which compiler it should use, so you have to preset this too:
-
CMAKE_C_COMPILER