C++代码调用C以及extern “C”用法
1 Gcc编译库测试
1.1 头文件(tool.h)
#ifndef __TOOL_H
#define __TOOL_H
#ifdef __cplusplus
extern “C” {
#endif
void hello();
#ifdef __cplusplus
}
#endif
#endif
1.2 source代码(tool.c)
#include “tool.h”
#include “stdio.h”
void hello(int val,const char* msg)
{
printf(“val=%d,msg=%s\n”,val,msg);
}
1.3 Gcc编译成静态库
gcc -c tool.c
ar -rcs libtool.a tool.o
1.4 查看符号信息
readelf -s -W libtool.a
File: libtool.a(tool.o)
Symbol table ‘.symtab’ contains 11 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS tool.c
2: 000000000000