【发布时间】:2020-11-26 18:10:48
【问题描述】:
假设我有两个文件:
1.c
inline int test(int in){
return in+1;
}
int get();
int main(){
return get();
}
2.c
int test(int in){
return in+9;
}
int get(){
return test(5);
}
使用gcc 1.c 2.c 编译顺利,没有错误。
这是定义的行为吗?即1.c 中的test 与其他函数不同,我们基本上在代码中生成了2 个test 函数?
【问题讨论】:
-
尝试在
main中调用get。 -
来自en.cppreference.com/w/c/language/inline : 如果非静态函数被声明为内联,那么它必须在同一个翻译单元中定义。不使用 extern 的内联定义在外部是不可见的,也不妨碍其他翻译单元定义相同的函数。这使得 inline 关键字可以替代 static 用于在头文件中定义函数,这些函数可能包含在同一程序的多个翻译单元中。
-
@P__JsupportswomeninPoland 我做了,在上面进行了相应的编辑。编译正常,2.c 中的定义按预期使用。 14 在 main 中返回。