用C++调用C的库函数(转)
用C++调用C的库函数
C++调用C的库函数时,如果头文件定义得不恰当,可能会出现明明某函数在obj文件中存在,但是却发生链接失败的情况,出现如下错误:
undefined reference to 'xxx'
出现问题的原因是c库函数编译成obj文件时对函数符号的处理和C++不同。因为C++函数支持重载,所以函数符号的处理要更复杂一些,c往往不作修饰。
例如有函数:
/* dofunc.c */
#include <stdio.h>
int dofunc()
{
printf("dofunc\n");
}
使用gcc编译成obj后
gcc -c dofunc.c
#生成 dofunc.o
objdump -x dofunc.o
[ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x00000000 dofunc.c
File
[ 2](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 1) 0x00000000 _dofunc
AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0
[ 4](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .text
AUX scnlen 0x14 nreloc 2 nlnno 0
[ 6](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .data
AUX scnlen 0x0 nreloc 0 nlnno 0
[ 8](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .bss
AUX scnlen 0x0 nreloc 0 nlnno 0
[ 10](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .rdata
AUX scnlen 0x8 nreloc 0 nlnno 0
[ 12](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _printf
c的dofunc函数在obj文件里的符号为 _dofunc
再看看使用g++编译后的代码:
g++ -c dofunc.c
objdump -x dofunc.o
SYMBOL TABLE:
[ 0](sec -2)(fl 0x00)(ty &
相关文档:
http://www.trendcaller.com/2009/05/hadoop-should-target-cllvm-not-java.html
Sunday, May 10, 2009
Hadoop should target C++/LLVM, not Java (because of watts)
< type="text/javascript">
digg_url="http://www.trendcaller.com/2009/05/hadoop-should-target-cllvm-not-java.html";
Over the years, ......
Linux中的进程通信
1.管道
函数原型:int pipe(int filedes[2]);
函数返回值: 正确返回0;错误返回-1
其中的文件描述符filedes[0]是用来读取数据的,filedes[1]是用来写数据的。
例子1:
#include<stdio.h>
#include<stdlib.h>
#include<u ......
1.打开“我的电脑”-“工具”-“文件夹选项”-“查看”-在“显示所有文件和文件夹”选项前打勾-“确定”
2.删除以下文件夹中的内容:
x:\\Documents and Settings\\用户名\\Cookies\\下的所有文件(保留index文件)
x:\\Documents and S ......
1. 创建类型为Win-32 Static Library的工程:
(1)填写工程名称,工程名称不与已经存在库名相同即可,如mylib;
(2)默认配置,直接点击完成、确定;
(3)在这个工程下新建一个类型为C/C++ Header File的文件,文件名为liwenying.h;
(4)在liwenying.h下面输入代码
......
以下代码演示如何用C来模拟多态。gcc版本:3.4.4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#ifndef class
#define class struct
#endif
#ifndef private
  ......