[英]extern inline function was referenced but not defined
I have a function (lets call it void foo(void) {}
) that I wish to be inline.But I defined it inside .cpp file. 我有一个函数(让我们称之为
void foo(void) {}
)我想要内联。但我在.cpp文件中定义它。
I can not move the function to the header because it is using other functions from .cpp. 我无法将函数移动到标题,因为它正在使用.cpp中的其他函数。
EDIT: 编辑:
code below just like real one is not using c++ features. 下面的代码就像真正的代码一样,不使用c ++功能。 I can clearly add:
我可以清楚地补充:
#ifdef __cplusplus
extern "C"
#endif
If i would like to convert code below to c99 i need only to change my project properties (or makefile if you like) NOT THE CODE . 如果我想将下面的代码转换为c99,我只需要更改我的项目属性(如果你愿意,还可以更改makefile),而不是代码 。 I am asking also about c++ because meaby it can solve my problem while c99 can not.
我也问c ++因为meaby它可以解决我的问题,而c99不能。
therefore C and C++ tags in my question are justified. 因此我的问题中的C和C ++标签是合理的。 the code is portable
代码是可移植的
see: When to use extern "C" in C++? 看: 何时在C ++中使用extern“C”?
EDIT #2 编辑#2
Ok i understand that i must put foo()
definition inside header file. 好吧,我明白我必须把
foo()
定义放在头文件中。 How can I call func_a()
and func_b()
from foo()
without moving them to the header file? 如何从
foo()
调用func_a()
和func_b()
foo()
而不将它们移动到头文件? Is there any workaround? 有没有解决方法?
Example 例
file.c / .cpp: file.c / .cpp:
int func_a (int a, int b, int c) {/*...*/};
double func_b (double a, double b, double c) {/*...*/};
void foo (int a, double b) { // unction definition
//...
int myInt = func_a(a, 2, 3);
//...
double myDouble = func_b(1.5f, b, 3.5f);
//...
}
file.h: file.h:
// Something before.
void foo (int a, double b); // function declaration
// Something after.
I want to point out: 我想指出:
file.h
is included to many other compilation units. file.h
包含在许多其他编译单元中。 foo(int, double)
is small and it calls other functions from file.cpp
foo(int, double)
很小,它从file.cpp
调用其他函数 foo(int, double)
inline because of optimization reasons ( I will end up using __forceinline__
) foo(int, double)
内联(我最终会使用__forceinline__
) foo()
many, many times across my program. foo()
。 Also it is used inside few while loops ( 100k + iterations each ) so it is expansive to let this symbol foo()
be not inline. foo()
不是内联的是扩展的。
I tried to: 我试过了:
file.c / .cpp: file.c / .cpp:
extern inline
void foo (int a, double b) {\*...*\}; // Definition
file.h: file.h:
extern inline
void foo (int a, double b); // Declaration
but I keep getting an warning: 但我一直在警告:
warning : extern inline function "foo" was referenced but not defined
And i do not understand why i am getting this warning. 我不明白为什么我会收到这个警告。
Is there any way to: 有没有办法:
foo()
inside my .cpp file ? foo()
定义? But still make it inline foo()
to the file.h
, keep func_a()
and func_b()
inside file.cpp
, but make func_a()
and func_b()
symbols "visible" to the foo()
( ! and only to the foo()
! ) from file.cpp
foo()
移到file.h
,将func_a()
和func_b()
在file.cpp
,但使func_a()
和func_b()
符号对foo()
“可见”(!并且只对foo()
!)来自file.cpp
Sory I am still learning cpp and I dont know if my question is clear, or is it even possible to make function inline from .cpp file. Sory我还在学习cpp而且我不知道我的问题是否清楚,或者甚至可以从.cpp文件中生成内联函数。
In C++, the inline
keyword means this: 在C ++中,
inline
关键字意味着:
That's all that it does. 就是这样。 In practice, this means that it enables and forces you to put the function body into a header file.
实际上,这意味着它启用并强制您将函数体放入头文件中。
The keyword itself has nothing to do with function inlining, per se. 关键字本身与函数内联无关。 It's just that if the compiler wants to be able to inline the function's body, it must have access to it when compiling the calling code;
只是如果编译器希望能够内联函数的主体,它在编译调用代码时必须能够访问它; in other words, the function's body must be in the same source file or in a header file included by that source file.
换句话说,函数的主体必须位于同一源文件或该源文件包含的头文件中。
If you want to enable the compiler to inline the function in all call sites, and these call sites happen in more than one source file, you have to put the function into a header file (and mark it as inline
). 如果要使编译器内联所有调用站点中的函数,并且这些调用站点发生在多个源文件中,则必须将该函数放入头文件(并将其标记为
inline
)。
On the other hand, it is also possible for the linker to do inlining, called Link-Time Code Generation or Whole Program Optimisation or something similar (depending on your toolchain's vendor). 另一方面, 链接器也可以进行内联,称为链接时代码生成或整个程序优化或类似的东西(取决于您的工具链的供应商)。 Enabling this will
启用此功能
So to inline the function, you have two options. 所以为了内联函数,你有两个选择。 Either put the function into a header file, or enable and rely on link-time optimisation to inline it for you.
将函数放入头文件,或启用并依赖链接时优化来为您内联。
In your particular case, to use __forceinline__
, you'll have define foo
in a header file. 在您的特定情况下,要使用
__forceinline__
,您将在头文件中定义foo
。 This requires the definition of foo
to see declarations of func_a
and func_b
. 这需要
foo
的定义来查看func_a
和func_b
声明。 To prevent namespace polution, you can declare these functions within the scope of foo
: 要防止名称空间污染,您可以在
foo
的范围内声明这些函数:
inline void foo(int a, double b) { // function definition
int func_a(int, int, int);
double func_b(double, double, double);
//...
int myInt = func_a(a, 2, 3);
//...
double myDouble = func_b(1.5f, b, 3.5f);
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.