[细节扣扣]inline那些事

来源:互联网 发布:越南网络电视直播 编辑:程序博客网 时间:2024/04/28 18:25

给个简单的例子:

test1.h:

void run();

test1.c:

inline void run(){ do sth. }

test2.c:

#include "test1.h"

int main(){

  run();

  return 0;

}

分别用gcc 和 g++ 执行

gcc test1.c test2.c

g++ test1.c test2.c

您的第一答案是啥?都能编译通过吗?

看下在gcc 4.8.1下,是能通过,但是在g++ 4.8.1下,打印出了错误:

test2.o: In function `main':
test2.c:(.text+0x7): undefined reference to `run()'
collect2: error: ld returned 1 exit status

两者在预处理的过程中都保留了inline关键字。

nm了在gcc和g++情况下的test1.o的符号表,发现g++并没有将inline run 函数编译到.o文件,不会生成独立的汇编码。


0 0