Hack Forums
[Shell] My simple auto-compiler - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] My simple auto-compiler (/showthread.php?tid=3901198)

Pages: Pages 1 2


My simple auto-compiler - Ombra - Dec 1, 2013

I recently started learning C, and i have a separate folder where i keep my C sources. Each time that i end to work on one of these, i have to compile it with GCC by typing in cmd the source path preceded by the function "gcc".

This is really boring each time, so i made this simple .bat file to auto compile my current project.

Code
@echo off
title Batch Auto Compiler
color 0a
echo Starting.
gcc [Path] -o [Name]
echo Compiled!
exit

You can easily understand that [Path] should be changed with source's path, while [Name] is the name that compiled file 'll have.

I know, it's really basic but hey, it does his job 🙂
In particular i suggest using a fixed .C as desk, so you'll have not to change the path each time.

P.S. if an error occur just add "pause" between "echo Compiled!" and "exit", so you 'll see the error reported by GCC


* - Nakamoto. - Dec 1, 2013

I made one of these that uses minGW to compile but it was for c++


* - webdev77 - Dec 1, 2013

And for Visual Studio Command Prompt (C/C++):

Code
(VS command prompt) cl source.cpp



* - Greenp0ison - Dec 2, 2013

Code
@echo off
title Batch Auto Compiler
set f=%~1
if "%f%"=="" ( echo Drag/Drop a file onto this to compile it &pause &exit )
echo Starting.
set /p n=Output name:
gcc "%f%" -o "%n%"
echo Compiled!
exit

Now you can drag/drop a file onto the batch file and it compiles it after you enter an output name.


* - Ombra - Dec 2, 2013

I know that it's a simple function, buti think that 'll be useful :)


* - White Light - Dec 2, 2013

(Dec 2, 2013 01:39 AM)Greenp0ison Wrote:  Now you can drag/drop a file onto the batch file and it compiles it after you enter an output name.

I'd prefer to automate it whenever possible, so let's ditch the asking for a name part:

Code
@echo off
title Batch Auto Compiler
set f=%~1
if "%f%"=="" ( echo Drag/Drop a file onto this to compile it &pause &exit )
echo Starting.
set n=%~n1.exe
gcc "%f%" -o "%n%"
echo Compiled!
exit



* - Ombra - Dec 2, 2013

Well, thank you for improving my code :)
I have not a great knoweledge about batch, i'm sure you already understood it ;)


* - Greenp0ison - Dec 2, 2013

(Dec 2, 2013 02:33 AM)White Light Wrote:  I'd prefer to automate it whenever possible, so let's ditch the asking for a name part:

Code
@echo off
title Batch Auto Compiler
set f=%~1
if "%f%"=="" ( echo Drag/Drop a file onto this to compile it &pause &exit )
echo Starting.
set n=%~n1.exe
gcc "%f%" -o "%n%"
echo Compiled!
exit

Let's scrap the F/N variables as well and just remove the unnecessary stuff:
Code
@if "%~1"=="" ( @echo Drag/Drop a file onto this to compile it &@pause &@exit /b )
@gcc "%~1" -o "%~n1.exe"
@exit /b

Woo.


* - iDyxl - Dec 2, 2013

I'll just be here, and look like i understand everything that's going on..


* - Greenp0ison - Dec 2, 2013

(Dec 2, 2013 04:39 AM)iDyxl Wrote:  I'll just be here, and look like i understand everything that's going on..

A brief summary of what should be realised by the code improvements:

1. @ECHO OFF is a convenience, and can be replaced by prefixing every command (including in-line commands w/ '&') with the "@" symbol.

2. Make it pretty, cut the crap code like unnecessary ECHO commands and pauses.

3. The EXIT command exits, no matter what. If you call the program from the command line, it quits the command line as well as the program itself. Use EXIT /B to end termination of just the program.

4. Automation is preferred. Batch is intended for automation, why not go with that?


* - Ombra - Dec 2, 2013

(Dec 2, 2013 04:32 AM)Greenp0ison Wrote:  Let's scrap the F/N variables as well and just remove the unnecessary stuff:
Code
@if "%~1"=="" ( @echo Drag/Drop a file onto this to compile it &@pause &@exit /b )
@gcc "%~1" -o "%~n1.exe"
@exit /b

This is very advanced to me :/
I would like to improve my batch knoweldge..


* - Greenp0ison - Dec 2, 2013

(Dec 2, 2013 04:58 AM)KingOfHunt3rs Wrote:  This is very advanced to me :/
I would like to improve my batch knoweldge..

Okay, the @ prefix to every command does the same as @ECHO OFF, just without the actual line of @ECHO OFF. The %~1/%~n1 stuff is just command line args, I'm pretty sure White Light has a tutorial for that.


* - Han. - Dec 2, 2013

Nice, but this is what Make is for 😝


* - Nakamoto. - Dec 2, 2013

(Dec 2, 2013 06:41 AM)Greenp0ison Wrote:  Okay, the @ prefix to every command does the same as @ECHO OFF, just without the actual line of @ECHO OFF. The %~1/%~n1 stuff is just command line args, I'm pretty sure White Light has a tutorial for that.

Seriously what does White Light not have a tutorial for. He needs to have his stickied.


* - White Light - Dec 2, 2013

(Dec 2, 2013 06:41 AM)Greenp0ison Wrote:  Okay, the @ prefix to every command does the same as @ECHO OFF, just without the actual line of @ECHO OFF. The %~1/%~n1 stuff is just command line args, I'm pretty sure White Light has a tutorial for that.

https://hackforums.net/showthread.php?tid=592361