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.
Related
I was lately using SDL and now i want to put the .exe file in a specific directory so it looks cleaner, somebody knows how to do that?
When compiling with g++, gcc, or many other similar compilers, you specify the output file with the -o flag.
For example, to set the output file as foo.exe in the parent directory, you would call g++ like this:
g++ [other options/source files here] -o ../foo.exe
I'm trying to understand the following line in a Makefile.in file:
CXXFLAGS += -O3 -DNDEBUG -std=c++11 -Wno-deprecated-declarations -Isrc -I #mathinc#
I know the -I flag adds a directory to the list of places where the compiler will search for included files but what does #mathinc# mean?
Note that the file is called Makefile.in -- this signifies that it is input to another file (or transformation).
In short, configure will run and determine, say, where the relevant include files are for #mathinc -- likely some math headers. After you run configure it will produce Makefile (no trailing .in) based on what it finds. Do inspect that file.
configure scripts are created in a system called autoconf which, like all build systems, has its fans and its haters. There are some decent tutorials as for example this one.
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
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.
I just recently switched back to Linux from windows and VC, but I never done any special coding using g++ compiler.
Currently my libraries (boost and others) are scattered all over the hard drive and I need to learn how to setup my compiler and linker so that all the compiler settings..
(includes, libs, flags) etc.. will be held in one single file or place, so that it becomes easy to manage, because I don't want to type these things every time I launch the compiler on command line.
Also note that I'm using a vim as my code editor and do not want to use IDE.
What is the best way to achieve that goal?
You need to use some of Building tools. It's allow you type small command (in vim you need just type :make) which launch build process with predetermined parameters (includes, libs, etc).
For C++ in Linux the most common tools are:
- make;
- automake;
- CMake.
If you use Qt also qmake is available.
I've had experience with all of them and my suggestion is use plain make for small projects and CMake for others and don't use autotools while you don't have to do it.
Note: All hight-level tools just help generate appropriate files (Makefile) for plain make (CMake generate Makefile based on CMakeLists.txt, automake based on Makefile.am, qmake based on *.pro).
because I don't want to type these things every time I launch the
compiler on command line.
I don't like to type either. All I want to do for small builds is issue:
(1) a short alias (2) the name of the file to compile, and (3) an output file.
Then I want my tool to take care of all common options, and if necessary, include the paths to any extra -I include directories, -L library directories and form the command line for me.
I have a short script that can handle the drudgery. Separating your projects into separate directories and including a 'bldflags' file with specific options allows the scripts to load any project specific options you may require. It is flexible enough to take any additional options specified on the command line. Alias the script in your .bashrc, and all that is required for quick builds is:
g+ filename.cpp outname
Now this is a very basic script and is not intented to replace proper build tools for your projects, but for quick compilations, it, or something like it, will sure cut down on the typing required. Here is the short script:
#!/bin/bash
## validate input
test -n "$1" && test -n "$2"|| { echo "insufficient input. usage: ${0//*\//} source.cpp out [options]"; exit 1; }
## set standard build flags and test if exists/source ./bldflags
stdclfags="-Wall" # add any standard flags you use.
test -r ./bldflags && bldflags="`<./bldflags`"
## show build command and call g++
echo -e "building $1 with:\n g++ $stdclfags -o $2 $1 $bldflags ${#:3}"
g++ $stdclfags -o "$2" "$1" $bldflags ${#:3}
exit 0
Make the script executable and include a simple alias in your .bashrc giving it any name you like:
alias g+='/home/david/scr/utl/bgc++.sh'
Examples of basic use: (basic without additional flags or a ./bldflags file)
$ g+ input.cpp output
building input.cpp with:
g++ -Wall -o output input.cpp
With a few extra options added on the command line:
$ g+ input.cpp output -Wunused -fno-default-inline
building input.cpp with:
g++ -Wall -o output input.cpp -Wunused -fno-default-inline
Including project specific options in ./bldflags (e.g: -I/home/david/inc -L/home/david/lib -Wl,-rpath=/home/david/lib
g+ input.cpp output -Wunused -fno-default-inline
building input.cpp with:
g++ -Wall -o output input.cpp -I/home/david/inc -L/home/david/lib -Wl,-rpath=/home/david/lib -Wunused -fno-default-inline
So to address the I don't want to type these things every time I launch the
compiler on command line, this is a very quick and easy way I've found to cut the typing down to a minimum for quick/repetitive builds where a full Makefile isn't needed.