【camke】cmake 生成 Makefile

env

ubuntu1804
cmake:3.22.1


tree

.
├── create_makefile.sh
└── source
    ├── CMakeLists.txt
    ├── include
    │   └── common.h
    └── src
        ├── common.c
        └── main.c

3 directories, 5 files

files

create_camke_files.sh :

#!/usr/bin/env bash

# Type 1
cmake -G "Unix Makefiles" -S ./source/ -B ./build/

# Type 2 : toolchain & build_type
#cmake -G "Unix Makefiles" -S ./source/ -B ./build/ \
#    -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ \
#    -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc \
#    -DCMAKE_BUILD_TYPE:STRING=RELEASE
#cmake -G "Unix Makefiles" -S ./source/ -B ./build/ \
#    -DCMAKE_CXX_COMPILER:FILEPATH=/opt/toolchains/crosstools-aarch64-gcc-10.3-linux-4.19-glibc-2.32-binutils-2.36.1/bin/aarch64-linux-g++ \
#    -DCMAKE_C_COMPILER:FILEPATH=/opt/toolchains/crosstools-aarch64-gcc-10.3-linux-4.19-glibc-2.32-binutils-2.36.1/bin/aarch64-linux-gcc \
#    -DCMAKE_BUILD_TYPE:STRING=RELEASE

CMakeLists.txt :

# 10. cmake needs this line
cmake_minimum_required(VERSION 3.22)

# 20. cmake nees this line
# define project name
project(__pleace_and_love__)

# 30. add "src" path
aux_source_directory(./src DIR_SRCS)

# 40. add "include" path
include_directories(./include)

# 50. add sub config

# 60. add sys link lib
#link_libraries(xxxx pthread)

# 90. target name
# specify output path
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ../source/bin)
add_executable(main ${DIR_SRCS})
# add link lib
#target_link_libraries(main xxxxx)


create Makefile

./create_makefile.sh

log :

-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 8.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xxxxxxx/build

Remark:

  1. cmake生成的文件指定到./build/文件夹下,CMakeLists.txt指定使用./source/下文件
    1.1 Makefile也在./build/文件夹下
  2. 要指定toolchain,修改shell,参考其中Type 2 的配置

make all

cd ./build/
make all

log :

[ 33%] Building C object CMakeFiles/main.dir/src/common.c.o
[ 66%] Building C object CMakeFiles/main.dir/src/main.c.o
[100%] Linking C executable main
[100%] Built target main

steps

  1. 创建工程,如“tree”中展示的一样
  2. 创建shell脚本,copy paste
  3. 创建CMakeLists.txt配置问价,copy paste
  4. 执行shell,生成build/,包含了Makefile等文件
    4.1 在build/下make ,生成可执行文件
    4.2 Makefile 在该文件夹下。

附件

工程打包下载:https://download.csdn.net/download/yujianliam/85707232

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过得精彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值