Linker error calling C function from C++ despite using EXTERN "C" - c++

I am using this library
http://rtmpdump.mplayerhq.hu/librtmp.3.html which is written in C.
It already uses extern "C" on all of its definitions. But still when I call a function from my [main.cpp] file,, compiler shows these errors:
[1>main.obj : error LNK2028: unresolved token (0A00000F) "extern "C" struct RTMP *
__cdecl RTMP_Alloc(void)" (?RTMP_Alloc##$$J0YAPAURTMP##XZ) referenced in function "int
__cdecl main(void)" (?main##$$HYAHXZ)]
[1>main.obj : error LNK2019: unresolved external symbol "extern "C" struct RTMP *
__cdecl RTMP_Alloc(void)" (?RTMP_Alloc##$$J0YAPAURTMP##XZ) referenced in function "int
__cdecl main(void)" (?main##$$HYAHXZ)]
I am using Visual Studio 2008. And there is no compilation error.
What am I missing?

Here's a scenario that fits the symptoms. Two source files and a header.
first.c
#include "first.h"
struct RTMP *RTMP_Alloc()
{
return (struct RTMP *) 0;
}
second.cpp
#include "first.h"
int main (int argc, char **argv)
{
RTMP *result;
result = RTMP_Alloc();
return 0;
}
first.h
#ifdef __cplusplus
extern "C"
#endif
struct RTMP
{
int val1;
int val2;
} ;
struct RTMP *RTMP_Alloc();
Notice that the extern "C" only applies to the struct definition. To correct it, either it needs to be inserted before the RTMP_Alloc definition, or even easier for large blocks, put inside an extern "C" { ... } construct (notice the addition of the curly braces).

Related

LNK2019: Unresolved external symbol in C++/CLI

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?
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)

LNK2019: Calling C function from CPP code

I have created an MSVS2015 project (MFC/C++) which
links with a static C library. When I build the
projects I get the following error:
error LNK2019:
unresolved external symbol "void __cdecl testLinkerError(void)" (?testLinkerError##YAXXZ) referenced
in function "void __cdecl test1(void)" (?test1##YAXXZ) [PATH_REMOVED]\test.obj [NAME_OF_MFC_PROJECT]
For debugging purposes, I have created 3 files:
test.cpp
linkertest.c
linkertest.h
test.cpp has been added to the MFC project and it looks like this:
#include "linkertest.h"
void test1(void)
{
testLinkerError();
}
The header file linkertest.h looks like this:
#pragma once
#ifdef _cplusplus
extern "C" {
#endif
void testLinkerError(void);
#ifdef _cplusplus
}
#endif
And linkertest.c looks like this:
#include "linkertest.h"
void testLinkerError(void) {
int x = 5;
int y = 7;
int z = x + y;
}
I am quite sure that this is some kind of name mangling issue, but I can't figure out how to solve it.
The calling convention in both projects is set to __cdecl
based on the compiler error, you compiler tried to look up ?testLinkerError##YAXXZ which is a C++ function name. so which means on your import part (not your DLL export) you did not use extern "C"....
and one more thing, The name __cplusplus (two underscores) is defined to the value 201402L when compiling a C++ translation unit. please check your code make sure _cplusplus (one underscore) is defined.

LNK2019 unresolved external appears when function is used inside static method

I have a C++ Visual Studio 2013 console application which is supposed to make use of a DLL MyDLLlib.dll which I have written. MyDLLlib is written in C. One of the functions is called Get_Version. The prototype is
const char *Get_Version();
I put this at the top of the source files to make use of the prototype:
extern "C"{
#include "MyDLLlib.h"
}
If in the function is called in the main as this
printf("version %s\n",Get_Version());
then it works.
However if I add a class with some static methods and a static method makes a call to Get_Version()
const char * ret = Get_Version();
then I get a link error:
Error 1 error LNK2019: unresolved external symbol
"__declspec(dllimport) char * __cdecl Get_Version(void)" (__imp_?Get_Version##YAPADXZ)
referenced in function "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl ServiceDispatch::decoder_Get_Version(class StringBuffer &)"
(?decoder_Get_Version#ServiceDispatch##CA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##AAVStringBuffer###Z)
D:\devt\CplusPlus\VSTrials\Link_to_MyDLLlib\Link_to_MyDllLib\ServiceDispatch.obj Link_to_MyDLLlib``
I am using the same include.
Any clue as to what I might be doing wrong?
If you have CLASS_DECLSPEC defined always as __declspec(dllimport), this will not work for sure. Look at this sample:
DLL_header.h
#if defined( _BUILD_DLL )
# define DLLAPI __declspec(dllexport) //Export when building DLL
#else
# define DLLAPI __declspec(dllimport) //Import when using in other project
#endif
DLLAPI const char *Get_Version();
DLL_source.cpp
#include "Header.h"
const char *Get_Version()
{
return "1.1.0.4";
}
Build DLL with _BUILD_DLL defined.
Main.cpp
#include "DLL_header.h"
int main()
{
printf("%s\n", Get_Version());
return 0;
}
Build this, with _BUILD_DLL not defined.
In your case, it could be problem with extern "C" - you include header inside extern "C", which declares Get_Version() as having __cdecl linkage. But linker is searching for
__imp_?Get_Version##YAPADXZ
Which is a mangled (C++) name. Is your DLL a C or C++ project? If your DLL is build as C project (not C++), put extern "C" on Get_Version()'s declaration with this #ifdef:
#ifdef __cplusplus
extern "C" {
#endif
DLLAPI const char *Get_Version();
#ifdef __cplusplus
}
#endif
Either way, remove extern "C" from around the #include. Also, check if .lib file for this DLL is attached to project as dependency.

declare printf function by myself (using Vistual Studio 2012 and c++)

I want to declare printf function by myself, and try if the code can work.
I looked into the <stdio.h> and found it declared as:
_Check_return_opt_ _CRTIMP int __cdecl printf(_In_z_ _Printf_format_string_ const char * _Format, ...);
Then I found
_Check_return_opt_ defined in <crtdefs.h>
_CRTIMP defined in <crtdefs.h>
_In_z_ _Printf_format_string_ defined in <sal.h>
So my code is as:
#include <crtdefs.h>
#include <sal.h>
_Check_return_opt_ _CRTIMP int __cdecl printf(_In_z_ _Printf_format_string_ const char * _Format, ...);
int main()
{
int a = 100;
printf("%d\n",a);
}
I guess this will work, but it gets an error:
error LNK2019: unresolved external symbol ...
What's wrong with my code? Are there anything I missed in <stdio.h>?
I have already read a question on stackoverflow.
(https://stackoverflow.com/questions/25155631/microsoft-visual-studio-2012-version-of-printf-function-declaration)
It helps, but doesn't solve my problem.
I also learn to know that an extern "C" function works fine. such as:
extern "C" int printf (const char * __format, ...);
But I think this still not accounts for my doubt.
In stdio.h, was the prototype for printf inside an extern "C" {} block? Note that this block could be large and so you might not have noticed it. If so, you need to use extern "C" around your prototype.
The extern "C" should remove your doubt - without that you are compiling the declaration as C++ which doesn't use a linking convention that's the same as C (which is how printf() is compiled in the library).
You can fix the problem using extern "C" or compiling your program as a C program instead of C++ (change the name of the source file to have a .cextension).

error : LNK2019: unresolved external symbol "extern "C"

What's wrong with this unresolved externals? I'm trying to implement this into InitInstance of my MFC application. But I'm getting this line error from the debugger.
LNK2019: unresolved external symbol "extern "C" int __stdcall EnumProcesses(unsigned long *,unsigned long,unsigned long *)" (?EnumProcesses##$$J212YGHPAKK0#Z) referenced in function "public: virtual int __thiscall COkApp::InitInstance(void)" (?InitInstance#COpenEyeProcessApp##$$FUAEHXZ)
Here is the OkApp files :
OkApp.h
//...
#include <psapi.h>
//...
OkApp.cpp
#include "OkApp.h"
//...
BOOL COkApp::InitInstance()
{
//...
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return 1;
}
//...
}
Knowing that my project is coded into Unicode project, how do I to fix this, please?
Any brilliant idea, please?
You need Psapi.lib to be linked.
#pragma comment(lib, "Psapi.lib")
It looks like EnumProcesses is declared extern "C" in the header, but not in the source file. Both have to be consistent. Either change both to extern "C" (if you intend to use EnumProcesses from within C), or remove the extern "C" in the header.
EDIT: Or you just forgot to add the library containing EnumProcesses when calling the linker.