FreeRTOS与LVGLv8一起的仿真工程,出现“void __cdecl func1(void) @@YAXXZ“的问题的解决方法

先定下结论:

在main文件中使用extern "C"{ }括起来。原因是C++文件与C文件使用编译器不同,编译C++时,会把在C文件中的func1编译成func1@@YAXXZ。

1、环境:

VS2019

已合并的FreeRTOS与LVGLv8一起仿真的工程。

2、问题分析

1)产生

在向已合并的FreeRTOS与LVGLv8一起仿真的工程 中添加应用程序时,出现错误。我已为是没有添加头文件,然鹅,仔仔细细添加后,还是出现此问题。我又将配置中的_CONSOLE程序改成_WINDOWS还是不行。TMD。

 

2)分析

搜到一篇文章:​​​​​​error: LNK2019: unresolved external symbol "void __cdecl fun1(void)" (?fun1@@YAXXZ) referenced in function _main (myprogrammingnotes.com)icon-default.png?t=M276https://myprogrammingnotes.com/error-lnk2019-unresolved-external-symbol-void-__cdecl-fun1void-fun1yaxxz-referenced-function-_main.html

关键内容如下:

Home > error: LNK2019: unresolved external symbol “void __cdecl fun1(void)” (?fun1@@YAXXZ) referenced in function _main

error: LNK2019: unresolved external symbol “void __cdecl fun1(void)” (?fun1@@YAXXZ) referenced in function _main

admin July 5, 2021 0

Today, I add two c files(one is fun.c, the other is its header file fun.h) to a c++ project, because I want to call a function(fun1) residing in the C source file(fun.c) in a c++ source file (main.cpp) of the C++ project. Then the error occurs.

main.obj:-1: error: LNK2019: unresolved external symbol “void __cdecl fun1(void)” (?fun1@@YAXXZ) referenced in function _main

This error is due to so called name mangling. The C++ compiler compiling main.cpp generates a function reference in main.o, however, the reference is not “fun1” but ?fun1@@YAXXZ, which is called name mangling. When compiling fun.c, the C compiler may generate the function name of fun1 simply as “fun1″(or another mangled name) in fun.o. So, the linker cannot find a match in the function implementation in fun.o for the function reference in main.o, thus reports the error.

 意思是:.Cpp与.C使用不同编译器将同一个函数编译成了不同名字。

3)解决方案

方案一:

using C++ compiler to compile the C source file

方案二:

tell C++ compiler not to mangle function names

You can guide the C++ compiler to generate C reference names when compiling main.cpp. Specifically, you can enclose the declaration of the function with extern “C”{…},i.e.,

  1. #ifdef __cplusplus
  2. extern "C"{
  3. #endif
  4. void fun1( );
  5. #ifdef __cplusplus
  6. }
  7. #endif

This way, the calling of fun1 will simply generate the reference as “fun1″(or other type of C name mangling).

3、结论

在main文件中使用extern "C"{ }括起来。原因是C++文件与C文件使用编译器不同,编译C++时,会把在C文件中的func1编译成func1@@YAXXZ。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值