How to properly manage multiple include chains in a C project? - c

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.

Related

Find references between a header file and some source file without compiling

I am trying to find out dependencies between some source file and some header file. Can I find them without compiling?
because if I have to go to compilation way, then I can exclude the header and compile but still it may go through successfully if this header is being included by other headers which are included in the source file. So, I can't catch the dependencies.
Also, compilation eats time since this is a huge project containing many sources.
Any suggestions on how to go about this effectively?

vim include .h files from projects include directory

EDITED.
Is there a way so that vim detects the path of the header files without using relative paths in the source files like #include "../foo.h"
How to set the path option correctly for the current project folder and how to make a rule for it so that every project with that folder structure can profit?
Are there plugins for it?
I have a multifile c project. int the project folder is one src and one include folder. the src directory contains all .c files, the include folder inhabits all .h files.
project
|_ src
|_ include
example: main.c
#include "..\foo.h"
..
Vim knows about foo.h if the file is within the src folder, but not if it is in the include folder
I have tried setting the path variable in the .vimrc file with no luck.
All other includes from usr/include are working fine.
You must declare either a relative or Absolute path. So there is no "auto detection" just from a file name.
Side note: Vim doesn't care about your paths, the C Preprocessor Include cares about your paths

Gradle C plugin: how to solve references between multiple modules

I have a c language program that has the following structure:
src/main/c/main.c
src/main/headers/main.h
src/module_1/c/module_1.h
src/module_1/headers/module_1.h
...
src/modulen/c/module_n.c
src/module/headers/module_n.h
In the gradle script I have defined:
components {
module_1(NativeLibrarySpec)
...
module_n(NativeLibrarySpec)
main(NativeExecutableSpec){
sources{
c.lib library: "module_1", linkage: "static"
...
c.lib library: "module_n", linkage: "static"
}
The reason of using this structure is to facilitate creating unit tests for each module separately.
The problem comes with the inclusion of the .h files from the modules in the main or in other modules (there are some dependencies between them). I haven't found a way to make the headers of a module available to other modules. I would actually like to make them all "global" to the project (that is, automatically added to the source set for any module).
Thanks in advance
I do not know gradle but may give you some general advise.
I haven't found a way to make the headers of a module available to other modules.
You could make a central directory (repository) for all .h files of your project, for example src/include. The header files of each module can be placed there (in the version of the curent baseline).
I would actually like to make them all "global" to the project (that is, automatically added to the source set for any module).
The above repository can support that. However, including a header in a source file is a manual task. It is also wise not to include all headers into a source file; it may only need a few.

CodeBlocks header common folder

I have multiple projects which have the same header files over and over again, is it possible to create a common utils folder with all the headers and their source files?
Project files with header files
Yes, you can. First make a folder and insert all of your header file in that folder. then create a makefile and you can add that folder for the headers in a Makefile. See here
Other option can be to give path in include like
#include "../Headers/check.h"
#include "../Headers/stackstring.h"

Preserving Header Directory Structure in Xcode for Static Library

I'm developing a static library in C++ using Xcode. I have an Installation Directory set where it copies all of my public header files, but when I compile it just copies all the headers into one directory. Is there a way to tell Xcode to preserve the directory structure of my header files? Thanks in advance!
I also needed to preserve the header file directory structure for a C++ library project and I finally managed to do it. It is ridiculously complicated with XCode, compared to the simple nature of the task. The key is to create "folder references" at first, then to copy the header folders in an extra build phase and afterwards to delete .c/.cpp-files from these exported header folders with a script, because XCode will not only copy the .h-files.
I've written a blog post here on how to all achieve that, because it's more tricky in detail. You might also want to check out an example XCode project that I've pot on github.
When you add files to your project, you have to choose next parameter on an additional window "Create folder references for any added folders". And then all your files will have fixed path for your files and will save structure after compilation.

Resources