# c++ inline function problem

## Recommended Posts

Ok,I was messing aroung with some code and a problem appeared:

FILE1.CPP

#include <iostream>
using namespace std;
inline void fishy(){
cout << "da";
}

main.cpp

#include <iostream>
#include <string>

using namespace std;

extern void fishy();

int main() {
fishy();

}


As you can see I have 2 files: 1 contains an inline function definition,and the other declares the function as extern.When I try to compile the code i get a linker error.

I just need someone to tell me if I'm right but: inline functions don't have external linkage? And if that is true,what is the alternative? Using header files for all of them?

##### Share on other sites

Yep, inline means no external linkage.

Put the definition in a header included by the cpp files that use the function (or put them in a cpp file which you #include in a header, or call it myfile.inl instead if you want a different extension).

##### Share on other sites

Regardless of whether or not the compiler actually inlines the function, it still treats it semantically as though it were, so there is no function call to link against.

##### Share on other sites

Ok,no external linkage for the poor inline functions:) Thanks

##### Share on other sites

Make sure you don't put using namespace std in a header file though, that defeats the purpose of namespaces. You need to fully qualify std::cout or put using std::cout inside the function body to minimise any namespace symbol imports.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628342
• Total Posts
2982177

• 9
• 24
• 9
• 9
• 13