Cmake和Catkin_make

Cmake和Catkin_make

嵌入式Linux开发

本次技术分享是个人在准备智能车竞赛时候的Linux嵌入式开发所学,也算是对自己的一个勉励,智能车也是本人目前比较感兴趣的一个方向,首先我想跟大家分享的就是最基础的编译部分。

前言:

在传统的linux工程管理用Makefile的比较多(Uboot里面也是大量使用Makefile来进行管理工程);在新型领域,比如物联网开发(特别是一些开源项目等),用Cmake的比较多。

1.安装

嵌入式Linux一般用ubuntu进行开发,这里打开terminal直接用apt安装即可

1
apt install cmake

2.例子

简单代码例子

main.c如下

1
2
3
4
5
6
7
8
#include <stdio.h>

int main(void)
{
printf("hello,world\n");
return 0;

}

比如说当前的目录是 /home/dev

那我们需要创建一个CMakeLists.txt,在终端中输入

1
touch CMakeLists.txt

这时候我们ls一下,当前目录下应该有

CMakeLists.txt main.c

这两个文件

CMakeLists.txt内容如下

1
2
3
4
5
cmake_minimum_required (VERSION 3.0)

project (test)

add_executable(main main.c)

1.表示最低需要cmake版本是3.0

2.表示当前项目名字是test

3.表示源文件是main.c 最终的elf文件是main

3.使用

在当前目录下,在终端中输入cmake .就可以生成makefile等文件,此时再输入make,即可生成elf可执行文件

makefile内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.10

# Default target executed when no arguments are given to make.
default_target: all

.PHONY : default_target

# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:

#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.
.SUFFIXES:

# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =

.SUFFIXES: .hpux_make_needs_suffix_list

# Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:

.PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake

# The command to remove a file.
RM = /usr/bin/cmake -E remove -f

# Escaping for special characters.
EQUALS = =

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/ucar/test

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/ucar/test

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache

.PHONY : rebuild_cache/fast

# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
/usr/bin/cmake-gui -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache

.PHONY : edit_cache/fast

# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/ucar/test/CMakeFiles /home/ucar/test/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/ucar/test/CMakeFiles 0
.PHONY : all

# The main clean target
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean

# The main clean target
clean/fast: clean

.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast

# clear depends
depend:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

#=============================================================================
# Target rules for targets named main

# Build rule for target.
main: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 main
.PHONY : main

# fast build rule for target.
main/fast:
$(MAKE) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/build
.PHONY : main/fast

main.o: main.c.o

.PHONY : main.o

# target to build an object file
main.c.o:
$(MAKE) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/main.c.o
.PHONY : main.c.o

main.i: main.c.i

.PHONY : main.i

# target to preprocess a source file
main.c.i:
$(MAKE) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/main.c.i
.PHONY : main.c.i

main.s: main.c.s

.PHONY : main.s

# target to generate assembly for a file
main.c.s:
$(MAKE) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/main.c.s
.PHONY : main.c.s

# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... rebuild_cache"
@echo "... main"
@echo "... edit_cache"
@echo "... main.o"
@echo "... main.i"
@echo "... main.s"
.PHONY : help

#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

4.多个源文件

文件目录如下:

|__test1.c

|__test1.h

test1.c内容如下:

1
2
3
4
5
6
7
#include <stdio.h>
#include "test1.h"

void func(int id)
{
printf("a=%d\n",id);
}

test1.h内容如下:

1
2
3
4
5
6
#ifndef _TEST1_H
#define _TEST1_H

void func(int id);

#endif /* _TEST1_H */

然后在main.c里面编写如下代码:

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include "test1.h"
int main(void)
{
func(6);
printf("hello,world\n");
return 0;

}

CMakeLists.txt的内容也要进行更改

1
2
3
4
5
cmake_minimum_required (VERSION 3.0)

project (main)

add_executable(main main.c test1.c)

重复cmake. 就可以看到是一样的效果


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!