undefined reference to `OpenPrinterA@12'

J

Jimmy Rasmussen

When trying to compile this program

-----------------------------------------
#include <windows.h>
#include <winspool.h>

main() {

LPHANDLE printer;

OpenPrinter("sdsd",printer,NULL);
}
-----------------------------------------

with gcc I get the following error message: undefined reference to
`OpenPrinterA@12'

What am I doing wrong? OpenPrinter is defined in winspool.h so why is it not
found?

regards
Jimmy
 
J

Jens.Toerring

Jimmy Rasmussen said:
When trying to compile this program
LPHANDLE printer;

with gcc I get the following error message: undefined reference to
`OpenPrinterA@12'
What am I doing wrong? OpenPrinter is defined in winspool.h so why is it not
found?

What you're getting here isn't a compiler but a linker error. And that
means that you need to link the library that defines the OpenPrinter()
function (whatever it does) to your program - the header file only
tells the compiler how the function is to be used but does not contain
any code for it.
Regards, Jens
 
B

Bruno Desthuilliers

Jimmy said:
When trying to compile this program
Not standard
#include <winspool.h>
Not standard

must be
int main(int argc, char **argv)
or
int main(void)
LPHANDLE printer;

Not standard
OpenPrinter("sdsd",printer,NULL);
Not standard

main() must return an integer
What am I doing wrong? OpenPrinter is defined

no, it is not defined, it is *declared*.
in winspool.h so why is it not
found?

How do you expect your linker to resolve a symbol if you do not link
with the approriate object code (ie : object file or library) ? Read
your linker manual to know how to tell it to link to a specific lib, and
read your OS/API/Lib/Whatsoever doc to know which lib to link to.

Bruno
 
J

Jimmy Rasmussen

Since winspool.h is found in the same directory as stdio.h etc., I assumed
that the code was part of the standard library of my compiler. If this is
the case, is gcc not supposed to find the corresponding object files itself
? The functions mentioned in the windows.h file (that you said was not
standard) can be used without mentioning any libraries to the linker. I've
tried to explicitly tell gcc about the library (I found a file called
libwinspool.a) like this

gcc Printcharger.c -L "d:\Dev-C++\lib\" -l winspool

but with the same result. Maybe gcc (or my head) has not been properly
installed
 
M

Mark McIntyre

When trying to compile this program

snip windows code
with gcc I get the following error message: undefined reference to
`OpenPrinterA@12'

What am I doing wrong?

You're asking in the wrong group for starters. This is not a windows
programming group.
OpenPrinter is defined in winspool.h so why is it not
found?

the header doesn't define the function, only declare it. The actual
definition is probably in some library or other which you forgot to
link with.
By the way, did you notice that your function name is not the same as
that which gcc complains about?
 
A

A. Sinan Unur

Since winspool.h is found in the same directory as stdio.h etc., I
assumed that the code was part of the standard library of my compiler.

Standard libraries are not compiler specific (you could say that's why they
are called standard).
If this is the case, is gcc not supposed to find the corresponding
object files itself ?

Every compiler has a default set of libraries that it links against. If you
want to link against other libraries, you need to specify them.
The functions mentioned in the windows.h file (that you said was not
standard) can be used without mentioning any libraries to the linker.

windows.h is Windows specific. Its contents are not defined by the ANSI/ISO
standard C language. Hence, it is off-topic in this newsgroup.

It looks like OpenPrinter is not in the default set of libraries that are
linked against.
I've tried to explicitly tell gcc about the
library (I found a file called libwinspool.a) like this

gcc Printcharger.c -L "d:\Dev-C++\lib\" -l winspool

In some shells, you'd never be able to run that command on the command
line. In addition, lose the space between -l and winspool.
but with the same result. Maybe gcc (or my head) has not been properly
installed.

It's the latter. You are also in the wrong newsgroup. Next time, you should
at least read your compiler's documentation.

$ cat PrintCharger.c
#include <windows.h>
#include <winspool.h>

int main(void) {
LPHANDLE printer;
OpenPrinter("sdsd",printer,NULL);
return 0;
}

$ gcc -Wall PrintCharger.c -o PrintCharger.exe -lwinspool
 
S

Simon Biber

Jimmy Rasmussen said:
Since winspool.h is found in the same directory as stdio.h etc., I
assumed that the code was part of the standard library of my compiler.
If this is the case, is gcc not supposed to find the corresponding
object files itself?

No, this code is not part of the standard library, and on most implementations
it must be specifically linked in. In fact, on many implementations the <math.h>
section of the standard library is not even linked by default, you must link the
libm.a file by using an option like -lm!
The functions mentioned in the windows.h file (that you said was not
standard) can be used without mentioning any libraries to the linker.
I've tried to explicitly tell gcc about the library (I found a file
called libwinspool.a) like this

gcc Printcharger.c -L "d:\Dev-C++\lib\" -l winspool

but with the same result. Maybe gcc (or my head) has not been
properly installed

Perhaps so. Your file compiles fine when I use -l winspool:

C:\docs\prog\c>gcc jrasmussen.c
/cygdrive/c/DOCUME~1/simon/LOCALS~1/Temp/cc0ReABU.o(.text+0x3a):jrasmussen.c: undefined reference to
`_OpenPrinterA@12'
collect2: ld returned 1 exit status

C:\docs\prog\c>gcc jrasmussen.c -l winspool

C:\docs\prog\c>ls -l /usr/lib/w32api/libwinspool.a
-rwxrwxrwx 1 simon Users 97896 Sep 12 23:37 /usr/lib/w32api/libwinspool.a

Perhaps you also have a w32api subdirectory that you must specifically
indicate in your -L option?
 
J

Jimmy Rasmussen

Thank you very much for all your help !
I did not realize that windows programming was off-topic in this newgroup,
the title didn't suggest so, but maybe I should have looked into it... sorry
By the way, all my problems were solved by throwing away the space between
the -l and the winspool at the command prompt as suggested by Unur.

regards
Jimmy
 
B

Bruno Desthuilliers

Jimmy Rasmussen wrote:

(please dont top-post - corrected)
Since winspool.h is found in the same directory as stdio.h etc., I assumed
that the code was part of the standard library of my compiler.

When we say 'standard', we mean *ISO* standard, the one that defines the
C language and its standard lib.
If this is
the case, is gcc not supposed to find the corresponding object files itself
?

Except for the standard lib, your linker is not supposed to link to
anything unless you tell him so.
The functions mentioned in the windows.h file (that you said was not
standard) can be used without mentioning any libraries to the linker.

The fact that *your* compiler behaves that way has nothing to do with
the C language.
I've
tried to explicitly tell gcc about the library (I found a file called
libwinspool.a) like this

gcc Printcharger.c -L "d:\Dev-C++\lib\" -l winspool

but with the same result. Maybe gcc (or my head) has not been properly
installed

Maybe you'd need to RTFM ? Anyway, compiler-specific problems are OT here.

Bruno
 
M

Mark McIntyre

Since winspool.h is found in the same directory as stdio.h etc., I assumed
that the code was part of the standard library of my compiler.

Not correct - the Standard Library is defined by ISO, but compiler
wrtiters are quite free to store otehr stuff in the same directory.
If this is the case,

its not
is gcc not supposed to find the corresponding object files itself

but even if it were there's nothing that requires GCC or any other
compier to search for the objects. Not even MSVC does this- for
instance you have to tell it to link against the sockets lib.
? The functions mentioned in the windows.h file (that you said was not
standard) can be used without mentioning any libraries to the linker.

Windows.h also isn't standard, and if it can be used w/o being
specified, thats just how the compiler/linker was written.
gcc Printcharger.c -L "d:\Dev-C++\lib\" -l winspool

Thats probably the wrong command, or the right command in the wrong
order .Try asking in a gcc group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top