GCC Compiler: Compile Multiple files with different output file names - c++

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

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

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

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.

Related

how to chose between two implementation code in OCaml

I have uploaded the very small exemple project on a github repository
I have a school project. The specifications say that we can enable or disable a component.
I came from the c world in which we can compile c to object and chose the object to link. I try this in OCaml.
I have two source file with the same function but their effect is different.
I have two folders called "on" with implementation and "off" folder with an empty body
For the test, I have a simple a.ml and a.mli file that only print hello world and b.mli b.ml that call the module a. (so I have on/a.ml and off/a.ml)
I compile the on version and the off with this command:
ocamlc -c -I on on/a.mli on/a.ml -o on/a.cmo
then I try to link the C-way
ocamlc on/a.cmo b.ml -o on_b.exe
but I get the error
File "b.ml", line 1, characters 9-15:
Error: Unbound module A
I have then read I should specify the folder to search in with -I.
ocamlc -I on -I off on/a.cmo b.ml -o on_b.exe
I was happy because of that work for the on version
but it will not work for the off version
ocamlc -I on -I off off/a.cmo b.ml -o off_b.exe
I get the error
Error: Files b.cmo and off/a.cmo
make inconsistent assumptions over interface A
I have inspected with ocamlobjinfo it seems to build B its searches for the first module called A
In this example, I have only A and B but in future, I will build with some version on and some off... but don't do it manually
A solution found but no really efficient is to clean all .cmo and .cmi files ...
thanks for your reading and your time
EDIT:
I have also test with -open but it seems to work only for standards modules.
As you can see, compiling your .mli files produces a .cmi file. This is the file that you indicate is there by using the -I dir option.
So, if I understand you correctly, you have a a.mli in both your on and your off directory.
Now, when you refer to the module A in your code, the compiler looks for it in the current directory, then in the linked directories in the order they are given. When you compile "on", the right .cmi is found (because -I on comes before -I off in your command line). In the case of "off", the "on" cmi is found, and the compiler sees that it doesn't correspond to the right cmo.
Your two compiling lines should be:
ocamlc -I on on/a.cmo b.ml -o on_b.exe
ocamlc -I off off/a.cmo b.ml -o off_b.exe

compile folder of cpp files into GNU's g++ using Powershell commands

I'm looking for a way to pipe a full folders worth of c++ commands into g++ so that I don't have to type 25 file names into the g++ command.
I am using Powershell and thought that I could somehow use a pipeline with Get-childItem -Name -Path *.cpp. That way the whole folder could be compiled at once without the chance of me missing a file or misspelling a filename, and would be modular enough that I could just use Set-Location folderpathbefore pasting it wouldn't have to write a new makefile each time I make a linked program with a medium~large number of linked files.
Is this possible/practical or should I stick to using a makefile?
For example:
I have a elevator simulation 'borrowed' from a how to program book that has 12 header files, 12 classes saved into individual cpp files, and 1 main execution cpp in C:\Users\Noah\Desktop\ripsfromthebook\ch09\elevator.
let's assume that everything necessary for the program to work is present and if you typed in each cpp filename into a g++ command it would compile correctly into a working a.exe file.
They are named:
bell.cpp
bell.h
building.cpp
building.h
button.cpp
button.h
clock.cpp
clock.h
door.cpp
door.h
elevator.cpp
elevator.h
elevatorButton.cpp
elevatorButton.h
ElevatorSimulation.cpp
floor.cpp
floor.h
floorButton.cpp
floorButton.h
light.cpp
light.h
person.cpp
person.h
scheduler.cpp
scheduler.h
It would be great if I could use something similar to Get-ChildItem -Name -Path *.cpp | g++ -g to have all 13 ~.cpp file names be used as an input instead of having to write out this monster {see below} into the powershell commands.
Or using an array of strings to save the childItem output then run g++
$x = Get-ChildItem -name -path *.cpp
g++ -g $x -o main
would be better than this monster.
g++ -g bell.cpp building.cpp button.cpp clock.cpp door.cpp elevator.cpp elevatorButton.cpp ElevatorSimulation.cpp floor.cpp floorButton.cpp light.cpp person.cpp scheduler.cpp
short term solution I'm copy/pasting the output of Get-ChildItem into my makefile.
if your are using Cmakelists to build your library/exe you can use file() as below :
file(GLOB your_src
"relativePath/*.h"
"relativePath/*.cpp")
add_executable(your_exe ${your_src})
but it seems to be advised against
GLOB will generate a list of all files that match the globbing expressions and store it into the variable. Globbing expressions are similar to regular expressions, but much simpler. If RELATIVE flag is specified for an expression, the results will be returned as a relative path to the given path. (We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.)
source: https://cmake.org/cmake/help/v3.0/command/file.html#file

How to compile multiple unrelated .cpp files using one g++ command

I am checking some assignments and they have multiple .cpp files each with its own main function.
And have different file names.
I want one or a series of g++ commands that I can execute, which will give me a separate executable of each file.
How do I do that?
Since you are compiling using g++, I assume you have access to a shell:
for f in *.cpp; do g++ "$f" -o "`basename "$f"`"; done

SFML build all batch file

I was wondering if there was a way to alter the below code so it will build all the ".cpp" and ".h" files in a folder (+ subfolders) and specify one .exe name to be made.
#ECHO OFF
echo Building...
g++ -DSFML_STATIC main.cpp -lsfml-graphics-s -lsfml-window-s -lsfml-system-s
I know I can use CMake, but I want to know if it's possible with a batch script. If I can't do what I'm asking, can I at least specify a name for the .exe? It always comes out as "a.exe". I am using MingW.
Sorry, if this has been covered already. I searched and didn't find anything.
To specify the name for the output file, add option "-o name.exe".
To pass a large number of files to g++, you will have to use a response file like so:
#echo off
setlocal enabledelayedexpansion
for /r %%f in (*.cpp) do (
set a=%%f
set a=!a:\=/!
echo !a! >>files.tmp
)
endlocal
g++ -DSFML_STATIC #files.tmp -o bob.exe -lsfml-graphics-s -lsfml-window-s -lsfml-system-s
del files.tmp
Note that you should only give the *.cpp files to g++. It will find the .h files on its own while compiling.
I don't have a MingW setup so I wasn't able to test this completely, sorry. But the part about building the response file works at least.
Sources:
[http://www.mingw.org/wiki/MinGW_for_First_Time_Users_HOWTO]
[http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true]
[Multiple do commands in a for loop: Echoing a string to a file and then redirecting to the command window
"a.exe" is the default name that g++ uses for executables on Windows. If you want to specify your own name, add "-o foo.exe" to the g++ command line.

Extract all included files in cpp

I have .cpp file (text).I want to get the list of all files names which are included (#include) to this file.
What is the best way to do it?(Need to implement it in C++)
gcc -M source.cpp
Replace -M with -MM if you don't care about the system includes.
Assuming you have a "find" or "grep", something along these lines:
g++ -E source.cpp | grep '\# 1 '