0

For small pieces of code in assembler for me is very comfortable to put them in batch file and compile directly or in remote Windows machine.

This is my working .bat file designed to work with MASM32

;@goto end
.486
.model flat,stdcall
option casemap:none

include     \masm32\include\kernel32.inc  
include     \masm32\include\user32.inc
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\kernel32.lib

.data
      msg   db   "Hello Word",0
.code   
start:
  push 0
  push offset msg
  push offset msg
  push 0
  call MessageBox
  push 0
  call ExitProcess
end start

:end
@echo off
set name=%~n0
set cmpname=ml.exe
set linkname=link.exe
for %%X in (%cmpname%) do (set CMPL=%%~$PATH:X)
if defined CMPL ( 
  for %%I in (%linkname%) do (set MLINK=%%~$PATH:I)
    if defined MLINK (    
    %CMPL% /nologo /c /coff /Cp %name%.bat
    %MLINK% /SUBSYSTEM:WINDOWS /FIXED:NO %name%.obj
    del *.obj
    ) ELSE ( 
      ECHO Cannot find %linkname% !
   )
) ELSE ( 
  ECHO Cannot find %cmpname% !
)       
exit /b %ERRORLEVEL%

My problem:
for example, when trying to work with GCC

;@goto end
#include <stdio.h>
int  main()
{ 
   return 0;
}
:end
;@echo off

set name=%~n0
set cmpname=gcc.exe
for %%I in (%cmpname%) do (set CMPL=%%~$PATH:I)
if defined CMPL (    
%CMPL% -Wall -o %name% %name%.bat
del *.obj
    ) ELSE ( 
    echo Cannot find %cmpname% !
    )
exit /b %ERRORLEVEL% 

first error:
\CodeBlocks\MinGW\bin\gcc.exe was unexpected at this time.
I searched and found this , then I changed to the:

for %%I in (%cmpname%) do (set CMPL="%%~$PATH:I")

solve it, but have a new error:
testc.bat: file not recognized: File format not recognized collect2.exe: error: ld returned 1 exit status

My question is:
Is it possible and how to make above mentioned batch file to work with C/C++ compilers ?

Community
  • 1
  • 1
boleto
  • 1,149
  • 1
  • 22
  • 32
  • Sorry, in my previous answer I neglected one aspect of the C++ syntax. This can be made to work, sort of. – Ben Voigt Jul 06 '13 at 05:00

1 Answers1

3

If you are willing to place an additional do-nothing command in your filesystem which matches one of the primitive types, you can use something like:

int /*
;@echo off
set name=%~n0
set cmpname=gcc.exe
for %%I in (%cmpname%) do (set CMPL=%%~$PATH:I)
if defined CMPL (    
%CMPL% -Wall -o %name% %name%.bat
del *.obj
    ) ELSE ( 
    echo Cannot find %cmpname% !
    )
exit /b %ERRORLEVEL% 
*/ dummy;
#include <stdio.h>
int  main()
{ 
   return 0;
}

Earlier answer which didn't account for multi-line comments: You can't.

You can make a batch file that passes C++ code to the compiler, but it will have to generate the C++ code; you can't make a file that's both valid batch and valid c++.

The essential reason is that C++ does not have any command to stop processing a file, and C++ preprocessor commands cannot have any leading characters except for whitespace.

This means that you could create a multi-identity file between C++ and any language that treats # as a comment prefix. (Then you just include the entirety of the script to be processed in the other language within #if 0 ... #endif). But batch is not such a language.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720