Wednesday, March 28, 2007

Rough Waters With NetBeans/C++ IDE

Not a good rapport when your new favorite IDE begins to misbehave! I'm enchanted about NetBeans 5.5 and its C/C++ Development Pack that I'm willing to stick it out until I'm sure there's no solution for this problem. And the problem is this error you get when you try to build your Windows programs:

undefined reference to `_GetStockObject@4'
undefined reference to `_PlaySoundA@12'

Actually, the errors are not the IDE or development pack's problems, per se. The combo depends on Cygwin utilities - gcc, g++, make, and gdb - to compile, debug, and build programs. The code compiles just fine; it's the linker that doesn't seem to find header or implementation files required for the methods. For example, the GetStockObject() function is defined in wingdi.h, which is in the same location as other header files the program requires - but for some reason it's not seen. I don't know where the linker is looking for the files/libraries it needs.
Enter the world of DLLs - isn't there a GDI32.dll already on the system that has the same functions defined? I'd think linking would find this library and use it. Apparently it doesn't.
On a flip side, I took the exact code and threw it in Dev-C++, and it did a little better: it didn't produce the first error but it too couldn't find the PlaySoundA() function. Commenting that line out allows the program to run. Is it's library and includes more put together?
But this highlights a problem programmers have to deal with everyday - if you want to program for Windows, you better use Microsoft's tools. Sure enough, when I slap the code into MS Visual C++ 6.0, it builds without complaint. Sinister ... but I won't blame them for the issue at large.
I'm sure there's a solution out there. Google isn't helping much - lots of such problems reported but no solution at all. There's still hope though - I have another [unpolluted] installation of this development environment that I'll try. Hopefully this sheds a little light on this common issue.
I'm really hoping the NetBeans team would just 'nativize' all tools required to run the C/C++ development pack in the NetBeans IDE. At this point, I don't know exactly who to blame. Perhaps there's forums or FAQs I just haven't come across, or documentation I should have read ...


Anonymous said...

I got the same error messages trying to link to a shared library on Linux. I fixed it by adding a link option in at LDLIBSOPTIONS. In my case I was trying to link the GNU scientific library so the entry was: "LDLIBSOPTIONS= -lgsl"

Not sure if this helps or not, but perhaps there is a similar technique in cygwin. Best of luck.


Anonymous said...

The error is generated bacause the linker doesn't find the function call references in any default library. How the GetStockObject and PlaySound functions are not in any of the default libraries, you must to specify their corresponding libraries to the linker.

You can achieve this by adding the -Wl option to the compilation tool. Supose that I want to compile a file that contains the GetStockObject and the PlaySound functions which call references are in the gdi32 and winmm libraries, respectively. The compilation statement for a mingw compiler is:

gcc sourcefile -Wl, "C:\...\lib\libgdi32.a" "C\...\lib\libwinmm.a"

In the case you are not using mingw, just verify the correct library names in the lib directory of your compiler tools.

If you need to add more libraries just separate them by spaces. When too many libraries are needed a good choice is to create a makefile.

You can also make this compilation with Dev C++ adding the libraries to the Project Options in the Parameters tab.