[Eclipse IDE中有一个“ hello world”项目,应该根据ESP8266 RTOS SDK进行编译。
文件结构如下
我向其中添加了一个C ++类,并将其放入自己的文件夹中。这是类标题
#ifndef MAIN_BLINKER_BLINKER_H_
#define MAIN_BLINKER_BLINKER_H_
class Blinker {
public:
Blinker( int period );
int Period() const;
private:
int period_;
};
#endif /* MAIN_BLINKER_BLINKER_H_ */
和定义
#include "Blinker.h"
Blinker::Blinker( int period ) :
period_( period )
{}
int Blinker::Period() const {
return this->period_;
}
Main.cpp文件就是这样
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "blinker/Blinker.h"
extern "C" {
void app_main()
{
auto blnk = Blinker( 3000 );
int i = 0;
while ( 1 ) {
printf( "[%d] Hello beautiful world!\n", i );
i++;
vTaskDelay( blnk.Period() / portTICK_PERIOD_MS );
}
}
}
它可以编译,但是在最后阶段失败,因为链接器(或xtensa工具链中应该是链接器的)看不到Blinker方法的定义。这是我在构建日志中得到的
如果将类文件放在main.cpp
文件旁边,则构建成功。但是随着时间的流逝,将有成百上千个文件,并且没有任何分组,它将迅速变成难以处理的混乱。
或者,我可以将此类放在顶级components
文件夹中,并为它配备空的component.mk
文件。这也将使构建系统感到满意,但是它将迫使我使用丑陋的标头包含,例如../components/blinker/Blinker.h
,我想避免。
所以问题是如何使构建系统知道main
文件夹的子文件夹中的.c和.cpp文件?
尝试将blinker/Blinker.cpp
添加到CMakeLists.txt
。看一眼How to add new source file in CMakeLists.txt?