IImage链接错误(转)

IImage链接错误(转)

TITLE:
//    IImage和链接错误
//AUTHOR:
//    norains
//DATE:
//    Tuesday 14-August-2007
//Environment:
//        EVC4.0 + Windows CE 5.0 Standard SDK
//========================================================================

    IImage组件是在wince5.0新加入的,它可以调用公共接口来显示多种图片格式(jpg,png,gif,bmp),并且还可以扩展用以支持更多图片.但也许是因为新加入的原因,尚存在不少问题,其中编译的error link 2005就令人非常头痛.
   
    首先让我们先来看看一个能够编译通过的最简单IImage的用法:

//////////////////////////////////////////////////////////////////////    
// TempApp.cpp : Defines the entry point for the application.
//
//////////////////////////////////////////////////////////////////////

#include “stdafx.h”
#include “initguid.h”
#include “imaging.h”

#pragma comment (lib,”Ole32.lib”)

void Open()
{

     IImage * pImage;
     IImagingFactory * pImagingFactory;

     HRESULT hr;

    if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
     {  
        goto END;
     }

    if(FAILED(hr = CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**) &pImagingFactory)))
     {
        goto END;
     }

    if(FAILED(hr = pImagingFactory->CreateImageFromFile(TEXT(“测试.bmp”), &pImage)))
     {
        goto END;
     }

END:
    if(pImage != NULL)
     {
         pImage->Release();
         pImage = NULL;
     }

    if(pImagingFactory != NULL)
     {
         pImagingFactory->Release();
         pImagingFactory = NULL;
     }

     CoUninitialize();

}

int WINAPI WinMain(     HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                    int        nCmdShow)
{
     // TODO: Place code here.

     Open();
    return 0;
}

    在这个文件中,我们需要注意两点
   
    1.头文件的包含次序.
     #include “initguid.h”一定要在#include “imaging.h”之前.如果不包含”initguid.h”或在#include “imaging.h”之后含,则编译时会出现错误:
     TempApp.obj : error LNK2001: unresolved external symbol CLSID_ImagingFactory
     TempApp.obj : error LNK2001: unresolved external symbol IID_IImagingFactory  
   
    2.”Ole32.lib”需要pragma
     如果”Ole32.lib”没有pragma的话,那么编译的时候将会出现如下错误:
     TempApp.obj : error LNK2019: unresolved external symbol __imp_CoUninitialize referenced in function “void __cdecl Open(void)” (?Open@@YAXXZ)
     TempApp.obj : error LNK2019: unresolved external symbol __imp_CoCreateInstance referenced in function “void __cdecl Open(void)” (?Open@@YAXXZ)
     TempApp.obj : error LNK2019: unresolved external symbol __imp_CoInitializeEx referenced in function “void __cdecl Open(void)” (?Open@@YAXXZ)
     MIPSIIDbg/TempApp.exe : fatal error LNK1120: 3 unresolved externals
    
    
    
    
     如果将IImage组件用类的形式封装,那么在头文件的包含顺序就更有讲究.
    
     这个是一个完整的能通过编译的例子:
    

//////////////////////////////////////////////////////////////////////     
// TempApp.cpp : Defines the entry point for the application.
//
//////////////////////////////////////////////////////////////////////

 

#include “stdafx.h”
#include “imgtest.h”

int WINAPI WinMain(     HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                    int        nCmdShow)
{
     // TODO: Place code here.

     CImgTest imgTest;
     imgTest.Open();
    return 0;
}

//////////////////////////////////////////////////////////////////////
// ImgTest.h: interface for the CImgTest class.
//
//////////////////////////////////////////////////////////////////////

 

#ifndef IMGTEST_H
#define IMGTEST_H

#include “imaging.h”

class CImgTest  
{
public:
    void Open();
     CImgTest();
    virtual ~CImgTest();

protected:
     IImage * pImage;
     IImagingFactory * pImagingFactory;
};

#endif //#ifndef IMGTEST_H

//////////////////////////////////////////////////////////////////////
// ImgTest.cpp: implementation of the CImgTest class.
//
//////////////////////////////////////////////////////////////////////

 

#include “stdafx.h”
#include “initguid.h”
#include “ImgTest.h”

#pragma comment (lib,”Ole32.lib”)

CImgTest::CImgTest()
{

}

CImgTest::~CImgTest()
{

}

void CImgTest::Open()
{

     HRESULT hr;

    if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
     {  
        goto END;
     }

    if(FAILED(hr = CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**) &pImagingFactory)))
     {
        goto END;
     }

    if(FAILED(hr = pImagingFactory->CreateImageFromFile(TEXT(“测试.bmp”), &pImage)))
     {
        goto END;
     }

END:
    if(pImage != NULL)
     {
         pImage->Release();
         pImage = NULL;
     }

    if(pImagingFactory != NULL)
     {
         pImagingFactory->Release();
         pImagingFactory = NULL;
     }

     CoUninitialize();
}

    CImgTest是将IImage COM封装的一个类,在这里例子里我们尤为要注意的是#include “initguid.h”的位置:位于”ImgTest.cpp”中,并且在”ImgTest.h”之前.看到这里,可能有的朋友会觉得奇怪,直接将#include “initguid.h”写在”ImgTest.h”中不行么?答案是否定的.
   
    我们可以更改头文件的包含,做个测试.
   
    更改后的头文件如下:

//////////////////////////////////////////////////////////////////////    
// ImgTest.h: interface for the CImgTest class.
//
//////////////////////////////////////////////////////////////////////

 

#ifndef IMGTEST_H
#define IMGTEST_H

#include “ImgTest.h”
#include “imaging.h”

class CImgTest  
{
public:
    void Open();
     CImgTest();
    virtual ~CImgTest();

protected:
     IImage * pImage;
     IImagingFactory * pImagingFactory;
};

#endif //#ifndef IMGTEST_H

    编译时将出现错误:
ImgTest.obj : error LNK2005: ENCODER_SAVE_FLAG already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_CHROMINANCE_TABLE already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_LUMINANCE_TABLE already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_TRANSFORMATION already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_ROTATION already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_QUALITY already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_RENDERMETHOD already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_VERSION already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_SCANMETHOD already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_COLORDEPTH already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ENCODER_COMPRESSION already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: DECODER_USEICC already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: DECODER_ICONRES already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: DECODER_OUTPUTCHANNEL already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: DECODER_TRANSRANGE already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: DECODER_TRANSCOLOR already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FMTID_JpegAppHeaders already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FMTID_ImageInformation already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FRAMEDIM_PAGE already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FRAMEDIM_RESOLUTION already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FRAMEDIM_TIME already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IID_IImageBytes already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IID_IBasicBitmapOps already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IID_IImageSink already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IID_IImageEncoder already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IID_IImageDecoder already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IID_IBitmapImage already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IID_IImage already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: CLSID_ImagingFactory already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IID_IImagingFactory already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_ICO already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_FLASHPIX already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_PHOTOCD already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_EXIF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_TIFF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_GIF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_PNG already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_JPEG already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_WMF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_EMF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_BMP already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_MEMORYBMP already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: IMGFMT_UNDEFINED already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: CodecIImageBytes already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderSaveFlag already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderChrominanceTable already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderLuminanceTable already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderTransformation already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderQuality already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderRenderMethod already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderVersion already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderScanMethod already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderColorDepth already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: EncoderCompression already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FormatIDJpegAppHeaders already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FormatIDImageInformation already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FrameDimensionPage already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FrameDimensionResolution already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: FrameDimensionTime already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatIcon already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatEXIF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatTIFF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatGIF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatPNG already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatJPEG already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatWMF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatEMF already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatBMP already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatMemoryBMP already defined in ImageIntroduce.obj
ImgTest.obj : error LNK2005: ImageFormatUndefined already defined in ImageIntroduce.obj

    那么在”ImgTest.cpp”中将#include “initguid.h”放置 #include “ImgTest.h”之后也不行么?答案依然是否定的.如果放在之后,则编译时会出现如下错误:
    ImgTest.obj : error LNK2001: unresolved external symbol CLSID_ImagingFactory
    ImgTest.obj : error LNK2001: unresolved external symbol IID_IImagingFactory
    MIPSIIDbg/ImageIntroduce.exe : fatal error LNK1120: 2 unresolved externals
   
   
    IImage的使用很简单也很方便,唯一需要注意的仅仅是#include “initguid.h”语句的位置而已,而这点却又往往让我们忽略,从而造就一些莫名其妙的链接错误.

你可能感兴趣的:(IImage链接错误(转))