前言
官方建议把用户新建的源代码文件都放在applications文件夹里,但是在做项目时,代码一般比较多,这个时候我们需要新建一个或多个文件夹来分类放置代码,本文就是介绍并实操一下“RT-thread 如何添加新的文件夹(模块)到工程”,本文以“imxrt1061-forlinx-OK1061-S”工程代码为例。本文在项目中添加hello文件夹,在hello文件夹中添加hello.c和hello.h,然后通过env工具和scons命令把hello文件夹及文件夹内的hello.c和hello.h文件添加到mdk5工程;
hello.c和hello.h代码如下:
/* file: hello.h */
#ifndef _HELLO_H_
#define _HELLO_H_
int hello_world(void);
#endif /* _HELLO_H_ */
/* file: hello.c */
#include <stdio.h>
#include <finsh.h>
#include <rtthread.h>
int hello_world(void)
{
rt_kprintf("Hello, world!\n");
return 0;
}
MSH_CMD_EXPORT(hello_world, Hello world!)
操作说明
第一步:在项目目录下,新建“hello”文件夹,把hello.c和hello.h放入hello文件夹中。
第二步:打开hello文件夹,新建SConscript文件并且编辑该文件,内容如下;
import rtconfig
from building import *
import os
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
# add for startup script
if rtconfig.PLATFORM in ['gcc']:
CPPDEFINES = ['__START=entry']
else:
CPPDEFINES = []
group = DefineGroup('Hello', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES=CPPDEFINES)
list = os.listdir(cwd)
for item in list:
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
group = group + SConscript(os.path.join(item, 'SConscript'))
Return('group')
(SConscript文件)
第三步:双击env.exe,打开cmd命令框;
第四步:通过cd命令进入项目目录:
第五步:输入scons --target=mdk5命令
第六步:打开mdk工程,可以看到hello文件夹和hello.c文件已经添加到工程里
进入项目目录