下面的是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犯请大家指点指点!

解决方案 »

  1.   

    lib文件没有加上
    #pragma comment(lib, "dllTest.lib")
      

  2.   

    exe代码:
    #pragma comment(lib, "testdll.lib")
      

  3.   

    #pragma   comment(lib,   "dllTest.lib") 加上再看,
      

  4.   

    还可以再工程->设置->link->Object/library modules里面写上testdll.lib,并应用
      

  5.   

    对了,一定要象上面说的那样加入lib文件,而且有需要的话还要设置lib文件的目录