当前位置:网站首页>使用cmake交叉編譯helloworld

使用cmake交叉編譯helloworld

2022-06-13 05:24:00 藍牙先生

主機環境

cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
 

cmake版本

 cmake --version
cmake version 3.16.3
 

helloworld.c

[email protected]:/mnt/lab/cmake_helloworld$ cat helloworld.c
#include <stdio.h>

int main(int argc, char *argv[])
{
        printf("hello world\n");
        return 0;
}

 CMakeLists.txt

[email protected]:/mnt/lab/cmake_helloworld$ cat CMakeLists.txt
project(helloworld_project)

cmake_minimum_required(VERSION 3.16)

set (CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set (CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)

add_executable(helloworld helloworld.c)

執行cmake命令生成Makefile 

 [email protected]:/mnt/lab/cmake_helloworld$ cmake .
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/lab/cmake_helloworld

執行make命令

[email protected]:/mnt/lab/cmake_helloworld$ make
Scanning dependencies of target helloworld
[ 50%] Building C object CMakeFiles/helloworld.dir/helloworld.c.o
[100%] Linking C executable helloworld
[100%] Built target helloworld 

查看生成的helloworld格式

 [email protected]:/mnt/lab/cmake_helloworld$ file helloworld
helloworld: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=64931729ed98ba097ffafab33a540e307404aaf9, for GNU/Linux 3.7.0, not stripped

交叉編譯成功 

原网站

版权声明
本文为[藍牙先生]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130522577998.html