C++/C-library linking (pslib)

T

Thomas Ruschival

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
 
R

Rolf Magnus

Thomas said:
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

Don't use gcc for linking anything that contains C++ code. Use g++. There is
more difference than -lstdc++.
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>

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"
{
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.
 
V

Victor Bazarov

Thomas Ruschival said:
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++.

The two languages are different enough to make it generally possible.
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.

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
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top