Jump to content


Plagued by linking errors


  • You cannot reply to this topic
11 replies to this topic

#1 IrishFarmer

    Member

  • Members
  • PipPip
  • 43 posts

Posted 24 October 2005 - 09:22 PM

I've been writing my own 2D engine, albeit with a lot of help. However, I'm running into problems non-stop. I'm using the Windows API to make a Missile Command game as you might know. However, I'm getting link errors even from simple commands like PlaySound().

For christ's sake, its one line of code and my compiler won't let me link the executable. Anyway, more specifically I wrote the line:

PlaySound("Sound.wav", NULL, SND_ASYNC | SND_FILENAME);

And I get this error when I try and link:

Slideshow.obj : error LNK2001: unresolved external symbol __imp__PlaySoundA@12
Debug/Slideshow.exe : fatal error LNK1120: 1 unresolved externals

If I comment out the line then the program will compile and link and run just fine. So what's the deal? Does WindowsXP not support playing sounds from a filename? Because for my purposes (I'm not developing a professional application here) using sounds as a resource would be a waste of time. I'll do it if I have to, but I'd rather find another solution. Although to be honest, I'm not even sure that would work. Because it seems like Visual C++ is having a problem with any use of the PlaySound() function.

I'm wondering if I'm missing a library or something like that. I mean, including the Windows.h file should be enough, shouldn't it?

I don't know very much about link errors or what they mean.


edit:

Oh yeah. I included the file in the project directory, and then when that didn't work, I put the file into the actual folder containing the executable and still nothing.

#2 Mihail121

    Senior Member

  • Members
  • PipPipPipPip
  • 1052 posts

Posted 24 October 2005 - 09:33 PM

You have to link an extra library (winmm.lib) to your project since PlaySound is not defined in the Win32 libraries included by default. A simple line of code will do, since you are using Visual C++:

#pragma comment(lib, "winmm.lib")

And everything should run fine!

Cheers

#3 IrishFarmer

    Member

  • Members
  • PipPip
  • 43 posts

Posted 24 October 2005 - 09:42 PM

Ok. I figured I was missing a library, because I hadn't seen a link error like that since I was writing DirectX code. I was almost certain that PlaySound() didn't need a library. Maybe I should learn to trust my instincts a little bit less. Thank you! And also, I love you.

#4 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 24 October 2005 - 10:01 PM

Heya Irish.

In the spirit of 'giving' versus 'teaching' (with the suffix a-man-a-fish), I'm going to come in with the teaching side of it.

I'm sorry if I'm telling you this and you know it, I'm only guessing that you could use this explanation by the way you phrased your question. Please don't be offended if I'm reiterating what you already know, it's merely an attempt to help you out. :)

First off, MSDN/Google is your friend:

Google for "PlaySound msdn" and your first page will be this:

http://msdn.microsof...2_playsound.asp

At the bottom of which says you need the following requirements:

Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Mmsystem.h; include Windows.h.
Library: Use Winmm.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.

That said, that's how you can search for things in the future. In the meantime, I hope you don't mind if I dissect your primary post:

Quote

However, I'm getting link errors even from simple commands like PlaySound().

For christ's sake, its one line of code and my compiler won't let me link the executable.



I'm not trying to be pedantic, I just want to ensure that you have the terms straight in your head. That's not your compiler complaining, that's your linker. They're two different things (cl.exe vs. link.exe, for Visual Studio).

Linker errors signify that the compiler has already ran, and signed off saying, "Yep, this code is syntactically valid, behaves within the semantics set forth by C++, doesn't violate any const constraints etc."

What the compiler does *not* check for, is for implementations of things. The compiler will ensure that the functions you call do *exist* (that is, are 'declared'), but it won't check that they're *defined*. That's the linkers job.

... snip ...

Quote

I'm wondering if I'm missing a library or something like that. I mean, including the Windows.h file should be enough, shouldn't it?

I don't know very much about link errors or what they mean.

As you now know, you were missing a library. The reason is because not all the implementation was in the header, merely the declaration.

Anyways, I hope that helps. :)

#5 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 24 October 2005 - 10:04 PM

Mihail121 said:

You have to link an extra library (winmm.lib) to your project since PlaySound is not defined in the Win32 libraries included by default. A simple line of code will do, since you are using Visual C++:

#pragma comment(lib, "winmm.lib")

And everything should run fine!

Cheers

Furthermore, Irish Farmer, since I'm not sure if you know what this code is doing, it's basically injecting a message to the linker, informing it to link against that library when link time comes. Keep in mind, it's not portable to other compilers, it's very much cl.exe specific (not that that's a problem, if you're not writing for other compilers. :))

For more information about pragma comment, visit: http://msdn.microsof...dir_comment.asp

Cheers!

-e-

#6 IrishFarmer

    Member

  • Members
  • PipPip
  • 43 posts

Posted 24 October 2005 - 10:17 PM

That was a bit more information than I expected to get. But helpful nonetheless. I guess I didn't think about using google to search for errors, though I wasn't sure what it would turn up. Mayhaps that'll keep me from bugging you guys in the future.

Furthermore, now that PlaySound() is working, it helped me solve a run-time error that had been plaguing me as well. The missile class has an explode() method that basically destroys the missile and then creates a new explosion object. Well when I played the explosion sound effect it kept playing over and over and over again so I realized that the missile's explode method was being called over and over again. I don't know how that one got by me. But this was causing the circular explosions to keep shrinking and I couldn't figure out why. But that fixed it.

Not to bore you with all of those details, but just saying that I'm all the more appreciative for it.

Etc.

#7 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 24 October 2005 - 10:35 PM

I'm new to this board, so I can't say whether they would say your question is 'bugging' people or not: but either way, knowing how to diagnose your own errors will save you alot of hand wringing while waiting for someone else to have insight for you.... I should know, as I had to learn the hard way! :)

With Visual C++, you'll find that most of the errors have numbers associated with them (LNK1120, for example), that are generally good search parameters into Google. It's a technique I often use, and it helps you understand more about compilers.

Also, it seems like you have a decent grasp of C++, but this is another favored resource of mine: http://www.parashift.com/c++-faq-lite/. It might come in handy for you. :)

Happy hunting - glad to hear your game is coming along. :)

#8 Nodlehs

    Valued Member

  • Members
  • PipPipPip
  • 152 posts

Posted 25 October 2005 - 01:14 AM

The whole point of the forums is to communicate about game development. If you are having problems, need help, or can offer suggestions, feel free. So don't feel bad about asking questions again when they arise IrishFarmer. Not asking questions of people who can help point you in the right direction is silly, use the resources at hand! To expand on that though, it helps everyone to help you greatly if you have done some homework on the error previously to posting, and share that experience.

Example: I googled the error code I was getting(Error Foo.bar:256), checked in my reference books on C++ and couldn't find any mention of this error I am getting, I think there might be an error in my code here(point it out) but am unsure why its doing that. The code I have quoted does X in relation to the rest of my code.

Doing something like that shows you just aren't showing up and expecting people to do the work for you, which prompts people to be more inclined to help you! And obviously people take into consideration your level of knowledge, people won't flame you for being new to using something new!

#9 IrishFarmer

    Member

  • Members
  • PipPip
  • 43 posts

Posted 25 October 2005 - 01:21 AM

Well, I've been programming with engine scripting languages for a couple of years, so writing in straight C is a bit new to me.

However, I did notice that the help file contains the same information that I would have found in that first google search. Tells me which header files contain the declarations and which libraries I need, etc. That will be a big help.

#10 IrishFarmer

    Member

  • Members
  • PipPip
  • 43 posts

Posted 25 October 2005 - 01:32 AM

As long as we're all chatting and having fun, I have a question related to the forum.

If I ever get this game off the ground and finish something playable, would you guys prefer I just making upload the project and files somewhere so you can compile on your own, or upload the executable and all of the files. Because obviously virii are a problem, but I wanted to see what you guys prefer. Frankly, I wouldn't want to down load a .exe or even take the time to compile someone's source code. But that's just me being lazy. If there's interest then I won't mind putting something up. What would you prefer?

#11 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 25 October 2005 - 02:08 AM

Personally, for all my programs I provide both the binaries and the full source code; that way, people can compile it if they want, or run it if they prefer. Personally I don't worry too much about downloading a demo or game that a fellow developer has posted; I virus-scan everything I download anyway.
reedbeta.com - developer blog, OpenGL demos, and other projects

#12 IrishFarmer

    Member

  • Members
  • PipPip
  • 43 posts

Posted 27 October 2005 - 12:23 AM

Not really on topic, but I just wanted to brag real quick. I've gotten quite a few things working in the last couple of days. One being collision detection between the explosions and the falling bombs, the other being falling bombs, and then the last being the correct setup to quickly implement a short function that will get the core of the game up and running, which is to generate falling bombs based upon the level of the game at random starting points to random targets.

One glaring problem is that the falling bombs adjust themselves a couple of times on the way down. For instance, it'll look like its going to overshoot its target by a lot, then 1/3 of the way down adjust course to look like it'll only slightly overshoot the target, then just before hitting it adjusts even more to hit the target dead on. I'm using that distance formula too so I don't get the problem....at least, I don't get why its so extreme. It wouldn't be so bad, but in a game where you need to be able to predict the course of falling objects, you can't have those objects changing course unpredicatably. Anyway, without further adue, here's a poor representation of the collision detection.

Posted Image

UH OH! What's gonna happen?

Posted Image

Oh that's what'll happen.

Posted Image

This is the result!!!! Yeah. Pretty lame, I know, but I'm excited. Three months ago, if someone told me I'd be programming really lame 'vector' games with C++ I'd have laughed at them...in their face.

Thanks for bearing with me.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users