includes some function prototypes but doesn't include function definitions. And
after linking definitions are also included as machine code, right?
If we ourselves create a header files, should we provide both function prototypes and definitions in the header file, or should separate them into two header file and library file?
Prototypes (declarations) are in headers. Definitions (implementations) are in library files. We don't have to include function implementations unless you have a static library and wish to statically link the function into your executable.
If you create own global functions and put the declaration into a header file, then you should define it in a .c file. If you define it in the header file and someone would include your header file in multiple .c files and then compile and link them, the linker would complain, that there are multiple definitions of your function. That's because an #include basically just copies the content of the header file into the .c file. There is an exception. You could put static function definitions into a header file. Static function have only file scope. They can't be used from different .c files. Thus, a linker wouldn't search for it in other .o files and also wouldnt complain that it finds more definitions of the function with that name.
Обсуждают сегодня