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 .??

3
  • Try this at the command line: for file in *.cpp; do g++ -o ${file%*.cpp} $file; done Commented Feb 5, 2015 at 13:42
  • @Galik I have posted your comment as an answer. Commented Feb 5, 2015 at 13:51
  • 1
    TBH @prajmus has basically the same answer as me, just a slightly different way of converting the file name. Commented Feb 5, 2015 at 13:53

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

5 Comments

You forgot the -Wall -Wextra -g flags to g++
@BasileStarynkevitch that's just an example
@prajmus Is there any way to save the timestamp of .cpp to the output files.
@prajmus .cpp last modified timestamp.
@I don't want the YYYYMMDD with the filename but rather when i use ls -ltr i should see file-name.cpp first, on the next line fine-name which is output of the file for each file.
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).

2 Comments

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...)
Take some hours to read more material, in particular the GNU make documentation (which starts with some tutorial ...)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.