LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 997|回复: 3

how to let c call C++ @ RH9.0 Linux

[复制链接]
发表于 2004-2-13 15:43:58 | 显示全部楼层 |阅读模式
1. i use RH9.0 Linux. gcc version is 3.2.2.

2. the source files is :

a.cxx is:

void my_fun( void)
{

}

a.h is :

void my_fun( void);

b.c is :

#include "a.h"

int main( void)
{
my_fun();
}

3. then i will compile .

g++ -c -g a.cxx -o a.o
gcc -c -g b.c -o b.o
ld a.o b.o -o a.out

but the ld complain that

ld: warning: cannot find entry symbol _start; defaulting to 08048184
b.o(.text+0x11): In function `main':
/tmp/b.c:6: undefined reference to `my_fun'

4. but i have define the function in a.cxx
why ld complain that?
please help me
发表于 2004-2-13 20:25:47 | 显示全部楼层
可以将a.cxx改写成这样:

  1. extern "C" {
  2. void my_fun( void)
  3. {

  4. }
  5. }
复制代码

但是只能使用简单的功能,你自己试试看能不能满足你的需要。
发表于 2004-2-13 23:47:50 | 显示全部楼层
C++ and Fortran using Name mangling when compiling.
Fortran( I have no idea );

For C++, when we enjoy the convinience of overload, the ld would not
know which function should be called, so the compiler will mangle the name,
therefore, overloaded function has one UNIQUE name each.

One of problems about the issue is there is no standard for name mangling,
which means exported functions are not portable in different compiler. -- that
is one of the reasons M$ introduced COM.

So, if you wanted export something, using C convention.
 楼主| 发表于 2004-2-14 10:36:50 | 显示全部楼层
thanks.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表