Topic: help! link C and C++


Author: ywzhang@cs.mcgill.ca (Yingwei ZHANG)
Date: 14 Jun 1994 15:23:30 GMT
Raw View
Hi, I met a problem in programming. I am doing a large project.
Most of the project is written in C, while I want to do my work
in C++. We use gcc (gcc version 2.5.8) as our compiler and linker.
The problem is: gcc has different conventions for names of C++ functions
and C functions. For example, if we have foo() is in f.c file, it will
have the name _foo in f.o after after compiling; while if we have foo() in
f.cc, it will have the name _foo__Fe in f.o file. So in other parts which
are written in C, it can not call my functions written in C++!

Is there any good idea to solve this problem?  For example, maybe an compiling
option can make c++ compiler use C compiling convention.

Thank you for your reply!

Yingwei




Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 14 Jun 1994 15:50:33 -0500
Raw View
ywzhang@cs.mcgill.ca (Yingwei ZHANG) writes:


>Hi, I met a problem in programming. I am doing a large project.
>Most of the project is written in C, while I want to do my work
>in C++. We use gcc (gcc version 2.5.8) as our compiler and linker.
>The problem is: gcc has different conventions for names of C++ functions
>and C functions. For example, if we have foo() is in f.c file, it will
>have the name _foo in f.o after after compiling; while if we have foo() in
>f.cc, it will have the name _foo__Fe in f.o file. So in other parts which
>are written in C, it can not call my functions written in C++!

>Is there any good idea to solve this problem?  For example, maybe an compiling
>option can make c++ compiler use C compiling convention.

1) "main" should be in c++. I think GCC does not require this, but
most other C++ compilers do have this requirement.

2) All functions, that might be used for interchange between
C and C++ should have their parameters declared in C++
(ANSI C) style.

3) All header files declaring functions to be used from both
languages should be in the following form:

....  /* staring comments */

#ifdef __cplusplus
extern "C" {
#endif

...   /* common declarations go here */

#ifdef __cplusplus
}
#endif








The compiler symbol __cplusplus (please excuse me if I have the
name a bit off) is always defined (by the compiler) when compiling
C++ code. The effect of the condition blocks is to cause all
common items in C++ code to be declared as 'extern "C"'. This
is a flag to the C++ compiler that these items use C language
conventions.




Author: jhpb@sarto.gaithersburg.md.us (Joseph H. Buehler)
Date: Wed, 15 Jun 1994 11:52:12 GMT
Raw View
>>>>> "YZ" == Yingwei ZHANG <ywzhang@cs.mcgill.ca> writes:

    YZ> Hi, I met a problem in programming. I am doing a large
    YZ> project.  Most of the project is written in C, while I want to
    YZ> do my work in C++. We use gcc (gcc version 2.5.8) as our
    YZ> compiler and linker.  The problem is: gcc has different
    YZ> conventions for names of C++ functions and C functions. For
    YZ> example, if we have foo() is in f.c file, it will have the
    YZ> name _foo in f.o after after compiling; while if we have foo()
    YZ> in f.cc, it will have the name _foo__Fe in f.o file. So in
    YZ> other parts which are written in C, it can not call my
    YZ> functions written in C++!

I found the following to work under gcc for what I wanted to do.

extern "C" return_type
cplusplus_function(type1 arg1, type2 arg2, type3 etc)
{
 // your C++ code here
}