【发布时间】:2017-09-21 07:49:26
【问题描述】:
我在 C 源文件中定义了一些静态函数。编译后,我用nm工具显示.o文件中的所有符号,发现我定义的所有静态函数都在rodata部分,它们的符号名称为func.xxxx。该函数不应该位于文本函数中吗?
以下是 nm 命令显示静态函数的结果。 func.xxxx 前面的 'r' 表示函数存储在rodata部分。
00000000 t desc_to_mattr
U __do_panic
00000000 r __func__.5546
00000000 r __func__.5554
00000000 r __func__.5560
00000000 r __func__.5565
00000000 r __func__.5604
00000000 r __func__.5638
00000000 r __func__.5698
00000000 r __func__.5710
00000000 r __func__.5719
00000000 r __func__.5729
00000000 r __func__.5758
以下是我的 gcc 选项:
arm-linux-gnueabihf-gcc -std=gnu99 -Werror -fdiagnostics-show-option -Wall -Wcast-align -Werror-implicit-function-declaration -Wextra -Wfloat-equal -Wformat-nonliteral -Wformat-security -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wwrite-strings -Wno-missing-field-initializers -Wno-format-zero-length -Waggregate-return -Wredundant-decls -Wold-style-definition -Wstrict-aliasing=2 -Wundef -pedantic -Wdeclaration-after-statement -Os -g -ffunction-sections -fdata-sections -pipe -g3 -mcpu=cortex-a9 -mfloat-abi=soft -funwind-tables -mthumb -mthumb-interwork -fno-short-enums -fno-common -mno-unaligned-access -MD -MF
【问题讨论】:
-
@M.M
static函数对链接器可见吗?很可能它们由编译器处理,而链接器甚至看不到它们。 -
如果你只是定义函数但不使用它,它不一定会去任何地方:它可能会被编译器当场丢弃。如果你只使用它一次,它很可能会被编译器内联不管它有多长,并且不会像上面那样编译任何独立版本。关键是,当您说
static时,编译器已经知道该函数的所有 用途,并且可以进行相应的优化。 -
您认为
__func__是什么意思?您必须知道,因为它存在于您的 C 代码中。 -
@Lundin 但它可能会被某些库等的某些宏所掩盖......
-
@cmaster,我想你是对的,我只使用了一次静态函数,它应该被编译器内联。谢谢。
标签: c gcc compilation