Reputation: 764
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
Upvotes: 3
Views: 2648
Reputation: 11
You can use this task in your tesks.json
. It will automatically run make
command in Command Prompt when you press Run C/C++ File
bottom in vscode.
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: make.exe build active file",
"command": "C:\\Windows\\System32\\cmd.exe",
"args": [
"/c",
"chcp",
"65001>nul",
"&&",
"cd",
"${workspaceFolder}",
"&&",
"make"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
To use this task, simply add makefile
to the project workspace folder. If the file structure of the workspace is as follows,
C:.
│ makefile
│
├───.vscode
│ launch.json
│ tasks.json
│
├───build
│ test.exe
│
├───include
│ common.h
│ ...
│
├───src
│ common.c
│ ...
│
└───test
test.c
then makefile
will look like this.
# C version lable
CVERLBL = -std=c17
# warning label
WARNLBL = -Wall -Wextra
# optimization label
OPTILBL = -O3
# diagnostics messages formatting label
DXMFLBL = -fdiagnostics-color=always
# all labels in one line
LABELS = $(DXMFLBL) $(CVERLBL) $(WARNLBL) $(OPTILBL)
# source files
SOURCES = $(wildcard src/*.c)
# source files for test
TESTSRC = $(wildcard test/*.c)
# include directory
INCDIR = include
# output file location
OUTPUT = build/test.exe
all:
gcc $(LABELS) -g $(SOURCES) $(TESTSRC) -I $(INCDIR) -o $(OUTPUT)
Here is the output of the build.
Starting build...
cmd /c chcp 65001>nul && C:\Windows\System32\cmd.exe /c chcp 65001>nul && cd C:\Users\usr\test_project && make
gcc -fdiagnostics-color=always -std=c17 -Wall -Wextra -O3 -g src/common.c test/test.c -I include -o build/test.exe
Be sure you have make.exe
(or mingw32-make.exe
) in your environment path.
Upvotes: 1
Reputation: 31
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)
Upvotes: 2
Reputation: 1838
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
Upvotes: 7