C vs. C++ (VS2010)
-
I hope I chose an appropriate forum. Why does the test code below build in VS2010 as C code but not as C++ code (linker errors further below). And what's needed to make it compile as C++ code. Thanks. - Vince
#include
__declspec(dllimport) int __stdcall SetEnvironmentVariableA(const char *pName, const char *pValue);
__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(const char *pName, char *pValue, unsigned long nSize);int main ( int argc, char **argv )
{
char szValue[32768];
SetEnvironmentVariableA(argv[1], argv[2]);
GetEnvironmentVariableA(argv[1], szValue, 32768);
return 0;
}error LNK2001: unresolved external symbol "__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(char const *,char *,unsigned long)" (__imp_?GetEnvironmentVariableA@@YGKPBDPADK@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall SetEnvironmentVariableA(char const *,char const *)" (__imp_?SetEnvironmentVariableA@@YGHPBD0@Z) -
I hope I chose an appropriate forum. Why does the test code below build in VS2010 as C code but not as C++ code (linker errors further below). And what's needed to make it compile as C++ code. Thanks. - Vince
#include
__declspec(dllimport) int __stdcall SetEnvironmentVariableA(const char *pName, const char *pValue);
__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(const char *pName, char *pValue, unsigned long nSize);int main ( int argc, char **argv )
{
char szValue[32768];
SetEnvironmentVariableA(argv[1], argv[2]);
GetEnvironmentVariableA(argv[1], szValue, 32768);
return 0;
}error LNK2001: unresolved external symbol "__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(char const *,char *,unsigned long)" (__imp_?GetEnvironmentVariableA@@YGKPBDPADK@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall SetEnvironmentVariableA(char const *,char const *)" (__imp_?SetEnvironmentVariableA@@YGHPBD0@Z) -
I hope I chose an appropriate forum. Why does the test code below build in VS2010 as C code but not as C++ code (linker errors further below). And what's needed to make it compile as C++ code. Thanks. - Vince
#include
__declspec(dllimport) int __stdcall SetEnvironmentVariableA(const char *pName, const char *pValue);
__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(const char *pName, char *pValue, unsigned long nSize);int main ( int argc, char **argv )
{
char szValue[32768];
SetEnvironmentVariableA(argv[1], argv[2]);
GetEnvironmentVariableA(argv[1], szValue, 32768);
return 0;
}error LNK2001: unresolved external symbol "__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(char const *,char *,unsigned long)" (__imp_?GetEnvironmentVariableA@@YGKPBDPADK@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall SetEnvironmentVariableA(char const *,char const *)" (__imp_?SetEnvironmentVariableA@@YGHPBD0@Z)Put declarations inside
extern "C" { }
C & C++ uses different name mangling.