Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using gcc to compile C file
#10
The MinGW compiler installed with QB64PE is perfectly functional, so there is no need to waste several hundred megabytes of disk space on another MinGW version unless you need a specific compiler.

Here's a batch file I use that allows a user to compile C or C++ programs using QB64PE's MinGW.  It will invoke the appropriate compiler, GCC.EXE or G++.EXE, depending on the extension of your source file.

It can be used from the command line ("CCOMP.BAT hello.c"), but it can also be used drag-and-drop style: in your file manager, drag your C file onto the compiler .BAT file.

This batch file is designed to work from the QB64PE directory or from the MinGW subdirectory.



Code: (Select All)
@echo off
SETLOCAL ENABLEEXTENSIONS


REM v2024.04.21, JSR.
REM Public Domain.  Feel free to adapt it to your purposes.



REM About:
REM Designed to be run from within the main directory of any MinGW installation.
REM Can also be run from within QB64PE's directory, where it will find
REM and use the MinGW version that was installed along with QB64PE.
REM Also works with mstorsjo's CLang builds.
REM Not tested on other CLang builds.



::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Note: GCC options -m32 & -m64 don't work with MinGW compilers    ::
::       that use SEH or Dwarf2 exception handling.                 ::
::       These versions of MinGW can only create an executable with ::
::       the same word size as themselves.                          ::
::       (QB64PE currently uses an SEH-based compiler)              ::
::       To make use of -m32 & -m64 you need a compiler that uses   ::
::       SJLJ exception handling.                                   ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


REM The "set copts=" line allows you to set your preferred compiler options.


cls
if not "%~1"=="" goto lunch
echo.
echo Compile using QB64PE's included MinGW C/C++ compiler.
echo.
echo This batch script will invoke either gcc.exe (for .c source)
echo or g++.exe (for .cpp source) using preset compiler options.
echo It can first set any needed or desired environment variables.
echo.
echo If run from the command prompt,
echo additional options may be given by the user.
echo.
echo Usage: %~nx0 ^<filename^> [opt] [opt] [opt] [opt] [opt] ...
echo        (or drag your C/C++ source file onto %~nx0 in your file manager)
echo.
echo.
goto hawaii


:lunch
REM Auto-select C or C++ compiler....
set ccomp=gcc.exe
if /i "%~x1"==".cpp" set ccomp=g++.exe


REM Set copts to your preferred compiler options....
set copts=-Os -s -Wall -static
REM (Add -mwindows to copts to create a Windows GUI executable)


REM Let's figure out where the compiler is...
if not "%MGWDIR%"=="" goto work
REM ...start by assuming we're in the MinGW directory....
set MGWDIR=%~dp0
REM (MGWDIR is not needed by the compiler; it is for our use)
REM ...then check to see if we are really in the QB64PE directory....
if exist "%~dp0internal\c\c_compiler\" set MGWDIR=%~dp0internal\c\c_compiler


REM Set any needed or desired environment variables....
REM GCC does not really need PATH, but we'll set it anyway.
REM (Doing this makes it easier to adapt this script for other compilers which DO need PATH)
set PATH=%MGWDIR%\bin;%PATH%


:work
REM Let the compiler do its thing....
echo Compiling:
echo "%ccomp%" "%~1" -o "%~dpn1.exe" %copts% %2 %3 %4 %5 %6 %7 %8 %9
echo.
echo.
"%ccomp%" "%~1" -o "%~dpn1.exe" %copts% %2 %3 %4 %5 %6 %7 %8 %9
if exist "%~dpn1.exe" echo ---^>^  "%~dpn1.exe" created.
if not exist "%~dpn1.exe" echo ---^>^ "%~dpn1.exe" NOT created.


REM If your compiler leaves behind any extra files that you want automatically deleted, delete them here....
REM (the 3 examples below are commented out.  Feel free to change or replace them)
::del "%~dpn1.obj" 2>nul        :: Delete <YourProgramName.obj> file....
::del "%~dpn1.map" 2>nul        :: Delete <YourProgramName.map> file....
::del "%~dpn1.tds" 2>nul        :: Delete <YourProgramName.tds> file....
REM DEL will complain if its target file does not exist, so
REM the example lines above send DEL's error messages to NUL ("2>nul").
REM Doing this causes DEL to fail silently if the target files don't exist.


:hawaii
pause
Reply


Messages In This Thread
Using gcc to compile C file - by aurel - 04-18-2024, 07:31 PM
RE: Using gcc to compile C file - by a740g - 04-18-2024, 07:37 PM
RE: Using gcc to compile C file - by Kernelpanic - 04-18-2024, 09:18 PM
RE: Using gcc to compile C file - by aurel - 04-19-2024, 06:49 AM
RE: Using gcc to compile C file - by Kernelpanic - 04-19-2024, 08:16 AM
RE: Using gcc to compile C file - by aurel - 04-19-2024, 08:12 AM
RE: Using gcc to compile C file - by Kernelpanic - 04-19-2024, 07:06 PM
RE: Using gcc to compile C file - by aurel - 04-19-2024, 11:47 AM
RE: Using gcc to compile C file - by Kernelpanic - 04-19-2024, 02:00 PM
RE: Using gcc to compile C file - by JRace - 04-20-2024, 12:12 AM
RE: Using gcc to compile C file - by JRace - 04-20-2024, 12:31 AM
RE: Using gcc to compile C file - by bplus - 04-20-2024, 01:27 PM
RE: Using gcc to compile C file - by JRace - 04-20-2024, 03:35 PM
RE: Using gcc to compile C file - by Kernelpanic - 04-20-2024, 07:02 PM
RE: Using gcc to compile C file - by JRace - 04-20-2024, 08:06 PM
RE: Using gcc to compile C file - by Kernelpanic - 04-20-2024, 09:06 PM
RE: Using gcc to compile C file - by JRace - 04-20-2024, 10:11 PM
RE: Using gcc to compile C file - by Kernelpanic - 04-21-2024, 07:22 PM
RE: Using gcc to compile C file - by JRace - 04-21-2024, 07:59 PM
RE: Using gcc to compile C file - by TerryRitchie - 04-22-2024, 02:20 AM
RE: Using gcc to compile C file - by JRace - 04-22-2024, 03:22 PM
RE: Using gcc to compile C file - by aurel - 04-22-2024, 03:41 PM
RE: Using gcc to compile C file - by aurel - 04-22-2024, 03:48 PM



Users browsing this thread: 3 Guest(s)