CMake 交叉编译
本文通过一个简单的示例,演示如何在 CMake 工程中添加交叉编译的配置,实现编译不同硬件平台的可执行文件。所有代码均可在 getiot/linux-c 仓库找到。
搭建工程
首先,编写一个最简单的 hello.c 文件:
hello.c
#include <stdio.h>
int main(void)
{
printf("Hello, GetIoT\n");
return 0;
}
创建一个 CMakeLists.txt 文件用于编译 hello.c 程序:
cmake_minimum_required(VERSION 3.1.3)
project(hello)
set( HELLO_SRCS hello.c)
add_executable(${PROJECT_NAME} ${HELLO_SRCS})
执行下面命令编译 x86-64 平台的可执行文件:
$ mkdir build
$ cd build
$ cmake .. && make
查看可执行文件描述:
$ file hello
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=5f92608723d5b497b773b56b366f4dada31c2318, for GNU/Linux 3.2.0, not stripped