0

Noob question here. I have a C project with a lot of files. And each of these files have other files as dependencies. Right now, for each library, I've created a ".h" file and a ".c" file. The ".h" files have the function prototypes and macro-definitions and the ".c" files have the function definitions. My question is whether I should include other libraries required in the ".h" file or the ".c" file. Also, are variables defined in the ".c" file available in the global scope even though the ".h" file is included in the main program. How do I manage compiler conflicts from having the same file multiple times? Someone kindly explain the whole thing to me.

sixter
  • 163
  • 1
  • 3
  • 11
  • 1
    Include header guards `#ifndef __MY_Header_File__ #define __MY_Header_File__ #include "someheader.h" #endif` to prevent multiple inclusion. – David C. Rankin Apr 23 '19 at 04:19
  • Amongst others, see [Should I use `#include` in headers?](https://stackoverflow.com/questions/1804486/should-i-use-include-in-headers/1804719#1804719) and [Self-sufficient header files in C/C++](https://stackoverflow.com/questions/1892043/self-sufficient-header-files-in-c-c/1892332#1892332) and [Including header file from static library](https://stackoverflow.com/questions/27660713/including-header-file-from-static-library/27660748#27660748) and [How to link multiple implementation files in C](https://stackoverflow.com/questions/15622409/how/15622496#15622496) – Jonathan Leffler Apr 23 '19 at 06:24
  • You might have to use the `extern` keyword to share the global variable with other C-files. However, global variables are considered bad style. – E. van Putten Apr 23 '19 at 07:37
  • Possible duplicate of [Self-sufficient header files in C/C++](https://stackoverflow.com/questions/1892043/self-sufficient-header-files-in-c-c) – Kamiccolo Apr 24 '19 at 15:10

0 Answers0