I'm trying to get started on using dll's for my code in VS 2005. The code I have is very simple, just to try a test case.开发者_JAVA百科
testdll.h:
#ifdef TEST_EXPORTS
#define TESTDLLPORT __declspec( dllexport )
#else
#define TESTDLLPORT __declspec( dllimport )
#endif
namespace TestDLLNS
{
static int s = 0;
class MyTestDll {
public:
static TESTDLLPORT int printDLLFuncs();
};
}
testdll.cpp:
// testdll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "testdll.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
namespace TestDLLNS {
int MyTestDll::printDLLFuncs() {
cout << "DLL function called" << endl;
return s;
}
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
test.cpp:
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "testdll.h"
int main(int argc, char* argv[])
{
cout << "int: " << TestDLLNS::MyTestDll::printDLLFuncs() << endl;
cout << "Called dll" << endl;
return 0;
}
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int _cdecl TestDLLNS::MyTestDll::printDLLFuncs(void)" (_imp_?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ) referenced in function _main test.obj
dumpbin \exports testdllD.dll gives the following: ordinal hint RVA name
1 0 0001105F ?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ
So the symbol clearly exists in the .dll. Should Visual Studio also be creating a testdllD.lib files which I should be linking with test.cpp? If so, how I get visual studio to make both a .dll and a .lib.
Edit: Am I doing the importing/exporting correctly? From what I understand, whem compiling the dll you would want to use dllexport while when compiling the executable which uses the dll, dllimport would be used.
There are few things to note and I hope you did not miss them.
- What you have is a linking error, means that your compiling was okay, but there is a link error. The error shows that the linking does not find a definition for your printDLLFuncs() function which was referenced in the object file.
- You either have to provide the path for the .lib file where the function is defined in the Project Directories -> library path, or put it in any of the project folders so your Visual Studio can find it.
精彩评论