Skip to main content
gamedev.net
🔒 Locked

Undefined Reference to CoInitialize using DevC++

Started by Si0n 9 replies 12.3k views
Original Post
Si0n
Si0n
Title is pretty self-explanatory, but this is what I'm getting

Compiler: Default compiler
Building Makefile: "C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Makefile.win"
Finding dependencies for file: C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Main.cpp
Executing  make...
make.exe -f "C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Makefile.win" all
g++.exe Main.o  -o "DMusic.exe" -L"C:/DEV-CPP/lib" -mwindows ../../../../../Dev-Cpp/lib/libddraw.a ../../../../../Dev-Cpp/lib/libdinput.a ../../../../../Dev-Cpp/lib/libdinput8.a ../../../../../Dev-Cpp/lib/libdsound.a ../../../../../Dev-Cpp/lib/libdxguid.a  

Main.o(.text+0x10de):Main.cpp: undefined reference to `CoCreateInstance@20'
Main.o(.text+0x1132):Main.cpp: undefined reference to `CoCreateInstance@20'
Main.o(.text+0x1471):Main.cpp: undefined reference to `CoUninitialize@0'
Main.o(.text+0x1497):Main.cpp: undefined reference to `CoInitialize@4'

Execution terminated

Which libraries should I include to get it to work? Here is the source in case you are curious

//#define WIN32_LEAN_AND_MEAN
#define INITGUID

#include <windows.h>   
#include <windowsx.h>
 
#include <mmsystem.h>
#include <conio.h>
#include <cstdlib>
#include <memory>
#include <cstdarg>
#include <cstdio> 
#include <cmath>
#include <io.h>
#include <direct.h>
#include <cwchar>
#include <fcntl.h>

#include <ddraw.h>
#include <dsound.h>
#include <dmksctrl.h>
#include <dmusici.h>
#include <dmusicc.h>
#include <dmusicf.h>

#ifndef DSBCAPS_CTRLDEFAULT
#define DSBCAPS_CTRLDEFAULT (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME )
#endif

#define	class_name TEXT("Direct Music Test")

HWND		  		g_hWnd   	= NULL;
HINSTANCE			g_hInstance	= NULL;

IDirectMusicPerformance    *lpdmperf 		= NULL;
IDirectMusicLoader         *lpdmloader 		= NULL;
IDirectMusicSegment        *lpdmsegment 	= NULL;
IDirectMusicSegmentState   *lpdmsegstate 	= NULL;

#define ddrawInitStructure(ddstruct) { memset(&ddstruct, 0, sizeof(ddstruct)); ddstruct.dwSize = sizeof(ddstruct); }
#define multibyteToWide(x, y) MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, y, -1, x, _MAX_PATH); 

inline int keyUp(int key)		{ return !(GetAsyncKeyState(key) & 0x8000);	}
inline int keyDown(int key)		{ return  (GetAsyncKeyState(key) & 0x8000);	}

IDirectMusicPerformance *initPerformance();
IDirectMusicLoader *initLoader();
IDirectMusicSegment *loadMIDI(IDirectMusicPerformance*, IDirectMusicLoader*, WCHAR*);

HRESULT freeDirectMusic();

int game_init(int = 0, void* = NULL);
int game_main(int = 0, void* = NULL);
int game_over(int = 0, void* = NULL);

LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR cmdLine, int cmdShow)
{
 	g_hInstance = hInstance;
 	
 	WNDCLASSEX wndclsEx;
 	
 	wndclsEx.hInstance		= hInstance;
 	wndclsEx.lpfnWndProc	= wndProc;
 	wndclsEx.style			= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
 	
 	wndclsEx.hCursor		= LoadCursor(0, IDC_ARROW);
 	wndclsEx.hIcon			= LoadIcon(0, IDI_APPLICATION);
 	wndclsEx.hIconSm		= LoadIcon(0, IDI_WINLOGO);
 	wndclsEx.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
 	
 	wndclsEx.lpszMenuName	= NULL;
 	wndclsEx.lpszClassName	= class_name;
 	
 	wndclsEx.cbSize			= sizeof(WNDCLASSEX);
 	wndclsEx.cbWndExtra		= 0;
 	wndclsEx.cbClsExtra		= 0;
 	
 	if (!RegisterClassEx(&wndclsEx))
 	{
		MessageBox(0, TEXT("Could not register window class."), class_name, MB_OK | MB_ICONERROR);
		
		return 1;
	}
	
	HWND hWnd = CreateWindowEx(0, class_name, class_name, WS_OVERLAPPEDWINDOW, 
		 	  					  CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
 								  CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
 								  
    if (!hWnd)
  	{
		MessageBox(0, TEXT("Could not register create window."), class_name, MB_OK | MB_ICONERROR);
		
		UnregisterClass(class_name, hInstance);
		
		return 1;
	}
	
	g_hWnd = hWnd;
	
	ShowWindow(hWnd, cmdShow);
	UpdateWindow(hWnd);
	
	if (!game_init())
  	{		
		UnregisterClass(class_name, hInstance);
		
		return 1;
	}
	
	MSG msg;
	
	while (1)
	{
        if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;
                
            TranslateMessage(&msg);
            DispatchMessage(&msg);
		}
		    
        if (!game_main())
            break;
	}
	
	UnregisterClass(class_name, hInstance);
	           
    return (game_over() ? msg.wParam : 1);
}

IDirectMusicPerformance *initPerformance()
{
    IDirectMusicPerformance *pPerf = NULL;
    
    if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, 
       												IID_IDirectMusicPerformance, (void **)&pPerf)))
        return NULL;
        
    return pPerf;
}

IDirectMusicLoader *initLoader()
{
    IDirectMusicLoader *pLoader = NULL;
    
    if (FAILED(CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
       												IID_IDirectMusicLoader, (void **)&pLoader)))
        return NULL;
        
    return pLoader;
}

IDirectMusicSegment *loadMIDI(IDirectMusicPerformance *pPerf, IDirectMusicLoader *pLoader, 
													  		  					 WCHAR *file_name)
{
    IDirectMusicSegment *pSegment = NULL;
    
    char  szDir[_MAX_PATH];
    WCHAR wzDir[_MAX_PATH];
    
    if (!_getcwd(szDir, _MAX_PATH))
        return NULL;
        
    multibyteToWide(wzDir, szDir);
    
    if (FAILED(pLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wzDir, false)))
        return NULL;
    
	DMUS_OBJECTDESC objDesc;    
    memset(&objDesc, 0, sizeof(DMUS_OBJECTDESC));
    
    objDesc.dwSize		= sizeof(DMUS_OBJECTDESC);
    objDesc.guidClass 	= CLSID_DirectMusicSegment;
    wcscpy(objDesc.wszFileName, file_name);
    objDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME;
    
    if (FAILED(pLoader->GetObject(&objDesc, IID_IDirectMusicSegment, (void **)&pSegment)))
        return NULL;
        
    if (FAILED(pSegment->SetParam(GUID_StandardMIDIFile, -1, 0, 0, (void *)pPerf)))
        return NULL;
        
    if (FAILED(pSegment->SetParam(GUID_Download, -1, 0, 0, (void *)pPerf)))
        return NULL;
        
    return pSegment;
}

HRESULT freeDirectMusic()
{
    lpdmperf->Stop(NULL, NULL, 0, 0);
    
    lpdmsegment->SetParam(GUID_Unload, -1, 0, 0, (void *)lpdmperf);
    lpdmsegment->Release();
    lpdmsegment = NULL;
    
    lpdmperf->CloseDown();
    lpdmperf->Release();
    lpdmperf = NULL;
    
    lpdmloader->Release();
    lpdmloader = NULL;
    
    CoUninitialize();
    
    return S_OK;
}

int game_init(int nParams, void *pParams)
{
 	srand(GetTickCount());
 	
 	if (FAILED(CoInitialize(NULL)))
 	    return 0;
 	    
    if (!(lpdmperf = initPerformance()))
        return 0;
    
	if (FAILED(lpdmperf->Init(NULL, NULL, g_hWnd)))
	    return 0;
		
	if (FAILED(lpdmperf->AddPort(NULL)))
	    return 0;
		     
    if (!(lpdmloader = initLoader()))
        return 0;
        
    if (lpdmsegment)
    {
	    lpdmsegment->Release();
	    lpdmsegment = NULL;
	}
	
	ShowCursor(0);
	
	return 1;
}

int game_main(int nParams, void *pParams)
{	
	static int window_closed = 0;
	
 	if (window_closed)
 	    return 1;
 	    
 	if (keyDown(VK_ESCAPE))
 	{
        window_closed = 1;
        
		SendMessage(g_hWnd, WM_DESTROY, 0, 0);   
	}
 	
    return 1;
}

int game_over(int nParams, void *pParams)
{	    	
	if (FAILED(freeDirectMusic()))
	    return 0;
	
	return 1;
}

LRESULT CALLBACK wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {	  	
    case WM_CLOSE: 
		 
		if (MessageBox(hWnd, TEXT("Going so soon?"), class_name, MB_YESNO | MB_ICONWARNING) != IDYES)
		    return 0;
		  
    case WM_DESTROY:
		 
        PostQuitMessage(0);
        
        return 0;
	}
	
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

RhoneRanger
RhoneRanger
I BELIEVE with CoCreateInstance you need to link dxguid.lib. (I am pretty sure on this part.)

I am not 100% certain anymore (it has been a while) but I think somewhere in your app you have to

#define INITGUID or something like that. (Not sure if that is still needed.)

This guy explains the whole process if any other questions.

EDIT: I see you already defined INITGUID. Now all that is left is to link with dxguid.lib.
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
Si0n
Si0n
Unfortunately, it still doesn't workk. If anybody wouldn't mind running the source code on their dev-c++ compiler, that would be great, cuz it might just be me or something... :(
Si0n
Si0n
Sorry for the incovenience, but I really can't seem to find the solution to my problem, and it's driving me insane. =/
RhoneRanger
RhoneRanger
All I can say is it is a library not being linked. I dont remember all the libs you need for direct music, as I no longer use that myself. dxguid , winmm, dmusici (or something like that) and another lib for dmusic that I don't recall the name too.


You could also make sure ole32.lib, as that also is needed for com.
Perhaps if you listed your libs you use, we could help eliminate it.
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
Si0n
Si0n
Linking the libole32.a solved it, thanks a lot! =)
lack o comments
lack o comments
[edit]oops, I a little late, oh well ;) [/edit]
Hmm, never seen that before. CoCreateInstance is included in the header "objbase.h" and the DLL "ole32.dll" (which may imply ole32.lib, as RhoneRanger stated).


Quote:

I BELIEVE with CoCreateInstance you need to link dxguid.lib. (I am pretty sure on this part.)

I am not 100% certain anymore (it has been a while) but I think somewhere in your app you have to

#define INITGUID or something like that. (Not sure if that is still needed.)

This is definately not the case. dxguid.lib and INITGUID essentially do the same thing. They define GUIDs(globaly unique identifiers) for the various DirectX interfaces. CoCreateInstance is a function used in COM, not and interface, much less, one for DirectX.

Also, there are no .libs for DirectMusic, because it contains no functions. Everything is entirely COM based and all calls are wrapped in interfaces.
RhoneRanger
RhoneRanger
Quote:


Also, there are no .libs for DirectMusic, because it contains no functions. Everything is entirely COM based and all calls are wrapped in interfaces.


Interesting how I do not see

IDirectMusicLoader::GetObject

or

IDirectMusicLoader::GetLoader

functions in ole. I wonder if there is a library for this.. Could it possibly be that there is a Dmusic.lib??

Just Curious

OH, and

Quote:


dxguid.lib and INITGUID essentially do the same thing.


I wonder if this is true. I ask you to type OR even #define INITGUID without linking dxguid.lib, see if any different results occur.

It is amazing how brown hairy orafaces can talk these days.


lack o comments: IF you are going to rebuke someone, make sure you know of what you speak.

[Edited by - RhoneRanger on February 20, 2005 4:28:50 AM]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
lack o comments
lack o comments
There's no need for insults =) I wasn't, and am still not trying to be rude.

I really don't know where the .lib for DirectMusic/Aduio is. All I know is that there is not a DMusic.lib. Maybe the code is in DXGUID.lib?

As far as INITGUID...I do know of what I am speaking. At least I think I do. I define it BEFORE all headers in a single source file, and everything works. Right? No need to link to DXGUID.lib because it's done for me.

If I have some misunderstanding of this, please tell me what I have gotten wrong. Don't just make sarcastic remarks and insults.
Muhammad Haggag
Muhammad Haggag
Quote:
Original post by lack o comments
Don't just make sarcastic remarks and insults.

Seconded. RhoneRanger, this isn't the first time you flame people for no obvious reason. 'lack o comments' is a very helpful and respectable member in this forum. Please behave yourself.

lack o comments
lack o comments
Smacks head. I completely forgot.

Just for the record. All DirectX interface code is stored in the run-time DLL's.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.