我试图调用一个函数(从一个#包含的库),该函数以函数指针作为参数,并将指向C文件中的函数的指针传递给这个函数。编译器引发“函数名称的未定义引用”错误。
我试着从.c文件中删除代码并将其直接放入main.cpp文件中(参见下面标记为“THIS WORKS”的部分),这样就避免了错误。我知道我应该能够将它保存在.c文件中,因为我正在非常密切地跟踪一个编译过程中没有错误的示例。
/****************/
/*** MAIN.CPP ***/
/****************/
extern "C"
{
#include "btntask.h"
}
using namespace touchgfx;
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#define configGUI_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
#define configGUI_TASK_STK_SIZE ( 1024 )
static void GUITask(void* params)
{
/* STUFF */
}
/*********** THIS WORKS ************/
/*
void btn_tasked(void* params)/{
/* STUFF */
}
*/
/*********** THIS WORKS ************/
int main(void)
{
xTaskCreate(GUITask, "GUITask",
configGUI_TASK_STK_SIZE,
NULL,
configGUI_TASK_PRIORITY,
NULL);
/* error undefined reference to btn_task */
xTaskCreate(btn_task, "BTNTask",
512,
NULL,
configGUI_TASK_PRIORITY+1,
NULL);
for (;;);
}这是btntask.h
/****************/
/*** btntask.h ***/
/****************/
#ifndef BTNTASK_H
#define BTNTASK_H
void btn_task(void* params);
#endif /* BTNTASK_H */这是btntask.c
/****************/
/*** btntask.c ***/
/****************/
#include "btntask.h"
void btn_task(void* params)
{
/* STUFF */
}编译器日志如下:
Converting images
Compiling Core/Src/main.cpp
Linking TouchGFX/build/bin/target.elf
TouchGFX/build/ST/STM32F429IDISCO/Core/Src/main.o: In function `main':
d:\Dropbox\TouchGFXProjects\MiniGame\Project/Core/Src/main.cpp:116: undefined reference to `btn_task'
collect2.exe: error: ld returned 1 exit status
gcc/Makefile:363: recipe for target 'TouchGFX/build/bin/target.elf' failed
make[2]: *** [TouchGFX/build/bin/target.elf] Error 1
gcc/Makefile:359: recipe for target 'generate_assets' failed
make[1]: *** [generate_assets] Error 2
../gcc/Makefile:45: recipe for target 'all' failed
make: *** [all] Error 2编译由我正在使用的软件包(TouchGFX)执行。如果有用的话,这就是日志中报告的编译命令:
touchgfx update_project --project-file=simulator/msvs/Application.vcxproj && touchgfx update_project --project-file=../EWARM/application.ewp && touchgfx update_project --project-file=../EWARM6/project.ewp && touchgfx update_project --project-file=../MDK-ARM/application.uvproj
make -f ../gcc/Makefile -j8* UPDATE *我注意到TouchGFX填充了一个.obj文件的Debug文件夹,其中一个用于应用程序中的每个源代码文件,我可以看到它缺少一个btntask.obj。显然,它没有链接btntask.obj文件。我得弄清楚为什么。有一个makefile详细说明了所有的链接,但它使用了很多我不熟悉的语法。
* *最终确定了要包含的目录列表。解决方案是编辑列表以添加我的btntask文件所在的其他目录。
# Directories containing application-specific source and header files.
# Additional components can be added to this list. make will look for
# source files recursively in comp_name/src and setup an include directive
# for comp_name/include.
components := TouchGFX/gui target TouchGFX/generated/gui_generated谢谢大家的参与。
发布于 2019-01-04 02:11:35
您需要知道这两个概念:
名称残缺
您可能需要在btn_task文件和头文件中将extern "C"封装到btntask.c文件中。
由于项目包含C++文件,所以C文件可能是由C++编译器而不是C编译器编译的。那么您的C函数实现就是名称损坏了。但参考的位置仍然是使用非损坏的名称。
链接
不要忘记将btntask.c的产品包含在链接阶段。
毕竟,我不熟悉您的项目控制软件,您必须确保这两点由您自己。
https://stackoverflow.com/questions/54032002
复制相似问题