Subject: Summary:How to call a C++ program from C?

From: Tao Peng <[email-masked]>
Date: Fri, 8 Oct 1999 10:09:29 -0700 (PDT)

About two weeks ago, I have submited the following question:

Dear All,

I have a big program which is written in C and Fortran, but mainly in C.
The author compile each module by their own compiler ( cc and f77 on SGI)
and link them by f77.

Now I have another C++ program (still others) and want incorporate it
into the previous big program.

My idea is want to call C++ from C, but compile them separately, and link
them altogether.

Can anyone tell me whether this idea is possible, or have better suggestion?

Now I have received the following replies:

1)
Ernest Friedman-Hill <[email-masked]>

C can call C++ routines which are declared as extern "C", but no others.

2)
From: [email-masked]
This is difficult, and certainly not possible in a portable way.

Mixing C and Fortran is easy, but you should use f77 to link (on
some systems this is not strictly necessary; adding the Fortran
libraries when linking is sufficient).

Mixing C and C++ is also easy, but you must use CC to link.

So for mixing C with C++ and Fortran, you'd have to link with both
CC and f77.

The reason for this problem is that both C++ and Fortran program often
need specific initialization code that is added during linking. An
added complication with C++ is name mangling (the compiler must create
unique names for overloaded functions etc.), but there are ways around
that.

In summary, what you are trying to do is possible only on a system
that allows calling Fortran from C without any special initialization
code for Fortran. I don't know whether this is true for SGI systems.
-- 

3)
From: Jochen <[email-masked]>
On Mit, 29 Sep 1999 [email-masked] wrote:
>> I have a big program which is written in C and Fortran, but mainly in C.
>> The author compile each module by their own compiler ( cc and f77 on 
SGI) 
>> and link them by f77.
>> 
>> Now I have another C++ program (still others) and want incorporate it 
>> into the previous big program.

>The reason for this problem is that both C++ and Fortran program often
>need specific initialization code that is added during linking. An
>added complication with C++ is name mangling (the compiler must create
>unique names for overloaded functions etc.), but there are ways around
>that.

Hi Konrad,

you are right - as usual :-)

Many times you can resolve the explained problems by incremental linkage
(like ld -r), though.
So you link all your FORTRAN code into one big object file using the
FORTRAN compiler as linker driver, analogous the C++ code using the C++
linker driver. Then you link these into the final exectuable.
The only remaining problem I ever encountered is some trouble about a
duplicate or no main function.

-- Jochen

Thanks for all the help!!!

Have a nice day!

                                Yours Sincerely
                                Tao Peng

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\*   	                                               	\*
\*      Tao Peng                                        \*
\*                                                      \* 
\*      Molecular Design Laboratory                     \*
\*      Institute of Physical Chemistry	                \*
\*      Peking University                               \*
\*      Beijing, 100871	                                \*
\*      P.R.China                                       \*
\*                                                      \*
\*      E-mail:[email-masked]                 \*
\*      Lab Phone: 86-10-62756833                       \*
\*      Fax Number:86-10-62751725                       \*
\*      http://www.ipc.pku.edu.cn/~taop/                \*
\*      Address:Peking University Building 46 Room 2122 \*
\*      Dormitory Phone: 86-10-62761206                 \*
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*