Hi I programming in windows I'm using ms vc++ 6.0 I was writing a simple dll which looks like this:
//simple.dll
#include<windows.h>
#ifdef _cplusplus
extern "C"
{
#endif
_declspec(dllexport) int _cdecl function(int a)
{
a+=a;
return a;
}
#ifdef _cplusplus
}
#endif
I compile the codes above and I got simple.dll and simple.lib. I place the simple.lib in ms visual studio Lib directory and in the project settings at Linker menu I added the 'simple.lib'.
The code below is the simplexe.the application wich uses the simple.dll\
//simplexe.cpp
#include<windows.h>
extern "C" int _cdecl function(int);
int main(void)
{
int result=1;
int a=9;
result=function(a);
return result;
}
Then I received the following Errors:
simple.obj : error LNK2001: unresolved external symbol _function
Debug/simplexe.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Also I tried a run-time linking here is what I did:
//func.h
int function(int number);
//runtime.cpp
#include<windows.h>
#include<iostream>
#include "func.h"
using namespace std;
int main()
{
HINSTANCE dllhandle;
int result;
int number=99;
dllhandle=LoadLibraryEx("a",0,NULL);
if(dllhandle==NULL)
{
printf("Error DLL not found :error code:",GetLastError());
}
if(GetProcAddress(dllhandle,"function")==NULL)
{
cout<<"Function Not Found in the DLL.\n";
FreeLibrary(dllhandle);
}
else{
result=function(number);
cout<<result<<endl;
}
FreeLibrary(dllhandle);
return 0;
}
The following Errors occur:
runtime.obj : error LNK2001: unresolved external symbol "int __cdecl function(int)" (?function@@YAHH@Z)
Debug/runtime.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Can somebody help. And demostrate where is my problem.Thanks.
//simplexe.cpp
#include <iostream>
#include <windows.h>
#ifdef _cplusplus
extern"C"
#endif
int _cdecl function(int);
int main(){
int result=1;
int a=9;
result=function(a);
std::cout <<result<<std::endl;
return result;
}
You were looking for a function compiled as C code, while compiling the actual function as C++. C++ mangles symbol names and there were probably other stuff wrong, too.