【camke】cmake 生成 Makefile

文章目录

  • env
  • tree
  • files
  • create Makefile
  • make all
  • steps
  • 附件

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

你可能感兴趣的:(#,PM,makefile,cmake)