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
  • 603
  • 8
  • 22

4 Answers4

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
  • 238,783
  • 38
  • 220
  • 352
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,419
  • 1
  • 15
  • 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
  • 27,591
  • 48
  • 66
  • 103
0

you can also ls all .cpp files and pipe that into grep -v, to exclude a specific result.

g++ -std=c++17 `ls *.cpp | grep -v main.cpp` -o a.out