c · 2021-12-22 0

make、makefile与cmake

一、make

1.构建

代码变成可执行文件,叫做编译(compile);先编译这个,还是先编译哪个(即编译的安排),叫做构建(build)。

Make 是常用的构建工具。实际上,任何只要某个文件有变化,就要重新构建的项目,都可以用Make构建。

2.语法

<target> : <prerequisites>
[tab]  <commands>

规则语法由 "目标"(target)、"前置条件"(prerequisites)、命令(commands)三部分组成

3.栗子

创建文件 rules.txt

a.txt: b.txt c.txt
    cat b.txt c.txt > a.txt

.PHONY: clean

clean:
    rm a.txt

生成 a.txt 文件命令:make -f rules.txt a.txtmake --file=rules.txt a.txtmake -f rules.txt

删除 a.txt 文件命令:make -f rules.txt clean

.PHONY clean表明clean是"伪目标"(phony target)

声明clean是"伪目标"之后,make就不会去检查是否存在一个叫做clean的文件,而是每次运行都执行对应的命令。

  • 如果规则是Makefile 文件,可以不用 -f 指定文件
  • 如果Make命令运行时没有指定目标,默认会执行Makefile文件的第一个目标

二、makefile

有文件目录

| - - makefile
    | - - include
        | - - head.h
    | - - src
        | - - add.cpp
        | - - sub.cpp
        | - - main.cpp
        | - - Makefile

1.head.h

/* head.h  */
int add(int a, int b);
int sub(int a, int b);

2.add.cpp

/* add.cpp  */
int add(int a, int b)
{
    return a + b;
}

3.sub.cpp

/* sub.cpp  */
int sub(int a, int b)
{
    return a - b;
}

4.main.cpp

/* main.cpp  */
#include <stdio.h>
#include "head.h"

int main()
{
    printf("sum = %d n", add(10, 3));
    printf("sub = %d n", sub(10, 3));
    return 0;
}

5.Makefile

main: main.o add.o sub.o
    gcc add.o sub.o main.o -o main

main.o: main.cpp
    gcc -c main.cpp -I ../include -o main.o

add.o: add.cpp
    gcc -c add.cpp -o add.o

sub.o: sub.cpp
    gcc -c sub.cpp -o sub.o

.PHONY: clean

clean:
    rm -f *.o

5.构建

Makefile所在文件的目录下,执行make

执行生成的 main 文件

三、cmake

cmake是一个跨平台makefile工具,可以生成工程文件

有文件目录

zxm@zxm-pc:~$ tree helloword/
helloword/
├── CMakeLists.txt
├── main
│   ├── CMakeLists.txt
│   ├── include
│   │   └── head.h
│   └── src
│       └── main.cpp
└── math
    ├── CMakeLists.txt
    ├── include
    │   └── head.h
    └── src
        ├── add.cpp
        └── sub.cpp

6 directories, 8 files

1.根CMakeLists.txt文件

# 显示信息
message(STATUS "- - - - - - Build in helloworld folder - - - - - -")

cmake_minimum_required(VERSION 3.6)

project(myhello)

add_subdirectory(./math) # 指定生成目录

add_subdirectory(./main)  # 指定生成目录

2.math目录

1.head.h

/* head.h  */
int add(int a, int b);
int sub(int a, int b);

2.add.cpp

/* add.cpp  */
int add(int a, int b)
{
    return a + b;
}

3.sub.cpp

/* sub.cpp  */
int sub(int a, int b)
{
    return a - b;
}

4.CMakeLists.txt

# 显示信息
message(STATUS "- - - - - - Build in math folder - - - - - -")

cmake_minimum_required(VERSION 3.6)

project(mymath)

# 包含头文件
include_directories(include)

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(src DIR_SRCS)

# 生成库文件
add_library(mymath SHARED ${DIR_SRCS})

3.main目录

1.head.h

/* head.h  */
int add(int a, int b);
int sub(int a, int b);

2.main.cpp

/* main.cpp  */
#include <stdio.h>
#include "head.h"

int main()
{
    printf("sum = %d n", add(10, 3));
    printf("sub = %d n", sub(10, 3));
    return 0;
}

3.CMakeLists.txt

# 显示信息
message(STATUS "- - - - - - Build in main folder - - - - - -")

cmake_minimum_required(VERSION 3.6)

project(mymain)

# 包含头文件
include_directories(include)

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(src DIR_SRCS)

# 生成可执行文件
add_executable(mymain ${DIR_SRCS})

target_link_libraries(mymain ${CMAKE_BINARY_DIR}/math/libmymath.so)

4.编译

执行 cmake 命令生成 Makefile 文件;执行make命令生成可执行文件

执行命令及生成的文件:

zxm@zxm-pc:~/helloword$ cmake .
-- - - - - - - Build in helloworld folder - - - - - -
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.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
-- - - - - - - Build in math folder - - - - - -
-- - - - - - - Build in main folder - - - - - -
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zxm/helloword
zxm@zxm-pc:~/helloword$ tree
.
├── CMakeCache.txt
├── CMakeFiles
│   ├── 3.22.1
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   ├── CMakeCCompilerId.c
│   │   │   └── tmp
│   │   └── CompilerIdCXX
│   │       ├── a.out
│   │       ├── CMakeCXXCompilerId.cpp
│   │       └── tmp
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── Makefile2
│   ├── Makefile.cmake
│   ├── progress.marks
│   └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main
│   ├── CMakeFiles
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── mymain.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── compiler_depend.make
│   │   │   ├── compiler_depend.ts
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   ├── progress.make
│   │   │   └── src
│   │   └── progress.marks
│   ├── cmake_install.cmake
│   ├── CMakeLists.txt
│   ├── include
│   │   └── head.h
│   ├── Makefile
│   └── src
│       └── main.cpp
├── Makefile
└── math
    ├── CMakeFiles
    │   ├── CMakeDirectoryInformation.cmake
    │   ├── mymath.dir
    │   │   ├── build.make
    │   │   ├── cmake_clean.cmake
    │   │   ├── compiler_depend.make
    │   │   ├── compiler_depend.ts
    │   │   ├── DependInfo.cmake
    │   │   ├── depend.make
    │   │   ├── flags.make
    │   │   ├── link.txt
    │   │   ├── progress.make
    │   │   └── src
    │   └── progress.marks
    ├── cmake_install.cmake
    ├── CMakeLists.txt
    ├── include
    │   └── head.h
    ├── Makefile
    └── src
        ├── add.cpp
        └── sub.cpp

19 directories, 53 files
zxm@zxm-pc:~/helloword$ make
[ 20%] Building CXX object math/CMakeFiles/mymath.dir/src/add.cpp.o
[ 40%] Building CXX object math/CMakeFiles/mymath.dir/src/sub.cpp.o
[ 60%] Linking CXX shared library libmymath.so
[ 60%] Built target mymath
[ 80%] Building CXX object main/CMakeFiles/mymain.dir/src/main.cpp.o
[100%] Linking CXX executable mymain
[100%] Built target mymain
zxm@zxm-pc:~/helloword$ tree .
.
├── CMakeCache.txt
├── CMakeFiles
│   ├── 3.22.1
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   ├── CMakeCCompilerId.c
│   │   │   └── tmp
│   │   └── CompilerIdCXX
│   │       ├── a.out
│   │       ├── CMakeCXXCompilerId.cpp
│   │       └── tmp
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── Makefile2
│   ├── Makefile.cmake
│   ├── progress.marks
│   └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main
│   ├── CMakeFiles
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── mymain.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── compiler_depend.make
│   │   │   ├── compiler_depend.ts
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   ├── progress.make
│   │   │   └── src
│   │   │       ├── main.cpp.o
│   │   │       └── main.cpp.o.d
│   │   └── progress.marks
│   ├── cmake_install.cmake
│   ├── CMakeLists.txt
│   ├── include
│   │   └── head.h
│   ├── Makefile
│   ├── mymain
│   └── src
│       └── main.cpp
├── Makefile
└── math
    ├── CMakeFiles
    │   ├── CMakeDirectoryInformation.cmake
    │   ├── mymath.dir
    │   │   ├── build.make
    │   │   ├── cmake_clean.cmake
    │   │   ├── compiler_depend.make
    │   │   ├── compiler_depend.ts
    │   │   ├── DependInfo.cmake
    │   │   ├── depend.make
    │   │   ├── flags.make
    │   │   ├── link.txt
    │   │   ├── progress.make
    │   │   └── src
    │   │       ├── add.cpp.o
    │   │       ├── add.cpp.o.d
    │   │       ├── sub.cpp.o
    │   │       └── sub.cpp.o.d
    │   └── progress.marks
    ├── cmake_install.cmake
    ├── CMakeLists.txt
    ├── include
    │   └── head.h
    ├── libmymath.so
    ├── Makefile
    └── src
        ├── add.cpp
        └── sub.cpp

19 directories, 61 files
zxm@zxm-pc:~/helloword$ ./main/mymain 
sum = 13 
sub = 7