extern "C" 详解
extern "C" 详解
在C++中,为了支持重载机制,在编译生成汇编代码时,函数的名字要加入函数的参数类型或者返回值类型等信息
在C中,因没有重载机制,编译后的代码只是简单的函数名字而已,不加入其他的信息
1. 不加入extern "C"
testexternc.cpp
int mytest(void)
{
int a=10,b=20;
int c=a+b;
return c;
}
Command: g++ -S testexternc.cpp或者gcc -S testexternc.cpp
Generated file: testexternc.s
.file "testexternc.cpp"
.text
.align 2
.globl _Z6mytestv
.type _Z6mytestv, @function
_Z6mytestv:
.LFB2:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
subl $16, %esp
.LCFI2:
movl $10, -12(%ebp)
movl $20, -8(%ebp)
movl -8(%ebp), %eax
addl -12(%ebp), %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
leave
ret
.LFE2:
.size _Z6mytestv, .-_Z6mytestv
.globl __gxx_personality_v0
相关文档:
题目:输入三个字符串a,b和c,将a中b的第一次出现替换为c。
代码:
#include <iostream.h>
#include <string.h>
/*字符串替换,第一个参数为原串,第二个参数为要匹配的子串
第三个参数为要替换的第一个子串中包含第二个子串的部分*/
char *strReplace(char *str1,char *str2,char *str3);
void main() ......
#include <stdio.h>
int main()
{
char *str[] = {"welcome", "to", "fortemedia", "nanjing"};
char **p = str + 1;
str[0] = ( *p++ ) + 2;
str[1] = * ( p + 1 );
str[2] = p[1] + 3;
str[3] = p[0] + ( str[2] - str[1] );
printf ( "%s\n", str[0] );
printf ( ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. setvect函数
setvect函数的功能是设置中断矢量入口,其用法为:void setvect(int intr_num, void interrupt(*isr)());程序实例如下:
#include <st ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. sopen函数
sopen函数的功能是打开一共享文件,其用法为:int sopen(char *pathname, int access, int shflag, int permiss);程序实例如下:
#include ......
今天要编个最简单的"hello world"的C程序,算是初步感受下Linux中的编程环境,涉及以下3个步骤:
1. 先在"vim"里编写源文件;
2. 然后再用"gcc"编译生成"hello"的可执行文件;
3. 运行"hello",看看效果。
那么开始吧!
1. vim
vim hello.c
i
#include <stdio.h>
int
main (void)
{
printf ("hello.\n" ......