I'm doing basically the same thing (i.e. use MinGW GCC on Windows with C files). I use the -g option for each directory whose .c/.h files I want included in the compilation.
For example, if I want to compile everything in the myFolder directory, this works for me:
gcc -g c:\myFolder\*.c -o foo.exe
Note you can use the -g option multiple times on the commandline to include multiple directories. For example, I organize my .c/.h
files into various subfolders inside myFolder. So to tell gcc about mySubdir that is inside myFolder, this is what I do:
gcc -g c:\myFolder\*.c -g c:\myFolder\mySubdir\*.c -o foo.exe
Note that for any .h files that I put in such sub directories that I need to reference from C files in the parent dir, I have to use the relative path in #include
.
For example, to reference foo.h, which lives inside myFolder/subDir, from a C file that lives in myFolder, I do:
#include "mySubdir/foo.h"
And that's basically it.
Now, for the sake of completeness, if you happen to use VSCode as I am for my C work (which is not necessarily optimal, but ok'ish), then you can tweak this setting in the .vscode/tasks.json, specifying each -g
option separately, for example:
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${fileDirname}/*.c",
"-g",
"${fileDirname}/mySubdir/*.c",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
(my GCC version is 10.3.0)