ESP32(Espressif-IDE)添加外部(.c .h)文件 (error:undefined reference to)

本文介绍如何在Espressif-IDE中为ESP32项目添加外部文件,包括myGPIO和myLED两个模块的具体实现步骤,并详细解释了CMakeLists.txt文件的配置方法。

0x00:前言

笔记文件

用习惯了keil和iar之类的开发软件,一下子转换到ESP32是特别的不习惯,这里使用的开发环境是乐鑫官方推出的Espressif-IDE,也是刚学折腾ESP32,刚开始就遇到难题了,按照以往的习惯,一般开发大一点的项目工程都是需要将不同的功能分开不同的文件写,一般都不会全写在main文件里。

这里主要就是记录一下Espressif-IDE添加外部文件的过程

可能这个过程中也是有错误的地方。具体我也是网上找了很久的方法,最后自己总结一下。主要自己做为笔记记录,具体原理呢,我也是一知半解,我也没有具体去学习CMake,我也是照葫芦画瓢,能用!

这里通过一个案例来说明整个操作流程,

设计一个程序,分别包含myGPIO和myLED两个文件夹,的程序

myGPIO文件夹包含:myGPIO.c     myGPIO.h      用于初始化 IO

myLED文件夹包含:  myLED.c       myLED.h        用于闪烁LED程序

0x01:向工程添加文件

这里以官方给出的  hello_world  工程为模板添加自己写的文件,

1、新建myDrives

2、myDrives 文件夹下添加 myGPIO文件夹和 myLED文件夹

 

 

 3、myGPIO

myGPIO.c文件

#include "myGPIO.h"
#include "myLED.h"

void myGPIO_Init(void )
{
	gpio_set_direction(GPIO_NUM_1,GPIO_MODE_OUTPUT);
}

myGPIO.文件

#ifndef MYDRIVES_MYGPIO_MYGPIO_H_
#define MYDRIVES_MYGPIO_MYGPIO_H_

void myGPIO_Init(void );

#endif /* MYDRIVES_MYGPIO_MYGPIO_H_ */

 4、myGPIO

myLED.c

#include "myLED.h"
#include "myGPIO.h"

void myLED_Init(void )
{
	myGPIO_Init();
}
void myLED_Function(void )
{
	myLED_H;
	printf("myLED_H\n");
	vTaskDelay(500);
	myLED_L;
	printf("myLED_L\n");
	vTaskDelay(500);
}

 myLED.h

#ifndef MYDRIVES_MYLED_MYLED_H_
#define MYDRIVES_MYLED_MYLED_H_

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

#define myLED_H gpio_set_level(GPIO_NUM_1,1)
#define myLED_L gpio_set_level(GPIO_NUM_1,0)

void myLED_Init(void );
void myLED_Function(void );

#endif /* MYDRIVES_MYLED_MYLED_H_ */

 4、main 主函数修改

#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "myGPIO.h"
#include "myLED.h"

void app_main(void)
{
    printf("Hello world!\n");
    myLED_Init();

    while(1)
    {
    	myLED_Function();

    }
}

 0x02:修改CMakeLists.txt文件

这里只修改 main 文件夹下的CMakeLists.txt文件

 

 

set(src
"hello_world_main.c"
../myDrives/myGPIO/myGPIO.c 
../myDrives/myLED/myLED.c 

)

set(inc
"."
../myDrives/myGPIO 
../myDrives/myLED
)


idf_component_register(
SRCS ${src}
INCLUDE_DIRS ${inc})

  0x03:编译运行

 

 

 

 

 

 

 

c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\arduino\sketches\DB844BFC4EE8887A797F0FFD059BB81E\sketch\sketch_sep18a.ino.cpp.o:(.literal._Z5setupv+0x38): undefined reference to `initPins()' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\arduino\sketches\DB844BFC4EE8887A797F0FFD059BB81E\sketch\sketch_sep18a.ino.cpp.o:(.literal._Z5setupv+0x3c): undefined reference to `initPWM()' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\arduino\sketches\DB844BFC4EE8887A797F0FFD059BB81E\sketch\sketch_sep18a.ino.cpp.o:(.literal._Z5setupv+0x40): undefined reference to `testAllMotors()' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\arduino\sketches\DB844BFC4EE8887A797F0FFD059BB81E\sketch\sketch_sep18a.ino.cpp.o:(.literal._Z4loopv+0x40): undefined reference to `readInfraredSensors()' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\arduino\sketches\DB844BFC4EE8887A797F0FFD059BB81E\sketch\sketch_sep18a.ino.cpp.o:(.literal._Z4loopv+0x44): undefined reference to `setMotorSpeed(int, int)' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\arduino\sketches\DB844BFC4EE8887A797F0FFD059BB81E\sketch\sketch_sep18a.ino.cpp.o:(.literal._Z4loopv+0x48): undefined reference to `printDebugInfo(int, int, int)' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\arduino\sketches\DB844BFC4EE8887A797F0FFD059BB81E\sketch\sketch_sep18a.ino.cpp.o: in function `setup()': C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:52: undefined reference to `initPins()' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:52: undefined reference to `initPWM()' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:67: undefined reference to `testAllMotors()' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\arduino\sketches\DB844BFC4EE8887A797F0FFD059BB81E\sketch\sketch_sep18a.ino.cpp.o: in function `loop()': C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:81: undefined reference to `readInfraredSensors()' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:98: undefined reference to `setMotorSpeed(int, int)' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:101: undefined reference to `printDebugInfo(int, int, int)' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:111: undefined reference to `setMotorSpeed(int, int)' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:114: undefined reference to `setMotorSpeed(int, int)' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:117: undefined reference to `setMotorSpeed(int, int)' c:/users/32251/appdata/local/arduino15/packages/esp32/esp32/tools/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\32251\AppData\Local\Temp\.arduinoIDE-unsaved2025818-1512-157dr56.bzym\sketch_sep18a/sketch_sep18a.ino:120: undefined reference to `setMotorSpeed(int, int)' collect2.exe: error: ld returned 1 exit status exit status 1 Compilation error: exit status 1
09-19
Executing action: all (aliases: build) Running ninja in directory E:\2020wifi\NET_WIFI_INVT_BT_ESP32\build Executing "ninja all"... [1/6] Performing build step for 'bootloader' [1/1] cmd.exe /C "cd /D E:\2020wifi\NET_WIFI_INVT_BT_ESP32\build\bootloader\esp-idf\esptool_py && D:\Espressif4.3\Espressif\python_env\idf4.4_py3.11_env\Scripts\python.exe D:/Espressif4.3/Espressif/frameworks/esp-idf-v4.4.7/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 E:/2020wifi/NET_WIFI_INVT_BT_ESP32/build/bootloader/bootloader.bin" Bootloader binary size 0x6420 bytes. 0xbe0 bytes (11%) free. [2/4] Linking CXX executable WIFI-G-BT-C3.elf FAILED: WIFI-G-BT-C3.elf cmd.exe /C "cd . && D:\Espressif4.3\Espressif\tools\xtensa-esp32-elf\esp-2021r2-patch5-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe -mlongcalls -Wno-frame-address @CMakeFiles\WIFI-G-BT-C3.elf.rsp -o WIFI-G-BT-C3.elf && cd ." d:/espressif4.3/espressif/tools/xtensa-esp32-elf/esp-2021r2-patch5-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: D:/Espressif4.3/Espressif/frameworks/esp-idf-v4.4.7/components/esp_wifi/lib/esp32/libcoexist.a(coexist_hw.o):(.iram1.18+0x0): undefined reference to `btdm_rf_bb_reg_init' d:/espressif4.3/espressif/tools/xtensa-esp32-elf/esp-2021r2-patch5-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: D:/Espressif4.3/Espressif/frameworks/esp-idf-v4.4.7/components/esp_wifi/lib/esp32/libcoexist.a(coexist_hw.o):(.iram1.18+0x16): undefined reference to `btdm_rf_bb_reg_init' collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed. ninja failed with exit code 1
最新发布
11-07
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值