Reputation:
I have a sample.c file where a non-static function is defined
Source: sample.c
#if defined(__cplusplus)
extern "C" {
#endif
int get_sample_value()
{
return 1000;
}
#if defined(__cplusplus)
}
#endif
There is pure C++ SDK project, sample_sdk which basically generates a couple of static-libs, where get_sample_value() function is used within one of the source files as follows:
Source: sample_sdk_source.cpp
extern int get_sample_value();
static void do_processing()
{
int sample_value = get_sample_value();
process( sample_value );
}
Above-mentioned sample.c will be compiled in another C++/CLI GUI application, SampleViewer where sample_sdk libs are included within this application. But when compiling the SampleViewer we are getting the following error:
libsample-sdk-x86.lib(sample_sdk_source.obj) : error LNK2019: unresolved external symbol "int __cdecl get_sample_value()" (?get_sample_value@@YAPBUint@@XZ) referenced in function "public: static void __cdecl do_processing()" (?do_processing@@SAXXZ)
I also tried to use the same function from SampleViewer::main.cpp file, but the same error exists. Is there any issue when accessing function defined in C file as extern from C++/CLI environment?
Upvotes: 1
Views: 619
Reputation: 30807
The linker error says it all:
extern int get_sample_value();
declaration in C++ sets up an undefined symbol for the mangled name ?get_sample_value@@YAPBUint@@XZ
_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)
Upvotes: 2