I'm trying to define a function in a CPP file that is forward declared in the header file. I'd like to know the proper way to do it, since everything I've tried compiles and runs, and my analysis skills are not good enough to investigate if the functions are truly inlined.
Here's what I'd like to do
/// Source.h
void inlined_func(); // what specifiers should I put here?
// I was thinking about doing both `extern` and `__forceinline`
/// Source.cpp
__forceinline void inlined_func()
{
std::cout << "we're in the inlined func" << std::endl;
}
A function declared as __forceinline by default gets internal linkage (the name can be referred to only in the current translation unit) as if declared as static. If you try to use it in another translation unit, you'll get a linker error LNK2001 unresolved external symbol .... To force external linkage, so that it can be referred to in other translation units, use extern keyword.
foo.h
void foo();
foo.cpp
#include <foo.h>
extern __forceinline void foo() {
/*...*/
}
main.cpp
#include <foo.h>
int main() {
foo();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With