How to define (non-methods) functions in header libraries

When writing a header library (e.g. Boost ), you can define free-floating (non-methods) functions without (1) inflating generated binary and (2) carrying "unused" warnings?

When I define a function in the header, which includes several source files, which, in turn, are associated with the same binary code, the linker complains about the redefinition. One way is to make the functions static, but this reproduces the code in each translation unit (BTW, can linkers safely distribute them?). In addition, this causes compiler warnings about an unused function.

I tried to look for an example of a free floating function in Boost, but I could not find it. Is the trick to contain everything in a class (or template)?

+4
source share
5 answers

If you really want to define a function (as opposed to declaring it), you need to use inline to prevent linker errors.

Otherwise, you can declare the function in the header file and provide its implementation separately in the source file.

+12
source

You can use the inline :

 inline void wont_give_linker_errors(void) { // ... } 
+8
source

Er ... The answer to your question is simply not . You simply do not define functions in the header files unless they are inline.

A “static” function can also be defined in headers, but this is only useful for very specific rare purposes. Using "static" only to solve the problem of multiple definitions is complete nonsense.

Again, the header files are for declarations that do not define a function. Why on earth would you like to define functions there?

You said you were writing a "title library". What is a header library? Please note that Boost defines its “functions” in header files, because their “functions” are not really functions, they are functional templates. Function templates should be defined in the header files (well, almost). If this is not the case, Boost will not do anything strange, like defining anything in header files.

+2
source

Besides the already mentioned inline , with most compilers, templates should be defined in the headers (and with all its compilers). Since boost is mostly templates, this explains why it's almost all the headers.

+1
source

People suggested inline , but this violates the very first part of your question, that is, it inflates the code, since the full definition is inserted into the code with every function call. Therefore, the answer to your general question is "No."

If you mark them as static , then they are still defined in each source file, as you correctly pointed out, but only once, and therefore a better option than inline if the code size is the only problem. I do not know if linkers, or are allowed, can specify duplicates and merge them. I do not suspect.

Edit

Just to eliminate any confusion as to whether I support the concept of using static and / or defining functions in header files, in the general case, be sure that I do not. It simply meant a technical response to the differences between the functions marked inline and static defined in the header files. Nothing more.

-one
source

All Articles