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.
Related
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)
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.
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).
I know this has been asked a thousand times but I can't fix this simple problem.
This is the code:
#include "windows.h"
extern "C"
{
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
{
return 0;
}
}
The subsystem is set to Windows (/SUBSYSTEM:WINDOWS). I have the following error:
error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
The error was caused because the file were WinMain was defined was a .h file, and therefore it wasn't included in the project.
The problem is that your WinMain function is declared in an extern "C" block, which gives it C-style linkage. The Microsoft Visual C++ Runtime expects there to be a function with the symbol _WinMain#16, which is the C++ symbol name for a __stdcall function that takes 16 bytes worth of arguments.
To solve this problem, remove the extern "C" linkage-specification block from around your WinMain function.
I have a DLL and a main executable, and the main executable is not linking in the symbols from the import library of the DLL anymore, and the declspec in the DLL .map file is not matching, nor is the mangled C++ name matching. I can't figure out why although I have done the usual things you do when you can't get C++ stuff to link.
My headers define something like this:
#ifdef MY_MODULE
#undef CLASS_EXPORT
#define CLASS_EXPORT __declspec(dllexport)
#else
#undef CLASS_EXPORT
#define CLASS_EXPORT __declspec(dllimport)
#endif
My classes seem to be exporting properly, but the .map file indicates some strange thing I think is wrong:
6 ?CreateDataArea##YAKPAGPBD111PAVCObject##K#Z (unsigned long __cdecl
CreateDataArea(unsigned short *,char const *,char const *,char const *,
char const *,class CObject *,unsigned long))
The mangled name above has ##YAK...
The link error is:
1>Device.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) public: unsigned long __thiscall
CCommonMemory::CreateDataArea(unsigned short *,char const *,char const *,
char const *,char const *,class CObject *,unsigned long)"
(__imp_?CreateDataArea#CCommonMemory##QAEKPAGPBD111PAVCObject##K#Z) referenced
in function "public: __thiscall CDevice::CDevice(void)" (??0CDevice##QAE#XZ)
So why is the mangled name ##QAEK different when I have the declspec macro configured and why is the .map file showing __thiscall calling convention when the macro defines __declspec(dllexport)?
It's exactly like I forgot to do the __declspec(dllimport/dllexport) macro, yet I did it.
I tried putting CLASS_EXPORT on EACH method exported, and I tried it in the first line of the class declaration with no change.
It looks like the definition for CreateDataArea() isn't being scoped to the class (ie., you've left off a CCommonMemory:: when defining the function).
If you look at what's getting into the map file, you'll see that it's a __cdecl function without the class name 'attached'.