I have a function (lets call it void foo(void) {}
) that I wish to be inline.But I defined it inside .cpp file.
I can not move the function to the header because it is using other functions from .cpp.
EDIT:
code below just like real one is not using c++ features. 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. I am asking also about c++ because meaby it can solve my problem while c99 can not.
therefore C and C++ tags in my question are justified. the code is portable
see: When to use extern "C" in C++?
EDIT #2
Ok i understand that i must put foo()
definition inside header file. How can I call func_a()
and func_b()
from foo()
without moving them to the header file? Is there any workaround?
Example
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:
// Something before.
void foo (int a, double b); // function declaration
// Something after.
I want to point out:
- I am working with Visual Studio 2015.
- I am writing relatively big project ( ~ 7000 Lines Of Code ). It is simulation of physical system.
file.h
is included to many other compilation units.foo(int, double)
is small and it calls other functions fromfile.cpp
- I want to make
foo(int, double)
inline because of optimization reasons ( I will end up using__forceinline__
) - I am calling
foo()
many, many times across my program. Also it is used inside few while loops ( 100k + iterations each ) so it is expansive to let this symbolfoo()
be not inline. - I am sick of header-only libraries.
I tried to:
file.c / .cpp:
extern inline
void foo (int a, double b) {\*...*\}; // Definition
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:
- keep definition of
foo()
inside my .cpp file ? But still make it inline - move
foo()
to thefile.h
, keepfunc_a()
andfunc_b()
insidefile.cpp
, but makefunc_a()
andfunc_b()
symbols "visible" to thefoo()
( ! and only to thefoo()
! ) fromfile.cpp
- any other workaround?
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.