下面的是DLL源码
/*********************/
/* dllTest.h */
/*********************/
#ifndef _DLL_TEST_H_
#define _DLL_TEST_H_#if defined DLL_TEST_API
#else
#define DLL_TEST_API __declspec(dllimport)
#endif#if defined __cplusplus
extern "C"
{
#endifDLL_TEST_API int n_gResult;DLL_TEST_API int Add(int nLeft, int nRight);#if defined __cplusplus
}
#endif#endif/*********************/
/* dllTest.cpp */
/*********************/
#include <windows.h>
#define DLL_TEST_API __declspec(dllexport)#include "dllTest.h"
#if defined __cplusplus
extern "C"
{
#endif
//int n_gResult; int Add(int nLeft, int nRight)
{
n_gResult = nLeft + nRight;
return(n_gResult);
}
#if defined __cplusplus
}
#endif
下面是EXE的代码:
/*********************/
/* testDll.cpp */
/*********************/
#include <windows.h>
#include <dllTest.h>#if defined __cplusplus
extern "C"
{
#endifint WINAPI WinMain(HINSTANCE hInstExe,
HINSTANCE ,
LPTSTR pszCmdLine,
int)
{
int nLeft = 10, nRight = 25;
TCHAR sz[100];
wsprintf(sz, TEXT("%d + %d = %d"), nLeft, nRight, Add(nLeft, nRight));
MessageBox(NULL, sz, TEXT("Calculation"), MB_OK); wsprintf(sz, TEXT("The result from the last Add is: %d"), n_gResult);
MessageBox(NULL, sz, TEXT("Last Result"), MB_OK);
return(0);
}#if defined __cplusplus
}
#endif
编译DLL可以成功,但是编译EXE时提示下列错误:
testDll.obj : error LNK2001: unresolved external symbol __imp__n_gResult
testDll.obj : error LNK2001: unresolved external symbol __imp__Add犯请大家指点指点!
/*********************/
/* dllTest.h */
/*********************/
#ifndef _DLL_TEST_H_
#define _DLL_TEST_H_#if defined DLL_TEST_API
#else
#define DLL_TEST_API __declspec(dllimport)
#endif#if defined __cplusplus
extern "C"
{
#endifDLL_TEST_API int n_gResult;DLL_TEST_API int Add(int nLeft, int nRight);#if defined __cplusplus
}
#endif#endif/*********************/
/* dllTest.cpp */
/*********************/
#include <windows.h>
#define DLL_TEST_API __declspec(dllexport)#include "dllTest.h"
#if defined __cplusplus
extern "C"
{
#endif
//int n_gResult; int Add(int nLeft, int nRight)
{
n_gResult = nLeft + nRight;
return(n_gResult);
}
#if defined __cplusplus
}
#endif
下面是EXE的代码:
/*********************/
/* testDll.cpp */
/*********************/
#include <windows.h>
#include <dllTest.h>#if defined __cplusplus
extern "C"
{
#endifint WINAPI WinMain(HINSTANCE hInstExe,
HINSTANCE ,
LPTSTR pszCmdLine,
int)
{
int nLeft = 10, nRight = 25;
TCHAR sz[100];
wsprintf(sz, TEXT("%d + %d = %d"), nLeft, nRight, Add(nLeft, nRight));
MessageBox(NULL, sz, TEXT("Calculation"), MB_OK); wsprintf(sz, TEXT("The result from the last Add is: %d"), n_gResult);
MessageBox(NULL, sz, TEXT("Last Result"), MB_OK);
return(0);
}#if defined __cplusplus
}
#endif
编译DLL可以成功,但是编译EXE时提示下列错误:
testDll.obj : error LNK2001: unresolved external symbol __imp__n_gResult
testDll.obj : error LNK2001: unresolved external symbol __imp__Add犯请大家指点指点!
#pragma comment(lib, "dllTest.lib")
#pragma comment(lib, "testdll.lib")