![]() |
|
#1
|
|||
|
|||
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
|
|||
|
|||
Re: Undefined reference to 'GetDefaultPrinterA'Quote:
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:
Compiling as a C program: Quote:
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:
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:
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:
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
|
|||
|
|||
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
|
|||
|
|||
Re: Undefined reference to 'GetDefaultPrinterA'Quote:
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 GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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