undefined reference to `OpenPrinterA@12'

Discussion in 'C Programming' started by Jimmy Rasmussen, Dec 13, 2003.

  1. 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
    Jimmy Rasmussen, Dec 13, 2003
    #1
    1. Advertising

  2. Jimmy Rasmussen

    -berlin.de Guest

    Jimmy Rasmussen <> wrote:
    > 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?


    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
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.physik.fu-berlin.de/~toerring
    -berlin.de, Dec 13, 2003
    #2
    1. Advertising

  3. Jimmy Rasmussen wrote:
    > When trying to compile this program
    >
    > -----------------------------------------
    > #include <windows.h>

    Not standard

    > #include <winspool.h>

    Not standard

    > main() {


    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

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


    > 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
    Bruno Desthuilliers, Dec 13, 2003
    #3
  4. 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

    "Bruno Desthuilliers" <> wrote in message
    news:3fdb32de$0$7126$...
    > Jimmy Rasmussen wrote:
    > > When trying to compile this program
    > >
    > > -----------------------------------------
    > > #include <windows.h>

    > Not standard
    >
    > > #include <winspool.h>

    > Not standard
    >
    > > main() {

    >
    > 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
    >
    > > -----------------------------------------
    > >
    > > with gcc I get the following error message: undefined reference to
    > > `OpenPrinterA@12'

    >
    > > 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
    >
    Jimmy Rasmussen, Dec 13, 2003
    #4
  5. On Sat, 13 Dec 2003 16:15:50 +0100, in comp.lang.c , "Jimmy Rasmussen"
    <> wrote:

    >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?
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Dec 13, 2003
    #5
  6. "Jimmy Rasmussen" <> wrote in
    news:3fdb498b$0$27317$:

    > 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

    --
    A. Sinan Unur

    Remove dashes for address
    Spam bait: mailto:
    A. Sinan Unur, Dec 13, 2003
    #6
  7. Jimmy Rasmussen

    Simon Biber Guest

    "Jimmy Rasmussen" <> wrote:
    > 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?

    --
    Simon.
    Simon Biber, Dec 14, 2003
    #7
  8. 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
    Jimmy Rasmussen, Dec 14, 2003
    #8
  9. Jimmy Rasmussen wrote:

    (please dont top-post - corrected)

    > "Bruno Desthuilliers" <> wrote in message
    > news:3fdb32de$0$7126$...
    >
    >>Jimmy Rasmussen wrote:
    >>
    >>>When trying to compile this program
    >>>
    >>>-----------------------------------------
    >>>#include <windows.h>

    >>
    >>Not standard
    >>
    >>
    >>>#include <winspool.h>

    >>
    >>Not standard
    >>
    >>
    >>>main() {

    >>
    >>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
    >>
    >>
    >>>-----------------------------------------
    >>>
    >>>with gcc I get the following error message: undefined reference to
    >>>`OpenPrinterA@12'

    >>
    >>>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.
    >>


    > 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
    Bruno Desthuilliers, Dec 14, 2003
    #9
  10. On Sat, 13 Dec 2003 18:16:55 +0100, in comp.lang.c , "Jimmy Rasmussen"
    <> wrote:

    >
    >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.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Dec 15, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Felipe
    Replies:
    3
    Views:
    445
    Felipe
    Jul 16, 2004
  2. Lu
    Replies:
    1
    Views:
    570
    Roel Schroeven
    Jul 10, 2003
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,753
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    591
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    370
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page