Connecting Tech Pros Worldwide Forums | Help | Site Map

C++/C-library linking (pslib)

Thomas Ruschival
Guest
 
Posts: n/a
#1: Jul 22 '05
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(.text+0x11): In function `main':
: undefined reference to `PS_boot()'
/tmp/ccSuItDe.o(.text+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(sheet,"test.ps");
PS_set_info(sheet,"Title","HelloWorld");
PS_begin_page(sheet,841.9,595.3);
PS_end_page(sheet);
PS_shutdown();
};

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

desperately asking
Thomas Ruschival

Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 22 '05

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(.text+0x11): In function `main':
> : undefined reference to `PS_boot()'
> /tmp/ccSuItDe.o(.text+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(sheet,"test.ps");
> PS_set_info(sheet,"Title","HelloWorld");
> PS_begin_page(sheet,841.9,595.3);
> PS_end_page(sheet);
> PS_shutdown();
> };
>
> what do I do wrong. BTW, I have debian [sid] running.[/color]

Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 22 '05

re: C++/C-library linking (pslib)


"Thomas Ruschival" <t.ruschival@vivid-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(.text+0x11): In function `main':
> : undefined reference to `PS_boot()'
> /tmp/ccSuItDe.o(.text+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(sheet,"test.ps");
> PS_set_info(sheet,"Title","HelloWorld");
> PS_begin_page(sheet,841.9,595.3);
> PS_end_page(sheet);
> 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.development.app, since the
compiler command-lines, and paths, and how 'ld' resolves symbols, is all
platform-specific.

V


Thomas Ruschival
Guest
 
Posts: n/a
#4: Jul 22 '05

re: C++/C-library linking (pslib)


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

did the trick!

Closed Thread