Hi Frank,
Thanks a lot, as a non-programmer, I did not take the name mangling of c++ in account. Your explanation is well understandable and clear, as I have written c++ programs with overloading. Now I remember from my C++ book that the compiler distinguishes functions by the types used in its declaration. Also thanks for your compilation and linking options as well as the syntax change. BTW, I am planning to move to a new distro, slackware 7.1. GPC is not included, so I have to build it from source. Do you recommend to install ("patch") it on top of gcc or egcs. egcs is standard for slack, but if I am meddling around already, why not change this also. I am not certain yet if the work on this subject will continue for me, we are just looking for possibilities. Anyway, you will hear more from me about it.
Thanks again, looking forward to hear more from you.
Greetings, Menno
On Mon, 28 Aug 2000, you wrote:
Menno Schaap wrote:
About my programming subject: I am investigating for a small naval architectural bureau, If they can port their DOS/WIN pascal programs to linux. I am a student of naval architecture with small programming experience under Pascal and C++. The company also asked me to have a look at GUI toolkits and write a graphical subsystem for their pascal program. I would like to use QT for this purpose. I made some small tests already, and found out that the compatibility with GPC and Prospero extended pascal is sufficient (small problems). My problem is that I can not use a c++ library in my pascal program. For C everything works allright, but as soon as I compile with g++ I get errors. I also noticed that the assembler output of the compiler is different when compiling C or C++ sources. I tried compiling with gpc -x, etc. Still I run into problems. I use gpc based on egcs 1.1.2 as well as based on gcc 2.95.2 (on two different pc's).
It is possible to combine Pascal, C and C++ code using the GNU compilers (I did such a group project last year), but there's a few things to note.
The problem you're experiencing is the "name mangling" C++ does: Because it allows function overloading, it has to change the "asmname" of each function, so they become distinct. You can prevent this with an `extern "C"' directive (that's how C++ programs use the usual system routines, AFAIK). E.g., the following works for me with your Pascal code:
extern "C" { int number = 0; void number_pp (void); }
void number_pp (void) { number = number + 1; }
If, however, the C++ code is in a library you can't (or don't want to) change this way, you'll have to use wrapper routines (i.e. small routines written in C++, declared as `extern "C"' which call the library and can be called by Pascal code).
Other things to note:
Both C++ and Pascal use specific runtime libraries (-lstdc++ for C++, AFAIK, and -lgpc for GPC). Whether you use g++ or gpc for the final linking compilation, only one of them will be linked automatically. So you'll have to add the other one explicitly on the command line, but that shouldn't be a problem.
E.g., if you use gpc for linking, give `-lstdc++', or put a `{$L stdc++}' directive in the Pascal source. If you use g++ for linking, note that GPC also needs the math library, so give `-lgpc -lm' on the command line.
(In the example you gave, this was not necessary, probably because nothing of libstdc++ was actually used, but for non-trivial programs, it will become necessary.)
It is very much recommended that the gpc and g++ used are based on the same backend (i.e., gcc) version (2.95.x these days), otherwise things become more difficult (e.g., you'll have to specify paths to the libraries, and they might not even cooperate well)...
The objects of C++ and GPC are different internally. AFAIK, there's no direct way to safely use them from the other language. If you need to use them, you might have to write non-OOP wrapper routines.
Finally, please note that in current GPC versions, the syntax:
VAR Number : asmname 'number' Integer;
was changed to:
VAR Number : Integer; asmname 'number'; external;
(Not yet for procedures, but that'll happen soon.)
Frank
-- Frank Heckenbach, frank@g-n-u.de, http://fjf.gnu.de/ GPC To-Do list, latest features, fixed bugs: http://agnes.dida.physik.uni-essen.de/~gnu-pascal/todo.html