c++ - LNK2019:C++/CLI 中未解析的外部符号
问题描述
我有一个sample.c文件,其中定义了一个非静态函数
来源:sample.c
#if defined(__cplusplus)
extern "C" {
#endif
int get_sample_value()
{
return 1000;
}
#if defined(__cplusplus)
}
#endif
有一个纯C++ SDK 项目sample_sdk,它基本上生成了几个静态库,其中get_sample_value()函数在一个源文件中使用,如下所示:
来源:sample_sdk_source.cpp
extern int get_sample_value();
static void do_processing()
{
int sample_value = get_sample_value();
process( sample_value );
}
上述sample.c将在另一个C++/CLI GUI 应用程序SampleViewer中编译,其中sample_sdk库包含在该应用程序中。但是在编译 SampleViewer 时,我们收到以下错误:
libsample-sdk-x86.lib(sample_sdk_source.obj):错误 LNK2019:函数“public: static void __cdecl do_processing()”中引用的未解析外部符号“int __cdecl get_sample_value()”(?get_sample_value@@YAPBUint@@XZ) ?do_processing@@SAXXZ)
我也尝试使用 SampleViewer::main.cpp 文件中的相同函数,但存在相同的错误。从 C++/CLI 环境访问 C 文件中定义为 extern 的函数时是否有任何问题?
解决方案
The linker error says it all:
- Your
extern int get_sample_value();
declaration in C++ sets up an undefined symbol for the mangled name?get_sample_value@@YAPBUint@@XZ
- Your definition in sample.c defines a symbol with a non-mangled name (
_get_sample_value
).
To solve this, either mark your declaration in C++ with extern "C"
as well, or better yet: move the declaration into a header that both sample.c and sample_sdk_source.cpp can include (with the #if defined(__cplusplus)
guard)
推荐阅读
- go - 从 Go 中的 if 语句中打破的干净方法
- npm - `npm publish` 和 `yarn publish` 有什么不同?
- swift - 为什么我无法快速从登录页面移动?
- c++ - 相机流式传输时 Qt GUI 线程没有响应
- python - 如何编写一个可以处理多个表单的FormView
- python - 使用 Airflow,MsSqlOperator 是否接受来自 SQL Server 的响应?
- html - 如何在保持中心圆的大小及其高度的同时使 svg 拉伸到父级的宽度?
- maven - Maven 阴影插件不执行
- ssl - 访问 SO 时证书过期
- javascript - 如何在谷歌电子表格中调用具有多个参数的自定义函数