[fpc-pascal]problem linking with c
Michalis Kamburelis
michalis at camelot.homedns.org
Wed Dec 24 00:41:41 CET 2003
Dean Zobec wrote:
> Hi,
> i tried to link a c function but had some problems:
> a simple example:
> in file cwrite.c
>
> #include <stdio.h>
>
> void writestringinc(void);
>
> void writestringinc(void) {
> puts("This string is written using c");
> fflush( stdout );
> }
>
> compiled with c using
> g++ -c cwrite.c
>
> now, this is a test program in fpc:
>
> Program testc;
> {$linklib c}
>
> {$L cwrite.o}
>
> Procedure writestringinc; cdecl; external;
>
> begin
> writeln ('Written with Pascal');
> writestringinc();
> end.
>
> both cwrite.o and testc.pp reside in the same directory.
> compiling with fpc gives me this:
> [dean at delfino c++progs]$ fpc testc.pp
> Free Pascal Compiler version 1.9.1 [2003/11/14] for i386
> Copyright (c) 1993-2002 by Florian Klaempfl
> Target OS: Linux for i386
> Compiling testc.pp
> Linking testc
> testc.o(.text+0x4a): In function `main':
> : undefined reference to `writestringinc'
> testc.pp(11,1) Error: Error while linking
> Closing script ppas.sh
>
> What am I doing wrong?
When you compile sources with g++ function names are "mangled". This is
done by all compilers for languages with things like objects, function
overloading, separate namespaces for each unit and so on (in general,
anything more complicated than simple C approach "one global namespace
for all functions"). G++ does this, FPC does this, and so on.
You can view generated object file with nm or objdump commands - e.g. call
nm cwrite.o
and you will see that cwrite.o does not contain 'writestringinc'
function - instead it constains something like '_Z14writestringincv' and
this a "mangled" name. (On your system it may be something similiar but
not exactly the same as '_Z14writestringincv' - details may depend on
the g++ version you use)
So what you have to do ? If only possible, you should compile with gcc,
not g++. In this case "cwrite.c" is simple C, no C++, so this solution
is good. But of course you may want sometimes to write something in C++
and have to compile it with g++. Then the only way (known to me) is to
link to the "mangled" name, i.e. write something like
Procedure writestringinc; cdecl; external name '_Z14writestringincv';
in your Pascal source file.
>
> Another question: is fpc compatible with C++?
>
Probably FPC developers will add/correct something here. As far as I
know FPC just generates object files (the ".o" files) in the same format
as gcc, g++ and many other compilers. In this sense FPC is "compatible"
with all those compilers.
However, as I already said, most compilers for languages other that C
have to do some name mangling and so the problems (like the one you
described here) arise. There are no standards telling how compilers
mangle names - each compiler for each language does it his own way. So
FPC cannot know how g++ with mangle the name 'writestringinc' and cannot
predict that it will be called '_Z14writestringincv' in object file.
> Thanks in advance,
>
> Ciao, Dean
>
>
>
>
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
--
Michalis Kamburelis
<michalis at camelot.homedns.org>
http://www.camelot.homedns.org/~michalis/
More information about the fpc-pascal
mailing list