Header only connection

Many C ++ projects (for example, many libraries Boost) are associated only with the title.

Is this also possible in the ordinary C? How to put source code in headers? Are there any sites in it?

+5
source share
4 answers

Summary: you can, but should not.

C and C ++ code is pre-processed before compilation: all headers are “inserted” into the source files that include them, recursively. If you define a function in the header and include two C files, you will get two copies in each object file ( violation of rule 1).

, static, . , , .

++ : static, , , , , ( "" ).

C , (, queue(3)). ++ , , C.

+11

.

++ , , , . 1

inline , C, ++. 2

// Won't cause redefinition link errors, because of 6.7.4/5
inline void foo(void) {
   // ...
}

[c99: 6.7.4/5:] , inlinespecifier . ; , . , . , , .

, .


1 - .
2 - C99 . C89/C90 .

+4

Boost - , ( ) C.

, , , C, #include, . " ..." C C .

+4

, . static, (.. c) .

, , , C. , -fwhole-program, .. . , . , .

static .

+3

All Articles