0

Is it possible to compile multiple files and save the output for each files with different name ? I had only copied .cpp files from old computer to my new one.Those programs are all error free and are tested.So now i have to compile each files to get the output file.There are about 310 programs so it is really hard to compile each file separately.I usually save output file with the same file name without any extension.Is there any way to compile all files in the directory and save each files output separately. I'm looking forward for a command like this

gcc *.cpp -o *  

If there are files,

filename1.cpp

filename2.cpp etc.

I want to get the output files like this :

filename1

filename2 etc.

EDIT :

Is there any way to save the timestamp of .cpp file to the output file .??

CodeIt
  • 3,492
  • 3
  • 26
  • 37

3 Answers3

3

If each file should have it's own executable and they're all in the same directory you can do this:

for i in *.cpp; do g++ $i -o `basename $i .cpp`; done

To add the timestamp:

for i in *.cpp; do g++ $i -std=c++11 -o `basename $i .cpp`-`date +%Y%m%d -r $i`; done

This will produce the date in YYYYMMDD format after the filename and hyphen

To change modification date:

for i in *.cpp; do g++ $i -std=c++11 -o `basename $i .cpp`; touch -t `date +%Y%m%d%H%M -r $i` `basename $i .cpp`; done
prajmus
  • 3,171
  • 3
  • 31
  • 41
0

You would do this with the "make" program, and a suitable makefile. That uses rules (some predefined), for transforming ".cpp" files into ".o" and executables. A quick check shows me that GNU make does have a default rule for .cpp to .o (long ago, it did not).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • For the given example, even without a makefile, you should be able to make filename1 (an executable) from filename1.cpp by typing "make filename1". To make everything, you would write a makefile listing the executables and naming the result (hard to describe in this space...) – Thomas Dickey Feb 05 '15 at 13:42
  • Take some hours to read more material, in particular the GNU make documentation (which starts with some tutorial ...) – Basile Starynkevitch Feb 05 '15 at 13:43
0

The GCC compiler is (internally) compiling one file at a time (but also has Link Time Optimization).

You want to use a builder like GNU make; adapt this example (or that one) to your needs.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547