竟然有这样的连接错误!:-< 500分!

BlackSword 2000-02-18 02:23:00
在下用MFC制作COM时,竟出现了奇怪的连接错误:

部分原文件:
//1.GUIDs.h

#ifndef GUID_H
#define GUID_H

// ComMyComTest Class GUID
// {E6C202F1-E5E7-11d3-8433-00C02600C221}
DEFINE_GUID(CLSID_MyComTest,
0xe6c202f1, 0xe5e7, 0x11d3, 0x84, 0x33, 0x0, 0xc0, 0x26, 0x0, 0xc2, 0x21);

// IMyComTest Interface GUID
// {E6C202F2-E5E7-11d3-8433-00C02600C221}
DEFINE_GUID(IID_IMyComTest,
0xe6c202f2, 0xe5e7, 0x11d3, 0x84, 0x33, 0x0, 0xc0, 0x26, 0x0, 0xc2, 0x21);
#endif

//2.IMyComTest

#include "GUIDs.h"

// New interface declaration
DECLARE_INTERFACE_(IMyComTest, IUnknown)
{
// IFortuneTeller
STDMETHOD(ShowDBResult)() PURE;
};

typedef IMyComTest* PIMyComTest;


//3.ComMyComTest.h

......
#include "IMyComTest.h"
......

//4.ComMyComTest.cpp

// ComMyComTest.cpp : implementation file
//

#include "stdafx.h"
#include "ComTest.h"
#include "ComMyComTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// ComMyComTest

// Support dynamic creation
IMPLEMENT_DYNCREATE(ComMyComTest, CCmdTarget)

// Support a class factory
IMPLEMENT_OLECREATE(ComMyComTest,"MyComTest",
0xe6c202f1, 0xe5e7, 0x11d3, 0x84, 0x33, 0x0, 0xc0, 0x26, 0x0, 0xc2, 0x21);

// Map the various interfaces onto the nested interface classes
BEGIN_INTERFACE_MAP(ComMyComTest,CCmdTarget)
INTERFACE_PART(ComMyComTest,IID_IMyComTest,CInnerMyComTest)
// ^^^^^^^^^^^^^^
// ^^^^^^^^^^^^^^

END_INTERFACE_MAP()

......


编译已通过,可连接时出现以下错误:
--------------------Configuration: ComTest - Win32 Debug--------------------
Linking...
Creating library Debug/ComTest.lib and object Debug/ComTest.exp
ComMyComTest.obj : error LNK2001: unresolved external symbol _IID_IMyComTest
Debug/ComTest.dll : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

ComTest.dll - 2 error(s), 0 warning(s)



问题是下画^^^^^^^^^^线的IID_IMyComTest不认识。

不知哪位大侠肯赐教!500分哟!!!
...全文
298 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
土豆 2000-02-18
  • 打赏
  • 举报
回复
关注
netmare 2000-02-18
  • 打赏
  • 举报
回复
在MFC中有如下定义(objbase.h)
// macros to define byte pattern for a GUID.
// Example: DEFINE_GUID(GUID_XXX, a, b, c, ...);
//
// Each dll/exe must initialize the GUIDs once. This is done in one of
// two ways. If you are not using precompiled headers for the file(s) which
// initializes the GUIDs, define INITGUID before including objbase.h. This
// is how OLE builds the initialized versions of the GUIDs which are included
// in ole2.lib. The GUIDs in ole2.lib are all defined in the same text
// segment GUID_TEXT.
//
// The alternative (which some versions of the compiler don't handle properly;
// they wind up with the initialized GUIDs in a data, not a text segment),
// is to use a precompiled version of objbase.h and then include initguid.h
// after objbase.h followed by one or more of the guid defintion files.

#ifndef INITGUID
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID FAR name
#else

#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
#endif // INITGUID
所以如果没定义INITGUID就会产生dzm_xy说的那种错误
INITGUID挺不好用的,很容易产生重复定义错
不如将你用DEFINE_GUID形式的GUID改为这种形式
// {A7519441-E62B-11d3-AEBA-8BA541E27461}
static const GUID IID_IMyComTest=
{ 0xe6c202f2, 0xe5e7, 0x11d3, { 0x84, 0x33, 0x0, 0xc0, 0x26, 0x0, 0xc2, 0x21} };
dzm_xy 2000-02-18
  • 打赏
  • 举报
回复
编译通过了不代表连接时肯定没问题,例如:

#include <...>

extern int myvar;

void main()
{
myvar = 0;
}

上述这段程序编译编译绝对通过,但连接时肯定出错,因为找不到变量myvar在哪个模块中。BlackSword遇到的就是这类情况,解决办法是必须在项目中加入_IID_IMyComTest
符号所在的模块(.lib或.obj)文件文件名,或在project-setting的连接库设置中加入该模块文件名。
DOU 2000-02-18
  • 打赏
  • 举报
回复
MSDN对该错误的完整描述:
逐个检查,我怀疑很可能是 少了extern "C"
If you are using C++, make sure to use extern “C” when calling a C function from a C++ program. By using extern “C” you force the use of the C naming convention"

Linker Tools Error LNK2001
unresolved external symbol "symbol"

Code will generate this error message if it references something (like a function, variable, or label) that the linker can’t find in all the libraries and object files it searches. In general, there are two reasons this error occurs: what the code asks for doesn’t exist (the symbol is spelled incorrectly or uses the wrong case, for example), or the code asks for the wrong thing (you are using mixed versions of the libraries?some from one version of the product, others from another version).

Numerous kinds of coding and build errors can cause LNK2001. Several specific causes are listed below, and some have links to more detailed explanations.

Coding Problems

Mismatched case in your code or module-definition (.DEF) file can cause LNK2001. For example, if you named a variable “var1” in one C++ source file and tried to access it as “VAR1” in another, you would receive this error. The solution is to exactly match the case of the symbol in all references.


A project that uses function inlining yet defines the functions in a .CPP file rather than in the header file can cause LNK2001.


If you are using C++, make sure to use extern “C” when calling a C function from a C++ program. By using extern “C” you force the use of the C naming convention. Be aware of compiler switches like /Tp or /Tc that force a file to be compiled as a C (/Tc) or C++ (/Tp) file no matter what the filename extension, or you may get different function names than you expect.


Attempting to reference functions or data that don't have external linkage causes LNK2001. In C++, inline functions and const data have internal linkage unless explicitly specified as extern.


A missing function body or variable will cause LNK2001. Having just a function prototype or extern declaration will allow the compiler to continue without error, but the linker will not be able to resolve your call to an address or reference to a variable because there is no function code or variable space reserved.


Name decoration incorporates the parameters of a function into the final decorated function name. Calling a function with parameter types that do not match those in the function declaration may cause LNK2001.


Incorrectly included prototypes will cause the compiler to expect a function body that is not provided. If you have both a class and non-class implementation of a function F, beware of C++ scope-resolution rules.


When using C++, make sure that you include the implementation of a specific function for a class and not just a prototype in the class definition.


Attempting to call a pure virtual function from the constructor or destructor of an abstract base class will cause LNK2001 since by definition a pure virtual function has no base class implementation.


Only global functions and variables are public.
Functions declared with the static modifier by definition have file scope. Static variables have the same limitation. Trying to access any static variables from outside of the file in which they are declared can result in a compile error or LNK2001.

A variable declared within a function (a local variable) can only be used within the scope of that function.

C++ global constants have static linkage. This is different than C. If you try to use a global constant in C++ in multiple files you get error LNK2001. One alternative is to include the const initializations in a header file and include that header in your .CPP files when necessary, just as if it was a function prototype. Another alternative is to make the variable non-constant and use a constant reference when assessing it.

Compiling and Linking Problems

The names of the Microsoft run-time and MFC libraries needed at link time are included in the object file module by the Microsoft compiler. If you use the /NOD (/NODEFAULTLIB) option, these libraries will not be linked into the project unless you have explicitly included them. Using /NOD will cause error LNK2001 in this case.


If you are using Unicode and MFC, you will get an unresolved external on _WinMain@16 if you don’t create an entrypoint to wWinMainCRTStartup. Use the /ENTRY option or type this value in the Project Settings dialog box. (To find this option in the development environment, click Settings on the Project menu, then click the Link tab, and click Output in the Category box.) See Unicode Programming Summary.

See the following Knowledge Base articles located in the Online Information System for more information. An easy way to reach an article is to copy a "Q" number above, open the Search dialog box from the Help menu and select the Query tab, then paste the number into the first text box and press ENTER.
Q125750 "PRB: Error LNK2001: '_WinMain@16': Unresolved External Symbol"


Q131204 "PRB: Wrong Project Selection Causes LNK2001 on _WinMain@16"


Q100639 "Unicode Support in the Microsoft Foundation Class Library"
Linking code compiled with /MT with the library LIBC.LIB causes LNK2001 on _beginthread, _beginthreadex, _endthread, and _endthreadex.


When compiling with /MD, a reference to "func" in your source becomes a reference "__imp__func" in the object since all the run-time is now held within a DLL. If you try to link with the static libraries LIBC.LIB or LIBCMT.LIB, you will get LNK2001 on __imp__func. If you try to link with MSVCxx.LIB when compiling without /MD you will not always get LNK2001, but you will likely have other problems.


Linking code compiled with an explicit or implicit /ML to the LIBCMT.LIB causes LNK2001 on _errno.


Linking with the release mode libraries when building a debug version of an application can cause LNK2001. Similarly, using an /Mxd option (/MLd, /MTd, or /MDd) and/or defining _DEBUG and then linking with the release libraries will give you potential unresolved externals (among other problems). Linking a release mode build with the debug libraries will also cause similar problems.


Mixing versions of Microsoft libraries and compiler products can be problematic. A new compiler version's libraries may contain new symbols that cannot be found in the libraries included with previous versions. Use DUMPBIN to find out if a symbol is in a 32-bit object file or library.


There is currently no standard for C++ naming between compiler vendors or even between different versions of a compiler. Therefore linking object files compiled with other compilers may not produce the same naming scheme and thus cause error LNK2001.


Mixing inline and non-inline compile options on different modules can cause LNK2001. If a C++ library is created with function inlining turned on (/Ob1 or /Ob2) but the corresponding header file describing the functions has inlining turned off (no inline keyword), you will get this error. To prevent this problem, have the inline functions defined with inline in the header file you are going to include in other files.


If you are using the #pragma inline_depth compiler directive, make sure you have a value of 2 or greater set, and make sure you are using the /Ob1 or /Ob2 compiler option.


Omitting the LINK option /NOENTRY when creating a resource-only DLL will cause LNK2001.


Using incorrect /SUBSYSTEM or /ENTRY settings can cause LNK2001. For example, if you write a character-based application (a console application) and specify /SUBSYSTEM:WINDOWS, you will get an unresolved external for WinMain. For more information on these options and entry points, see the /SUBSYSTEM and /ENTRY linker options.
Export Problems

When you are porting an application from 16 to 32 bits, LNK2001 can occur. The current 32-bit module-definition (.DEF) file syntax requires that __cdecl, __stdcall, and __fastcall functions be listed in the EXPORTS section without underscores (undecorated). This differs from the 16-bit syntax, where they must be listed with underscores (decorated). For more information, see the description of the EXPORTS section of module-definition files.


Any export listed in the .DEF file and not found will cause LNK2001. This could be because it does not exist, is spelled incorrectly, or uses decorated names (.DEF files do not take decorated names).
This error message is followed by fatal error LNK1120.
netmare 2000-02-18
  • 打赏
  • 举报
回复
在头文件中加入
#define INITGUID
#include <initguid.h>
cloud 2000-02-18
  • 打赏
  • 举报
回复
"IID_IMyComTest"是什么东西,好象错误提示是不认识"_IID_IMyComTest"

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧