Author Topic: Undefined Reference...  (Read 8582 times)

GMG

  • Guest
Undefined Reference...
« on: May 02, 2007, 11:57:05 pm »
I'm receiving the error 'undefined reference to 'x'', where 'x' equals some function.

For instance: undefined reference to 'GetDeviceCaps@8'.

I've included the proper header, so I don't know what this means. In fact, it doesn't like any device context-like stuff.

If it helps, I'm trying to convert an all-C program into C++. The code still compiles with no problems if it's done in a .c file.

And what is that '@8' thrown in at the end? That is not the name of the function. Is that some sort of name mangling thing?

Offline raph

  • Almost regular
  • **
  • Posts: 242
Re: Undefined Reference...
« Reply #1 on: May 03, 2007, 12:17:42 am »
Hi,
For instance: undefined reference to 'GetDeviceCaps@8'.
You need to link against all the needed libraries (gdi32 for GetDeviceCaps).
Go to Project->Build Options->Linker settings->Link libraries
Be sure to have some basic link libraries added, here. e.g. libkernel32.a, libuser32.a, libgdi32.a, libcomctl32.a
If you are getting more "undefined references", search google (or msdn directly) for the function name.
You will then find the name of the required library easily (you maybe will have to modify it slightly to fit mingw library names: see MinGW\lib directory).

If it helps, I'm trying to convert an all-C program into C++. The code still compiles with no problems if it's done in a .c file.
If your files are called .c, gcc will indeed compile them as c-files.
Anyway, most of your c-code shouldn't need any changes to be compiled with the c++ compiler.

And what is that '@8' thrown in at the end? That is not the name of the function. Is that some sort of name mangling thing?
That's the ordinal value associated with the GetDeviceCaps function. It is identifying the function in the library.
e.g. if you want to get a pointer to a function in a dll under windows call GetProcAddress with either the function name or the ordinal value as param.

Regards
raph
« Last Edit: May 03, 2007, 12:19:52 am by raph »

GMG

  • Guest
Re: Undefined Reference...
« Reply #2 on: May 03, 2007, 02:05:45 am »
Thanks for the heads up.

Anyway, I linked to those libraries you mentioned, but I'm still getting the same errors. When adding those libs, should I not have chosen 'keep as relative path'?

I've searched the net extensively and have, indeed, found the necessary libraries where these functions are defined, but to no avail. I immediately started out at msdn considering this is a Win32 app.

If I change from the gcc to g++ compiler to compile the .c file I get the same errors as trying to compile the new .cpp file with the g++ compiler.

I'm hopelessly lost.

GMG

  • Guest
Re: Undefined Reference...
« Reply #3 on: May 03, 2007, 03:31:24 am »
Well, I took a step back and just started a new project. I made a silly mistake and ass-u-me-d that I could compile a .cpp file inside a c project. Again, I assumed the compiler would see that the .cpp file in the c project was, in fact, a .cpp and choose the appropriate compiler.

Anyway, including time.h was the only addition I had to make. That and SelectObject barfed up some error about the variable receiving the return value. Maybe that can be remedied with some casting.

Thanks for the help, raph...