3

I'm using MinGW GCC compiler on windows, I need to compile all c files in a folder!

I've tried

gcc  *.c -o  Output {folder Path}

I got this error

gcc: error: *.c: Invalid argument 
gcc: fatal error: no input files

then the compilation terminated.

the used version of GCC is 4.7.1

Mohammad Desouky
  • 734
  • 7
  • 15

2 Answers2

6

gcc does not accept a wildcard (*.c) as input file.

You may write a script (batch@windows or .sh @Linux/Unix) which finds all source files and compile them one by one.

but you SHOULD use a makefile or CMAKE to organize your sources and their buildsystem. please read here

nivpeled
  • 1,810
  • 5
  • 17
1

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)