我本来打算发200分,谁知提示说我只能发100分的贴,不好意思了!
问题很简单的,大家都来看看吧,都能看懂的!
guilib.dll在编译链接没问题.客户程序链接DLL是出现下面的问题
正在生成代码...
正在编译资源...
Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
Copyright (C) Microsoft Corporation.  All rights reserved.
正在编译资源清单...
Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
Copyright (C) Microsoft Corporation.  All rights reserved.
正在链接...
GuiVisioDemo.obj : error LNK2005: "unsigned long m_clrBlueBasic" (?m_clrBlueBasic@@3KA) 已经在 GuiFolderVisio.obj 中定义
GuiVisioDemo.obj : error LNK2005: "unsigned long m_clrBlueSelect" (?m_clrBlueSelect@@3KA) 已经在 GuiFolderVisio.obj 中定义
LINK : fatal error LNK1104: 无法打开文件“GuiTk115d.lib”
////////////////////////////////////////////////////////////////////////////////////////////////////
我估计问题是出在头文件上, 打开头文件, 
下面是我的guilib这个dll工程,所使用的stdafx.h 和 guilib.h 头文件
/****************************************************************************
 stdafx.h  
****************************************************************************/#if !defined(AFX_STDAFX_H__42A0B087_D792_11D4_B90C_000000000000__INCLUDED_)
#define AFX_STDAFX_H__42A0B087_D792_11D4_B90C_000000000000__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions//#ifndef _AFX_NO_OLE_SUPPORT
#include <afxole.h>         // MFC OLE classes
#include <afxodlgs.h>       // MFC OLE dialog classes
#include <afxdisp.h>        // MFC Automation classes
//#endif // _AFX_NO_OLE_SUPPORT
#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h> // MFC ODBC database classes
#endif // _AFX_NO_DB_SUPPORT#ifndef _AFX_NO_DAO_SUPPORT
#include <afxdao.h> // MFC DAO database classes
#endif // _AFX_NO_DAO_SUPPORT#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#include <afxrich.h>
///////////////////////第一个问题//////////////////////////////////////////////////////////////////
下面的英语我能看懂,GUILIB_EXPORTS 应该正常是在编译预处理程序设置中被定义,如果没有的话,在这里定义它.
但是它为什么 "应该正常是在编译预处理程序设置中被定义" ,是我需要对预处理程序进行设置吗,怎么设置?
// GUILIB_EXPORTS should normally be defined in the compiler preprocessor setting,
// if not define it here to export the class(es) or functions in this dll.
#if defined _AFXDLL && !defined _GUILIB_STATIC_
#ifndef GUILIB_EXPORTS
#define GUILIB_EXPORTS
#endif //GUILIB_EXPORTS
#endif // _AFXDLL && !defined _GUILIB_STATIC_
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Automatic library selection macro, will not be defined in the dll user-application,
// and will make sure the correct export library is automatically linked to the
// application.
#define _GUILIB_NOAUTOLIB#define  GUISTYLE_XP   0x08001 
#define GUISTYLE_2003 0x08002
#define GUISTYLE_2003MENUBTN 0x08021
#define WIN_NT   0x08003 
#define WIN_2000 0x08004
#define WIN_XP 0x08005
#define WIN_95 0x08006
#define WIN_98 0x08007
#define WIN_32 0x08008
#define WIN_ME 0x08009
#define WIN_95OSR2 0x0800A
#define WIN_98_SE 0x0800B
 
#include "GuiLib.h"
#include "GuiLibRes.h"//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__42A0B087_D792_11D4_B90C_000000000000__INCLUDED_)
/**************************************************************************** guilib.h
 ****************************************************************************/#ifndef __GUILIB_H
#define __GUILIB_H#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000// Additional MFC headers
#ifndef __AFXTEMPL_H__
#include <afxtempl.h>
#endif //__AFXTEMPL_H__#ifndef __AFXPRIV_H__
#include <afxpriv.h>
#endif //__AFXPRIV_H__#if _MSC_VER >= 1300
#include <..\atlmfc\src\mfc\afximpl.h>
#else
#include <..\src\afximpl.h>
#endif/////////第二个问题///////////////////////////////////////////////////
//在我列出的头文件中. #if defined _AFXDLL && !defined _GUILIB_STATIC_ 这句话出现了好几次.
//它的意思是如果定义了_AFXDLL,就...    
//但是_AFXDLL到底定义了没有?
//我搜索了 整个项目, 包括连要使用DLL的客户程序 都没有 对_AFXDLL的定义.
//因此我的结论是没有定义...我认为我的结论是正确. 但是请看第三个问题
#if defined _AFXDLL && !defined _GUILIB_STATIC_
#ifdef GUILIB_EXPORTS
   #define GUILIBDLLEXPORT  _declspec(dllexport)
#else
   #define GUILIBDLLEXPORT  _declspec(dllimport)
#endif
#else
#define GUILIBDLLEXPORT
#endif  
////////////////问题四也是最主要的问题//////////////////////////
我现在就是搞不清头绪.  无法分析出来下面的问题.简单举例来说:
class  GUILIBDLLEXPORT  CGUIMenu 
{};
这个GUILIBDLLEXPORT 我搞不清它到底是被替换成了_declspec(dllexport) 还是_declspec(dllimport)
还是空.
第一种情况:假设我是动态链接.
那么我认为的DLL里应该是处理这一句 #define GUILIBDLLEXPORT  _declspec(dllexport)
而客程序应该是                    #define GUILIBDLLEXPORT  _declspec(dllimport)
可是DLL和客户程序都没有对_AFXDLL的定义那就不会执行到上面的语句.
也就形成了第二种情况#define GUILIBDLLEXPORT
像普通的C++ 包含头文件一样,不存动态链接了.
哎!我现在都搞不清,到底我是在动态链接?还是在直接包含头文件.
谁能帮我修改一下代码呀!
///////////////////////////////////////////////////////
#ifndef _GUILIB_NOAUTOLIB
                          
////////////////////////////////////////////第三个问题///////////////////////////////////////////////////////
但,链接器怎么会提示是:LINK : fatal error LNK1104: 无法打开文件“GuiTk115d.lib”
//整个方案里只有在#if defined _AFXDLL && !defined _GUILIB_STATIC_里面出现过“GuiTk115d.lib”
//我认为既然_AFXDLL没有定义过.编译器怎么会处理到这里呢?
//因此我的结论是它定义过了,我刚才认为_AFXDLL没有定义过.现在又认为它定义过了,现在我也搞不清它到底定义过没有.#if defined _AFXDLL && !defined _GUILIB_STATIC_
// MFC shared DLL with GuiToolKit shared DLL
#ifdef _DEBUG
#ifdef _UNICODE
#pragma comment(lib,"GuiTk115ud.lib") 
#pragma message("Automatically linking with GuiTk115ud.dll")
#else
#pragma comment(lib,"GuiTk115d.lib")  //////////整个方案里只有这里出现“GuiTk115d.lib”
                                                              //我认为既然_AFXDLL没有定义过.编译器怎么会处理到这里呢?
#pragma message("Automatically linking with GuiTk115d.dll")
#endif
#else
#ifdef _UNICODE
#pragma comment(lib,"GuiTk115u.lib") 
#pragma message("Automatically linking with GuiTk115u.dll") 
#else
#pragma comment(lib,"GuiTk115.lib") 
#pragma message("Automatically linking with GuiTk115.dll") 
#endif
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
#elif defined _GUILIB_STATIC_
// MFC shared DLL with GuiToolKit static library
#ifdef _DEBUG
#ifdef _UNICODE
#pragma comment(lib,"GuiTk115Staticuds.lib") 
#pragma message("Automatically linking with static GuiTk115Staticuds.lib") 
#else
#pragma comment(lib,"GuiTk115Staticds.lib") 
#pragma message("Automatically linking with static GuiTk115Staticds.lib") 
#endif
#else
#ifdef _UNICODE
#pragma comment(lib,"GuiTk115Staticus.lib") 
#pragma message("Automatically linking with static GuiTk115Staticus.lib") 
#else
#pragma comment(lib,"GuiTk115Statics.lib") 
#pragma message("Automatically linking with static GuiTk115Statics.lib") 
#endif
#endif
#else
// MFC static library with GuiToolKit static library
#ifdef _DEBUG
#ifdef _UNICODE
#pragma comment(lib,"GuiTk115Staticud.lib") 
#pragma message("Automatically linking with static GuiTk115Staticud.lib") 
#else
#pragma comment(lib,"GuiTk115Staticd.lib") 
#pragma message("Automatically linking with static GuiTk115Staticd.lib") 
#endif
#else
#ifdef _UNICODE
#pragma comment(lib,"GuiTk115Staticu.lib") 
#pragma message("Automatically linking with static GuiTk115Staticu.lib") 
#else
#pragma comment(lib,"GuiTk115Static.lib") 
#pragma message("Automatically linking with static GuiTk115Static.lib") 
#endif
#endif
#endif//Thanks to Juno Kampstra for this suggestion // Export class headers#include "GuiLibRes.h"#include "CBaseTab.h"
#include "CoolMenu.h"
#include "GuiBaseTab.h"
#include "GuiButton.h"
#include "GuiCapOutBar.h"
#include "GuicheckBox.h"
#include "GuiColors.h"
#include "GuiComboBoxExt.h"
#include "GuiContainer.h"
#include "GuiControlBar.h"
#include "GuiSplitter.h"
#include "GuiSysTray.h"#endif // _GUILIB_NOAUTOLIB#ifdef _GUILIB_IN_OTHER_DLL
GUILIBDLLEXPORT void GuiToolKitDllInitialize();
#endif // _GUILIB_IN_OTHER_DLL#endif //__GUILIB_H
下面是dll的客户程序GuiFolderVisio使用的 stdafx.h 
/****************************************************************************
 stdafx.h : include file for standard system include files,
****************************************************************************/#if !defined(AFX_STDAFX_H__BB7C2885_1A48_41D5_A2E4_2F776C995666__INCLUDED_)
#define AFX_STDAFX_H__BB7C2885_1A48_41D5_A2E4_2F776C995666__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT#include <Guilib.h>  //{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__BB7C2885_1A48_41D5_A2E4_2F776C995666__INCLUDED_)//////////////////////////////////////

解决方案 »

  1.   

    重复定义了,是不是多个对象重复link了
      

  2.   

    似乎和链接dll没有关系,只是在客户端出现了重定义和找不到文件的问题
    不要在.h中定义变量,也不要在多个.cpp定义相同名称的变量。
      

  3.   

    东西太多了。没仔细看,这东西一看就是重定义了,或者说你在一个头里面申请了一个方法,而同时在两个+的cpp文件中实现。
      

  4.   

    i know the mistake。
    the variable m_clrBlueBasic is declared in a head file,as a global variable.
    when you include the head file in two or more cpp files,can output the error.
      

  5.   

    重复定义了,你全部工程搜索,或直接用windows的目录搜索,找出重复的地方,
    加上extern就可以了
      

  6.   

    that m_clrBlueBasic  is not a global viriable,  it is a  data  member of  class .
    楼上几位兄弟,先谢谢你们,我贴的代码是很长, 但是大多数都是无关紧要,你稍微仔细看,就知道很简单的.
    只有少数在我不理解的地方,贴了4个问题.请针对问题回答,谢谢
      

  7.   

    在MFC dll工程中_AFXDLL 宏有定义,但不是在源文件中,是在编译时命令行中定义的宏。
    “/D "_WINDOWS" /D "_DEBUG" /D "_USRDLL" /D "_WINDLL" /D "_AFXDLL" ”
      

  8.   

    问题很多,尝试回答下:
    1、_AFXDLL是否定义,可以查看工程设置确定,如下图标红部分:2、"已经定义"错误,应该是有重复定义语句,工程中搜索一下关键字是个好办法,如下图:
      

  9.   

    在Debug下面编译应该不会出现链接器怎么会提示是:LINK : fatal error LNK1104: 无法打开文件“GuiTk115d.lib”这个错误。
      

  10.   

    _AFXDLL不用管它,很明显如果你用 #if defined _AFXDLL && !defined _GUILIB_STATIC_ ,在_GUILIB_STATIC_没有被定义的情况下,会执行
    #ifdef GUILIB_EXPORTS
     #define GUILIBDLLEXPORT _declspec(dllexport)
    那么你使用class GUILIBDLLEXPORT clstmp{};这个类都会成为一个输出类。会生成lib和dll供外部使用。
      

  11.   

    你用vs系列打开工程,没有生效的代码是灰的,vc6在这方面是个不足。