2

To compile all C++ files in my source directory I run

g++ -std=c++17 ../src/*.cpp -o ../out/a.out

How can I compile all cpp files in a given directory except for main.cpp?

K. Claesson
  • 525
  • 5
  • 14

3 Answers3

4

bash:

shopt -s extglob
g++ -std=c++17 ../src/!(main).cpp -o ../out/a.out

ref: https://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching

glenn jackman
  • 223,850
  • 36
  • 205
  • 328
1
for f in $(find /path/to/files -name "*.cpp" ! -name "main.cpp")
do
  g++ -std=c++17 path/to/files/"$f" -o /path/to/out/....
done
nullPointer
  • 4,188
  • 1
  • 14
  • 27
1

We can filter the glob into a Bash array:

unset files
for i in ../src/*.cpp
do test "$i" = '../src/main.cpp' || files+=("$i")
done

g++ -std=c++17 "${files[@]}" -o ../out/a.out

Or, using GNU grep and mapfile:

mapfile -d $'\0' -t files < <(printf '%s\0' ../src/*.cpp | grep -zv '/main\.cpp$')
g++ -std=c++17 "${files[@]}" -o ../out/a.out
Toby Speight
  • 25,191
  • 47
  • 61
  • 93