2

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;
}
Roman R.
  • 68,205
  • 6
  • 94
  • 158
L.Moyer
  • 81
  • 1
  • 10
  • `1` you should have included complete error message `2` add `#pragma comment(lib, "dsound.lib");` line like this: http://stackoverflow.com/questions/15543918/application-works-in-visual-studio-but-release-debug-exe-does-not – Roman R. Dec 17 '16 at 14:33
  • I forgot to add that. It's been edited to list the error. – L.Moyer Dec 17 '16 at 14:39
  • Adding .lib (see above) will fix the second error. For the first one, see http://stackoverflow.com/questions/9392252/which-header-should-i-include-for-dxtrace – Roman R. Dec 17 '16 at 14:41
  • 1
    Thank you! I hadn't even thought to add the .lib files to the file, let alone realize that the DxErr.lib file would need to be included. It compiles now, and I can finally code some Direct Sound. Thanks again! – L.Moyer Dec 17 '16 at 14:54
  • Keep in mind that DirectSound and the DirectX SDK itself is all legacy. See [Where is the DirectX SDK?](https://msdn.microsoft.com/en-us/library/windows/desktop/ee663275.aspx). – Chuck Walbourn Dec 18 '16 at 07:07
  • Yeah, I'm only using the June 2010 release of the DirectX SDK because the book uses DirectX 9, and recommended it so that I could follow along and run the proof of concept examples. I finished my Pong Clone after this question was resolved. – L.Moyer Dec 18 '16 at 19:54

1 Answers1

3

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.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158