I have a number of source files in a number of folders.. Is there a way to just compile all of them in one go without having to name them?
I know that I can say
g++ -o out *.cpp
But when I try
g++ -o out *.cpp folder/*.cpp
I get an error.
What's the correct way to do this? I know it's possible with makefiles, but can it be done with just straight g++?
Figured it out! :) I hate the idea of having to use make or a build system just to compile my code, but I wanted to split up my code into subfolders to keep it clean and organized.
Run this (replace RootFolderName (e.g. with .
) and OutputName):
g++ -g $(find RootFolderName -type f -iregex ".*\.cpp") -o OutputName
The find command will do a recursive case-insensitive regex search (-iregex
) for files (-type f
). Placing this within $()
will then inject the results into your g++
call. Beautiful! :)
Of course, make sure you're using the command right; you can always do a test run.
For those using Visual Studio Code to compile, I had to do this in the tasks.json args: []
of a task with "command": "g++"
:
"-g",
"$(find",
"code",
"-type",
"f",
"-iregex",
"'.*\\.cpp')",
"-o",
"program"
(Otherwise it would wrap the $()
all in single quotes.)
(Thanks to: user405725 @ https://stackoverflow.com/a/9764119/1599699 comments)