C头文件中的内联函数 [英] Inline function in header file in C

查看:264
本文介绍了C头文件中的内联函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在线搜索一个好的答案,但没有得到我可以完全理解的答案.假设我有一个标题"add.h":

I tried to search a good answer online but fail to get one I can fully understand. Suppose I have a header "add.h":

inline int add(int a, int b){ return a+b; }

名为"adddouble.c"的文件:

A file called "adddouble.c":

#include "add.h"

int adddouble(int a, int b){ return 2*add(a,b); }

名为"addsquare.c"的文件:

A file called "addsquare.c":

#include "add.h"

int addsquare(int a, int b){ return add(a,b)*add(a,b); }

主文件"main.c":

A main file "main.c":

#include<stdio.h>

int adddouble(int, int);
int addsquare(int, int);
int main(){
printf("add double = %d\n", adddouble(10,20));
printf("add square = %d\n", addsquare(10,20));
return 0;
}

我使用gcc5.2.0编译这些文件,但得到:体系结构x86_64的未定义符号:".如果我在add.h中将static添加到内联函数中,或者 添加声明"extern int add(int,int);"到"adddouble.c",它会编译 成功而没有错误.我是内联函数的新手,我不知道如何解释和理解这种行为.谢谢

I use gcc5.2.0 to compile those files, but got: "Undefined symbols for architecture x86_64:". If I add static to inline function in add.h or add declaration "extern int add(int, int);" to "adddouble.c", it compiles successfully without errors. I am new to inline function, I don't know how to explain and understand this behaviour. thanks

推荐答案

根据 http://www.open-std.org/jtc1 /sc22/wg14/www/docs/n1570.pdf , 看起来内联函数在C和C ++中的行为有所不同:

According to http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf, it looks like inline functions behave differently in C and C++:

任何具有内部链接的函数都可以是内联函数.为一个 具有外部链接的功能,则存在以下限制:如果 用内联函数说明符声明函数,然后应 也可以在同一翻译单元中定义.如果所有文件范围 翻译单元中函数的声明包括内联 不带extern的函数说明符,则其中的定义 翻译单元是一个内联定义.内联定义 没有提供功能的外部定义,并且没有 禁止在另一个翻译单元中使用外部定义.内联 定义提供了外部定义的替代方法,其中 转换器可以用来在同一位置实现对该函数的任何调用 翻译单位.未指定是否调用该函数 使用内联定义或外部定义. 140)

Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit. If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition. 140)

如果您未在其中放置static,则说明您在不实例化外部定义的情况下使用外部链接定义了内联函数.

If you don't put static there, you're defining an inline function with external linkage without instantiating an external definition.

extern int add(int, int);会实例化该函数的外部定义,并且您只需进行一次操作(与在哪个文件中无关),链接就可以成功(或者可以标记内联函数static和问题)消失).

extern int add(int, int); instatiates the external definition for that function, and you need to do that exactly once (doesn't matter in which file) for the linking to succeed (or you can mark the inline function static and the problem vanishes).

(在C ++中,inline在每个翻译单元中创建 weak 外部定义,因此您不必担心标记static或为内联函数精确实例化一个外部定义的情况, -g ++应该直接用for f in *.c; do g++ -c "$f"; done; g++ *.o编译它,没有任何问题.)

(In C++ the inline creates weak external definitions in each translation unit so you don't have to worry about marking things static or instantiating exactly one external definition for the inline function--g++ should straightforwardly compile it with for f in *.c; do g++ -c "$f"; done; g++ *.o without any problems.)

这篇关于C头文件中的内联函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆