1

inline使用注意事项

 3 years ago
source link: https://answerywj.com/2021/07/28/tips-of-inline/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

inline使用注意事项

发表于 2021-07-28

| 分类于 C/C++

| 0

| 阅读次数 6次

字数统计: 501 字

|

阅读时长 ≈ 2 分钟

GCC在不优化时不会内联任何函数,除非指定函数的“always_inline”属性。


先附上结论:GCC在不优化时不会内联任何函数,除非指定函数的“always_inline”属性。

测试代码:

#include <stdio.h>

inline void say(void) {
printf("Hello, World\n");
}

int main(void) {
say();
return 0;
}

使用-O3优化选项,一切正常:

$ gcc -O3 -o test_O3.o -c test.c
$ g++ test_O3.o -o test_O3
$ ./test_O3
Hello, World

使用-O0优化选项,链接时报错,提示找不到内联函数say

$ gcc -O0 -o test_O0.o -c test.c
$ g++ test_O0.o -o test_O0
test_O0.o: In function `main':
test.c:(.text+0x5): undefined reference to `say'
collect2: error: ld returned 1 exit status

分别查看文件test_O0.otest_O3.o

$ readelf -s test_O0.o

Symbol table '.symtab' contains 10 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.c
2: 0000000000000000 0 SECTION LOCAL DEFAULT 1
3: 0000000000000000 0 SECTION LOCAL DEFAULT 3
4: 0000000000000000 0 SECTION LOCAL DEFAULT 4
5: 0000000000000000 0 SECTION LOCAL DEFAULT 6
6: 0000000000000000 0 SECTION LOCAL DEFAULT 7
7: 0000000000000000 0 SECTION LOCAL DEFAULT 5
8: 0000000000000000 16 FUNC GLOBAL DEFAULT 1 main
9: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND say


$ readelf -s test_O3.o

Symbol table '.symtab' contains 13 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.c
2: 0000000000000000 0 SECTION LOCAL DEFAULT 1
3: 0000000000000000 0 SECTION LOCAL DEFAULT 2
4: 0000000000000000 0 SECTION LOCAL DEFAULT 3
5: 0000000000000000 0 SECTION LOCAL DEFAULT 4
6: 0000000000000000 0 SECTION LOCAL DEFAULT 5
7: 0000000000000000 0 SECTION LOCAL DEFAULT 6
8: 0000000000000000 0 SECTION LOCAL DEFAULT 9
9: 0000000000000000 0 SECTION LOCAL DEFAULT 10
10: 0000000000000000 0 SECTION LOCAL DEFAULT 8
11: 0000000000000000 21 FUNC GLOBAL DEFAULT 6 main
12: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND puts

可以发现文件test_O0.o中,inline修饰的函数say为未定义状态,说明inline函数并没有展开。

指定say函数为“always_inline”属性:

#include <stdio.h>

__attribute__((always_inline)) inline void say(void) {
printf("Hello, World\n");
}

int main(void) {
say();
return 0;
}

重新使用-O0优化选项编译运行,一切ok

$ gcc -O0 -o test.o -c test.c
$ gcc -O0 -o test_O0_2.o -c test.c
$ g++ test_O0_2.o -o test_O0_2
$ ./test_O0_2
Hello, World






About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK