自学内容网 自学内容网

Raspberry Pi3B+之C/C++开发环境搭建

1. 源由

为了配合《Ardupilot开源飞控之FollowMe验证平台搭建》,以及VINS-Fusion对于图像和IMU时序的严格要求,配合uav_splitter增加一个uav_mixeragent部署在摄像头/飞控端。

agent本次将采用C/C++来实现,采用传统Makefile作为工程管理文件,便于后续的OpenIPC来做集成。

2. 环境搭建

工程开发先采用树莓派Raspberry Pi3B+作为目标板,搭建C语言开发环境并编写一个简单的工程结构来实现“Hello World”示例代码,可以按照以下步骤进行。

2.1 搭建C语言开发环境

在树莓派上,安装基本的开发工具包:

sudo apt update
sudo apt install build-essential git tree

注:其中git用于开源项目代码管控;tree更好的了解工程结构。

2.2 工程目录结构

使用以下的目录结构来组织工程文件:

$ tree .
.
├── include
├── LICENSE
├── main.c
├── Makefile
├── README.md
└── src
    └── main.c

2 directories, 5 files
  • src/ 目录用于存放C语言的源代码。
  • include/ 目录用于存放头文件(如果有的话)。
  • Makefile 用于自动化构建过程。
  • LICENSE 用于对于开源代码许可证,建议用GPLv3.
  • Readme.md 该文件采用了MarkDown的语言格式,非常流行的文本版本管理语言格式。

2.3 Makefile

Makefile 会定义如何编译和链接C代码。以下是一个简单的示例:

# Define the compiler
CC = gcc

# Define compiler options
CFLAGS = -Wall -Iinclude

# Define source directory and object directory
SRCDIR = src
OBJDIR = obj

# Define the target executable name
TARGET = helloworld

# Define source files and object files
SRCS = $(wildcard $(SRCDIR)/*.c)
OBJS = $(SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

# Default target
all: $(TARGET)

# Link the object files to create the executable
$(TARGET): $(OBJS)
$(CC) $(OBJS) -o $(TARGET)

# Compile source files into object files
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@

# Create the object file directory
$(OBJDIR):
mkdir -p $(OBJDIR)

# Clean up generated files
clean:
rm -rf $(OBJDIR) $(TARGET)

.PHONY: all clean

2.4 Demo (main.c)

src/ 目录下创建一个 main.c 文件,实现简单的Hello World程序:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

3. 测试工程

3.1 编译

通过以下命令编译并运行程序:

$ make        # Compile the program

3.2 运行

运行程序后,应该在终端看到:

$ ./helloworld  # Run the generated executable
Hello, World!

4. 总结

上述是一个Linux的C/C++应用最为基础的工程。

在此基础上,根据项目要求进行功能、特性的开发。

5. 参考资料

【1】Linux应用程序之Helloworld入门


原文地址:https://blog.csdn.net/lida2003/article/details/142693333

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!