Sign in to follow this  
noatom

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this