Reputation: 15284
I have the following files:
//utils.hpp
namespace Utils {
inline void saveFile(FileInfo &);
}
and implementation:
//utils.cpp
#include "./utils.hpp"
inline void Utils::saveFile(FileInfo & f) {}
and main.cpp
#include "./utils.hpp"
int main(){
//...
Utils::saveFile(f);
}
But when I try to compile, it gives me error:
main.cpp.o: In function `main':
main.cpp:(.text+0x358): undefined reference to `Utils::savePPMFile(FileInfo &)'
collect2: ld returned 1 exit status
make[2]: *** [simpletest] Error 1
make[1]: *** [CMakeFiles/simpletest.dir/all] Error 2
make: *** [all] Error 2
If I remove inline
, everything works fine... How to use the inline function?
Upvotes: 1
Views: 626
Reputation: 1525
If you want to have inline functions and still want to keep your header file clean, you can have the implementation of the inline functions in another file (it could be any extension, Microsoft uses .inl
extension for such files) and include at the end of your header file as shown below.
//utils.hpp
namespace Utils {
inline void saveFile(FileInfo &);
}
#include "utils.inl"
And in utils.inl file
//utils.inl
inline void Utils::saveFile(FileInfo & f) {}
Now you can use util.hpp in your main program. util.inl automatically gets included to your main cpp file.
#include "./utils.hpp"
int main(){
//...
Utils::saveFile(f);
}
Upvotes: 1
Reputation: 254451
Inline functions must be defined in every translation unit that uses them. Either put the definition in the header, or remove the inline
specifier.
Upvotes: 5