C++/C-library linking (pslib)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Ruschival

    #1

    C++/C-library linking (pslib)

    Hi,
    as far as I know I can link all C libraries in C++ as well. but I can't get it done with pslib. pslib is a library to create Postscript documents.
    The exactly same code compiles and links with C and it doesn't when I
    use C++.
    This is my linking command:
    gcc -o test -L/usr/lib/ -lps -lstdc++ test.cpp

    and in /usr/lib is definitely the file
    /usr/lib/libps.so -> libps.so.0.2.4

    it all works fine when I compile my file as test.c as C
    code. but with test.cpp I get these errors:

    /tmp/ccSuItDe.o(.tex t+0x11): In function `main':
    : undefined reference to `PS_boot()'
    /tmp/ccSuItDe.o(.tex t+0x16): In function `main':
    : undefined reference to `PS_new()'
    [bla bla bla and so on............. .........]
    collect2: ld returned 1 exit status

    this is the stupid little piece of code I wrote after the
    bigger project didn't compile:

    #include <libps/pslib.h>
    main(int argc, char *argv[]) {
    PSDoc* sheet;
    PS_boot();
    sheet = PS_new();
    PS_open_file(sh eet,"test.ps");
    PS_set_info(she et,"Title","Hel loWorld");
    PS_begin_page(s heet,841.9,595. 3);
    PS_end_page(she et);
    PS_shutdown();
    };

    what do I do wrong. BTW, I have debian [sid] running.

    desperately asking
    Thomas Ruschival
  • Rolf Magnus

    #2
    Re: C++/C-library linking (pslib)

    Thomas Ruschival wrote:
    [color=blue]
    > Hi,
    > as far as I know I can link all C libraries in C++ as well. but I can't
    > get it done with pslib. pslib is a library to create Postscript
    > documents.
    > The exactly same code compiles and links with C and it doesn't when I
    > use C++.
    > This is my linking command:
    > gcc -o test -L/usr/lib/ -lps -lstdc++ test.cpp[/color]

    Don't use gcc for linking anything that contains C++ code. Use g++. There is
    more difference than -lstdc++.
    [color=blue]
    > and in /usr/lib is definitely the file
    > /usr/lib/libps.so -> libps.so.0.2.4
    >
    > it all works fine when I compile my file as test.c as C
    > code. but with test.cpp I get these errors:
    >
    > /tmp/ccSuItDe.o(.tex t+0x11): In function `main':
    > : undefined reference to `PS_boot()'
    > /tmp/ccSuItDe.o(.tex t+0x16): In function `main':
    > : undefined reference to `PS_new()'
    > [bla bla bla and so on............. .........]
    > collect2: ld returned 1 exit status
    >
    > this is the stupid little piece of code I wrote after the
    > bigger project didn't compile:
    >
    > #include <libps/pslib.h>[/color]

    Maybe the pslib header is is not written in a C++-aware way. C++ uses name
    mangling, which means that e.g. parameter types are added to the function
    name to form an internal symbol for your function. C usually (and in the
    case of gcc definitely) doesn't do that, so the functions cannot be found
    by the linker if C linkage isn't explicitly requested. Most C headers have
    someting like:

    #ifdef __cplusplus
    extern "C"
    {
    #endif

    // header code

    #ifdef __cplusplus
    }
    #endif

    so that if it's compiled with a C++ compiler, C linkage is requested for the
    functions declared in that header. If your header doesn't do that, you
    could try surrounding the extern "C" around your #include, like:

    extern "C"
    {
    #include <libs/pslib.h>
    }
    [color=blue]
    > main(int argc, char *argv[]) {
    > PSDoc* sheet;
    > PS_boot();
    > sheet = PS_new();
    > PS_open_file(sh eet,"test.ps");
    > PS_set_info(she et,"Title","Hel loWorld");
    > PS_begin_page(s heet,841.9,595. 3);
    > PS_end_page(she et);
    > PS_shutdown();
    > };
    >
    > what do I do wrong. BTW, I have debian [sid] running.[/color]

    Comment

    • Victor Bazarov

      #3
      Re: C++/C-library linking (pslib)

      "Thomas Ruschival" <t.ruschival@vi vid-md.de> wrote...[color=blue]
      > as far as I know I can link all C libraries in C++ as well. but I can't
      > get it done with pslib. pslib is a library to create Postscript documents.
      > The exactly same code compiles and links with C and it doesn't when I
      > use C++.[/color]

      The two languages are different enough to make it generally possible.
      [color=blue]
      > This is my linking command:
      > gcc -o test -L/usr/lib/ -lps -lstdc++ test.cpp
      >
      > and in /usr/lib is definitely the file
      > /usr/lib/libps.so -> libps.so.0.2.4
      >
      > it all works fine when I compile my file as test.c as C
      > code. but with test.cpp I get these errors:
      >
      > /tmp/ccSuItDe.o(.tex t+0x11): In function `main':
      > : undefined reference to `PS_boot()'
      > /tmp/ccSuItDe.o(.tex t+0x16): In function `main':
      > : undefined reference to `PS_new()'
      > [bla bla bla and so on............. .........]
      > collect2: ld returned 1 exit status
      >
      > this is the stupid little piece of code I wrote after the
      > bigger project didn't compile:
      >
      > #include <libps/pslib.h>
      > main(int argc, char *argv[]) {
      > PSDoc* sheet;
      > PS_boot();
      > sheet = PS_new();
      > PS_open_file(sh eet,"test.ps");
      > PS_set_info(she et,"Title","Hel loWorld");
      > PS_begin_page(s heet,841.9,595. 3);
      > PS_end_page(she et);
      > PS_shutdown();
      > };
      >
      > what do I do wrong. BTW, I have debian [sid] running.[/color]

      First of all, the code is not valid C++ code. The 'main' has implicit
      return type, which is not allowed in C++.

      Second, since the contents of <libps/pslib.h> are not known, the use of
      it within a C++ program is not necessarily guaranteed.

      Third, you have a superfluous semicolon after the closing curly brace.

      I strongly recommend asking in comp.os.linux.d evelopment.app, since the
      compiler command-lines, and paths, and how 'ld' resolves symbols, is all
      platform-specific.

      V


      Comment

      • Thomas Ruschival

        #4
        Solved: C++/C-library linking (pslib)

        Thanks alot,
        extern "C"{
        #include <>
        }

        did the trick!

        Comment

        Working...