从批处理文件调用并编译C代码? [英] Call and compile C code from a batch file?

查看:210
本文介绍了从批处理文件调用并编译C代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于汇编器中的一小段代码,对我来说很舒服 批处理文件中直接编译或在远程Windows计算机中编译.

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.

这是我设计用于MASM32的.bat文件

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%

我的问题:
例如,当尝试使用GCC时

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% 

第一个错误:
\ CodeBlocks \ MinGW \ bin \ gcc.exe这次是意外的.
我搜索并找到,然后我将其更改为:

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

解决它,但有一个新错误:
testc.bat:无法识别的文件: 无法识别文件格式 collect2.exe:错误:ld返回1退出状态

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

我的问题是:
是否可以以及如何使上述批处理文件与C/C ++编译器一起使用?

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

推荐答案

如果您愿意在文件系统中放置一个与原始类型之一匹配的附加no-nothing命令,则可以使用以下命令:

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.

您可以制作一个批处理文件,将C ++代码传递给编译器,但必须生成C ++代码.您不能创建同时具有有效批处理和有效c ++的文件.

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

根本原因是C ++没有任何命令来停止处理文件,并且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.

这意味着您可以在C ++和任何将#作为注释前缀的语言之间创建一个多身份文件. (然后,您只需在#if 0 ... #endif中包含要用其他语言处理的脚本的全部内容).但是批处理不是这种语言.

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.

这篇关于从批处理文件调用并编译C代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆