error LNK2019: unresolved external symbol Direct Sound

1.5k views Asked by At

So I'm reading "Beginning Game Programming Third Edition" by Jonathan S. Harbour, and I've gotten to the point where he teaches us how to use Direct Sound. The book uses it's own DirectSound.h and DirectSound.cpp files, which were from a previous release of the DirectX SDK, but when I try to compile I get the "LNK2019: unresolved external symbol" error.

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)

I have not implemented any Direct Sound code in my project as yet, the mere presence of these files causes the project to not compile. Without them, the project compiles and runs perfectly.

#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;
}
1

There are 1 answers

0
Roman Ryltsov On BEST ANSWER

Error LNK2019 is about adding a missing library - a typical problem. You should identify missing symbols, then identify library to additionally link, then add it using #pragma or via project settings.

Also as it's a beginner question, most likely Stack Overflow already has something closely related. Be always sure to run a search for it, compare code snippets to yours.

Related questions show that you need dsound.lib and dxerr.lib to be linked in.