一千萬個為什麽

搜索

Visual Studio,MSBuild:在構建成功後,在幹凈失敗後首次構建

我的構建顯然有問題,但我無法理解。我把它縮小到我的一個項目:第一次構建在clean之後失敗,所有後續構建成功。

我得到鏈接錯誤,說已經定義了一些符號:

>------ Build started: Project: Problem, Configuration: Debug Win32 ------
> Blah blah blah...
23>     Creating library D:\SVN.DRA.WorkingCopy\Debug\Problem.lib and object D:\SVN.DRA.WorkingCopy\Debug\Problem.exp
23>ProblemDependency1.lib(PD1.obj) : error LNK2005: "public: unsigned short __thiscall PD2Class::getFoo(void)const " ([email protected]@@QBEGXZ) already defined in ProblemDependecy2.lib(ProblemDependency2.dll)
23>ProblemDependency1.lib(PD1.obj) : error LNK2005: "public: void __thiscall PD2Class2::`default constructor closure'(void)" ([email protected]@[email protected]@QAEXXZ) already defined in ProblemDependency2.lib(ProblemDependency2.dll)
23>D:\SVN.DRA.WorkingCopy\Debug\Problem.dll : fatal error LNK1169: one or more multiply defined symbols found
  • 問題是一個C ++/CLI項目,使用/ clr開關構建,它引用了非托管C ++項目ProblemDependency1,一個靜態庫,以及ProblemDependency2,一個dll。
  • ProblemDependency1引用ProblemDependency2。
  • getFoo()聲明為內聯並在類聲明之外定義,在.h
  • PD2Class2沒有明確定義的默認構造函數,但是它有一個構造函數,它有所有默認參數,所以你可以說它包含默認構造函數作為特例
  • 定義這些內容的.h將#pragma once作為第一行。

對此進行故障排除的任何提示?如果需要,我可以發布更多信息

Update: I solved the first error thanks to Anders Abel's suggestion, but I still can't solve the second one (the one about the default constructor)

Update: If I compile using MSBuild outside Visual Studio, it fails always, with the same error

Edit: Here's some code. First, a bit of PD2Class2's declaration. PD2Class2's real name is CImage (feeling lazy to anonymize), CImage.h:

#pragma once
#pragma warning( disable: 4251 )    //TODO: Disable and solve

#include "ImageProperties.h"
#include "../CommonCppLibrary/CriticalSection.h"
#include 
#include 
#include 

class CSharedMemory;
class EmptyImageException;
struct IShape;

struct SImageStatics {
    unsigned short low3Percentile;
    unsigned short high97Percentile;
    unsigned short medianPixelValue;
    unsigned short meanPixelValue;
    unsigned short minPixelValue;
    unsigned short maxPixelValue;
};

namespace DRA{
namespace Image{
class __declspec(dllexport) CImage {
    friend class CImageLock;

//Attributes
    int m_iPitch;
protected:
    mutable CImageProperties                    m_cProperties;
    CSharedMemory *                             m_pSharedMemmory;
    mutable DRA::CommonCpp::CCriticalSection    m_csData;
    static const float                          PIXEL_FREQUENCY_COVERAGE;
    static const float                          PIXEL_CUTOFF_PERCENTAGE;
    static const int                            MINIMUM_PIXEL_FREQUENCY;    //Pixels with a frequency lower than this are ignored
    static const int                            MINIMUM_WINDOW_WIDTH_FOR_16_BITS;

//Methods
    //Some private methods

public:

    CImage( DWORD dwWidth = 0, DWORD dwHeight = 0, ULONG uBytesPerPixel = 0,
            bool isSigned = false, EPhotometricInterpretation ePI = PI_UNKNOWN,
            UINT bitsStored = 0, float pw = -1.0f, float ph = -1.0f, BYTE * pData = NULL );
    CImage( const CImageProperties& cProperties, int iPitch = 0 );
    CImage( const CImage& rImage );
    virtual ~CImage();
    virtual CImage& operator=( const CImage& );
    bool operator==( const CImage& rImage );

//Alter State
    //More methods
//Query State
    //More methods
};
}
}

接下來,構造函數的定義來自CImage.cpp:

CImage::CImage( DWORD dwWidth, DWORD dwHeight, ULONG uBytesPerPixel, bool isSigned,
                EPhotometricInterpretation ePI, UINT bitsStored, float pw, float ph,
                BYTE * pData ) :
        m_iPitch( dwWidth * uBytesPerPixel ),
        m_cProperties( dwWidth, dwHeight, uBytesPerPixel, bitsStored, ePI, isSigned, pw, ph ),
        m_pSharedMemmory( NULL ),
        m_csData(){
    m_pSharedMemmory = new CSharedMemory( pData ? pData : new BYTE[getSize()] );
}

最佳答案

我沒有太多的c ++經驗,但在其他.NET語言中這樣的問題,通常是由於在同一個解決方案中對另一個項目的DLL引用(到另一個項目的“obj”或“bin”文件夾中的DLL)這使得Visual Studio無法找出構建順序,因此,在“幹凈”之後第一次,你將不會擁有你依賴的DLL。在第二次構建時,這個DLL已經構建完成,構建將成功。

轉載註明原文: Visual Studio,MSBuild:在構建成功後,在幹凈失敗後首次構建