【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

CMake 生成 Makefile 的过程分为两个步骤: 1. 编写 CMakeLists.txt 文件 CMakeLists.txt 文件是 CMake 的配置文件,用于描述工程的源文件、编译选项、链接选项等信息。你需要在 CMakeLists.txt 文件中定义工程名称、源文件、头文件路径、库文件路径等信息。 以下是一个简单的 CMakeLists.txt 文件示例: ``` cmake_minimum_required(VERSION 3.10) project(MyProject) set(CMAKE_CXX_STANDARD 11) include_directories(include) add_executable(MyProgram src/main.cpp src/func.cpp) target_link_libraries(MyProgram m) ``` 在这个 CMakeLists.txt 文件中,首先指定了 CMake 的最低版本要求。然后定义了工程名称为 MyProject,并设置了 C++ 标准为 C++11。接着指定了头文件路径为 include 目录。然后定义了 MyProgram 目标,该目标依赖于 src/main.cpp 和 src/func.cpp 两个源文件,用于生成可执行文件 MyProgram。最后指定了链接库为数学库 libm。 2. 生成 Makefile 文件 在编写好 CMakeLists.txt 文件后,你可以使用 cmake 命令来生成 Makefile 文件。在命令行中进入工程目录,执行以下命令: ``` cmake -S . -B build ``` 其中,-S 参数指定了 CMakeLists.txt 文件所在的目录,-B 参数指定了构建目录。上述命令表示将 CMakeLists.txt 文件所在的当前目录作为源目录,将 build 目录作为构建目录。 执行完上述命令后,CMake生成 Makefile 文件并保存到 build 目录中。你可以进入 build 目录,执行 make 命令来编译工程。 ``` cd build make ``` 这样,就可以使用 CMake 生成 Makefile 文件,并编译生成工程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

过得精彩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值