简体   繁体   中英

How to build a header only lib without inline statment for each function?

In my code, I have standard and templatized functions. It seems to me that it is not good practice to have the declaration of standard functions in foo.h, their definition in foo.cpp, and the definition of template functions in foo.h. It's a little messy to have definition in both files. Like that:

foo.h

//Declaration of standard function
void stdFunc(int i);

//Definition of template function
template<typename T>
inline void templFunc(const T& t){
  cout << t << endl;
}

foo.cpp

#include "foo.h"

//Definition of standard function
void stdFunc(int i){
  cout << i << endl;
}

So I decided to define my functions in the headers. But now, all functions need to have the inline statment to avoid linker error, which is not recommended either.
How do header-only libraries like Boost or CGAL to not declare all their functions as inline?

If you want a header-only library then it must only contain templates or inline functions. There is no other way.

Note: inline has nothing to do with inlining but means the function may appear multiple times in the translation units without giving a duplicate defintion error when linking.

It's a promise that all the ocurances of the functions will be the same and the linker shall pick one of them as the representing object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM