error LNK2019: 未解析的外部符号 Direct Sound

error LNK2019: unresolved external symbol Direct Sound

所以我正在阅读 Jonathan S. Harbour 的 "Beginning Game Programming Third Edition",我已经到了他教我们如何使用 Direct Sound 的地步。这本书使用它自己的 DirectSound.h 和 DirectSound.cpp 文件,这些文件来自以前版本的 DirectX SDK,但是当我尝试编译时出现 "LNK2019: unresolved external symbol" 错误。

1>DirectSound.obj : error LNK2019: unresolved external symbol _DXTraceA@20 referenced in function "public: long thiscall CSoundManager::Initialize(struct HWND *,unsigned long)" (?Initialize@CSoundManager@@QAEJPAUHWND__@@K@Z)

1>DirectSound.obj : error LNK2019: unresolved external symbol _DirectSoundCreate8@12 referenced in function "public: long thiscall CSoundManager::Initialize(struct HWND *,unsigned long)" (?Initialize@CSoundManager@@QAEJPAUHWND__@@K@Z)

我还没有在我的项目中实现任何 Direct Sound 代码,仅仅存在这些文件就会导致项目无法编译。没有它们,项目编译和运行完美。

#ifndef DSUTIL_H
#define DSUTIL_H

#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>
#include <dsound.h>

class CSoundManager;
class CSound;
class CStreamingSound;
class CWaveFile;

#define WAVEFILE_READ   1
#define WAVEFILE_WRITE  2

#define DSUtil_StopSound(s)         { if(s) s->Stop(); }
#define DSUtil_PlaySound(s)         { if(s) s->Play( 0, 0 ); }
#define DSUtil_PlaySoundLooping(s)  { if(s) s->Play( 0, DSBPLAY_LOOPING ); }


//-----------------------------------------------------------------------------
// Name: class CSoundManager
// Desc: 
//-----------------------------------------------------------------------------
class CSoundManager
{
protected:
    LPDIRECTSOUND8 m_pDS;

public:
    CSoundManager();
    ~CSoundManager();

    HRESULT Initialize(HWND  hWnd, DWORD dwCoopLevel);
    inline  LPDIRECTSOUND8 GetDirectSound() { return m_pDS; }
    HRESULT SetPrimaryBufferFormat( DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );

    HRESULT Create( CSound** ppSound, LPTSTR strWaveFileName, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
};




//-----------------------------------------------------------------------------
// Name: class CSound
// Desc: Encapsulates functionality of a DirectSound buffer.
//-----------------------------------------------------------------------------
class CSound
{
protected:
    LPDIRECTSOUNDBUFFER* m_apDSBuffer;
    DWORD                m_dwDSBufferSize;
    CWaveFile*           m_pWaveFile;
    DWORD                m_dwNumBuffers;
    DWORD                m_dwCreationFlags;

    HRESULT RestoreBuffer( LPDIRECTSOUNDBUFFER pDSB, BOOL* pbWasRestored );

public:
    CSound( LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, DWORD dwNumBuffers, CWaveFile* pWaveFile, DWORD dwCreationFlags );
    virtual ~CSound();

    HRESULT FillBufferWithSound( LPDIRECTSOUNDBUFFER pDSB, BOOL bRepeatWavIfBufferLarger );
    LPDIRECTSOUNDBUFFER GetFreeBuffer();

    HRESULT Play( DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lVolume = 0, LONG lFrequency = -1, LONG lPan = 0 );
    HRESULT Stop();
    HRESULT Reset();
    BOOL    IsSoundPlaying();
};


//-----------------------------------------------------------------------------
// Name: class CWaveFile
// Desc: Encapsulates reading or writing sound data to or from a wave file
//-----------------------------------------------------------------------------
class CWaveFile
{
public:
    WAVEFORMATEX* m_pwfx;        // Pointer to WAVEFORMATEX structure
    HMMIO         m_hmmio;       // MM I/O handle for the WAVE
    MMCKINFO      m_ck;          // Multimedia RIFF chunk
    MMCKINFO      m_ckRiff;      // Use in opening a WAVE file
    DWORD         m_dwSize;      // The size of the wave file
    MMIOINFO      m_mmioinfoOut;
    DWORD         m_dwFlags;
    BOOL          m_bIsReadingFromMemory;
    BYTE*         m_pbData;
    BYTE*         m_pbDataCur;
    ULONG         m_ulDataSize;
    CHAR*         m_pResourceBuffer;

protected:
    HRESULT ReadMMIO();
    HRESULT WriteMMIO( WAVEFORMATEX *pwfxDest );

public:
    CWaveFile();
    ~CWaveFile();

    HRESULT Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags );
    HRESULT Close();

    HRESULT Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead );
    HRESULT Write( UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote );

    DWORD   GetSize();
    HRESULT ResetFile();
    WAVEFORMATEX* GetFormat() { return m_pwfx; };
};
#endif // DSUTIL_H
///////////////////////////////////////////////////////////////////////////////
// The function that causes the error

HRESULT CSoundManager::Initialize(HWND hWnd, DWORD dwCoopLevel)
{
    HRESULT hr;

    SAFE_RELEASE(m_pDS);

    // Create IDirectSound using the primary sound device
    if(FAILED(hr = DirectSoundCreate8(NULL, &m_pDS, NULL)))
        return DXTRACE_ERR(TEXT("DirectSoundCreate8"), hr);

    // Set DirectSound coop level 
    if( FAILED( hr = m_pDS->SetCooperativeLevel( hWnd, dwCoopLevel ) ) )
        return DXTRACE_ERR( TEXT("SetCooperativeLevel"), hr );   

    return S_OK;
}

错误 LNK2019 与添加缺少的库有关 - 一个典型问题。您应该识别缺失的符号,然后识别库以另外 link,然后使用 #pragma 或通过项目设置添加它。

此外,由于这是一个初学者问题,很可能 Stack Overflow 已经有一些密切相关的东西。请务必 运行 搜索它,将代码片段与您的代码片段进行比较。

  • Application works in visual studio but release/debug exe does not
  • Which header should I include for DXTrace?

相关问题显示您需要 dsound.lib 和 dxerr.lib 才能 linked。