是否必须在同一文件中声明c ++内联函数? [英] Do c++ inline functions have to be declared in same file?

查看:171
本文介绍了是否必须在同一文件中声明c ++内联函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含所有声明的头文件,一个.cpp文件被编译为.o文件,并通过main方法被编译为.cpp文件.

I have a header file with all the declarations, one .cpp file that is compiled to a .o file and a .cpp file with the main method.

奇怪的是,当.o文件中的某个函数在标头中声明为内联时,程序将无法编译(我在cygwin上使用g ++);仅当正常声明时.

The strange thing is, the program won't compile (I'm using g++ on cygwin) when a certain function in the .o file is declared inline in the header; only when declared normally.

那么,内联函数是否必须在使用它们的同一个文件中声明?

So, do inline functions have to be declared in the same file that they're used?

推荐答案

那么,内联函数是否必须在使用它们的同一个文件中声明?

So, do inline functions have to be declared in the same file that they're used?

使用前必须声明所有功能(通常是所有命名实体);内联函数还必须在使用过的每个翻译单元中定义.

All functions (and, in general, all named entities) must be declared before use; an inline function must also be defined in every translation unit in which it is odr-used.

因此,它们通常在标头中定义,因此该定义可以包含在多个翻译单元中.

For this reason, they are usually defined in headers, so that the definition can be included in more than one translation unit.

这篇关于是否必须在同一文件中声明c ++内联函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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