GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Oct-2005, 05:58
joergf joergf is offline
New Member
 
Join Date: Oct 2005
Posts: 2
joergf is on a distinguished road
Question

Undefined reference to 'GetDefaultPrinterA'


Hi,

I'm trying to get the name of the default printer with GetDefaultPrinter. I'm using DevCpp ,mingw and WinXP.
The Problem is I'm always getting an "undefined reference" error from the linker, although I have included the libwinspool for linking(I'm succesfully using other functions from libwinspool).
Any idea?

Thanks.
  #2  
Old 06-Oct-2005, 10:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Undefined reference to 'GetDefaultPrinterA'


Quote:
Originally Posted by joergf
Hi,

I'm trying to get the name of the default printer with GetDefaultPrinter. I'm using DevCpp ,mingw and WinXP.
The Problem is I'm always getting an "undefined reference" error from the linker, although I have included the libwinspool for linking(I'm succesfully using other functions from libwinspool).
Any idea?

Thanks.

First of all: is this C or C++? (You should always tell us; sometimes it makes a difference.) This particular error is usually due to a missing prototype.

If it is C, you may get a linker error rather than a compiler error for functions with missing prototypes (since functions that don't have prototypes will be assumed to be int functions and all arguments will be assumed to be ints --- that's for legacy reasons). That is, the compiler may not be unhappy, but the linker may not be able to find what it needs.

If it is C++ you will get a compiler error if the function doesn't have a prototype, since C++ doesn't have default types for functions and their arguments.

I'll illustrate. I took the following code and compiled it with C and then with C++:

CPP / C++ / C Code:
#include <windows.h>
#include <winspool.h>
int main()
{
  LPSTR lpStrx;
  LPDWORD lpDwrdx;

  GetDefaultPrinterA(lpStrx, lpDwrdx);

  return 0;
}

Compiling as a C program:

Quote:
gcc z.c -lwinspool

cc45W6BR.o(.text+0x2c):z.c: undefined reference to `_GetDefaultPrinterA'
collect2: ld returned 1 exit status

Note that this is a linker error, not a compiler error, even though the real problem is a missing prototype (I'll show how to fix it in a minute.)

If I save the file as z.cpp and compile with g++
Quote:
g++ z.cpp -lwinspool
z.cpp: In function `int main()':
z.cpp:8: error: `GetDefaultPrinterA' undeclared (first use this function)
z.cpp:8: error: (Each undeclared identifier is reported only once for each function it appears in.)

Bur we know that GetDefaultPrinterA is defined in <winspool.h>, right? Well look at it (yours might be slightly different, but the all come from Microsoft):

CPP / C++ / C Code:
#if _WIN32_WINNT >= 0x0500
BOOL WINAPI GetDefaultPrinterA(LPSTR,LPDWORD);
BOOL WINAPI GetDefaultPrinterW(LPWSTR,LPDWORD);
#endif

You see, this function won't work Windows 95, Windows 98, or other earlier versions, so they want to make sure that you have something later. Now, with Visual Studio C++, this and other important things are #defined in "stdafx.h" For users of gcc (mingw, cygwin, or other Windows ports), you have to #define it yourself.

You can look on msdn.com to see what value to give _WIN32_WINNT for each operating system. (0x0501 for Windows XP, as I recall --- but you should look it up).

So you can define _WIN32_WINNT at the beginning or your program or you can put into compiler options or project options for your dev-cpp. Here's how I got my file to compile. (Of course it doesn't do anything, but serves as an example of getting things going.)

CPP / C++ / C Code:
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <winspool.h>
int main()
{
  LPSTR lpStrx;
  LPDWORD lpDwrdx;

  GetDefaultPrinterA(lpStrx, lpDwrdx);

  return 0;
}

If you can't get my sample program to compile then let us know exactly what happened (command line, compiler messages etc.) If it works on my example, and if putting the #define statement in your program doesn't help, then you have to give us a little more information about what your code is trying to do.

Regards,

Dave

[edit] You don't have to #include <winspool.h>, I think, since it gets in there from <windows.h>. I just put it in (for my "doesn't work" example) so that there would be no question that it is included.
[/edit]
  #3  
Old 06-Oct-2005, 14:44
joergf joergf is offline
New Member
 
Join Date: Oct 2005
Posts: 2
joergf is on a distinguished road

Re: Undefined reference to 'GetDefaultPrinterA'


Although I was under the impression I started a C++ Project it was actually compiling as C. But only because I got a linker error I thougth it couldn't be a problem with the header but with the library.
With the #define (and compiled as C++) it's working now.
Thanks for the help and the really detailed answer!

Joerg
  #4  
Old 06-Oct-2005, 15:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Undefined reference to 'GetDefaultPrinterA'


Quote:
Originally Posted by joergf
Although I was under the impression I started a C++ Project it was actually compiling as C. But only because I got a linker error I thougth it couldn't be a problem with the header but with the library.
With the #define (and compiled as C++) it's working now.
Thanks for the help and the really detailed answer!

Joerg

If your file were being compiled as C++ you would have seen a compiler error, not a linker error.

Usually if your file is named "something.c" it will be compiled as a C file, and if it's named "something.cpp" it will be compiled as a C++ file (invoked with g++). I seem to recall that there are project defaults that allow you to select whether a given project is a C project or a C++ project (which determines the default types of certain files created by dev-cpp project wizard).


Regards,

Dave
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to interpret characters as they are being entered? nkhambal C Programming Language 18 14-Feb-2006 11:41
Linker Errors with Dev-C++ 5 Gamer_2k4 C++ Forum 1 18-Apr-2005 12:08
Function and Array (w/ reference variables) question brookeville C++ Forum 15 07-Dec-2004 02:11
Undefined Reference Dream86 C++ Forum 16 14-Nov-2004 17:29
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.