无法从Windows批处理脚本构建C程序

时间:2017-11-04 15:01:13

标签: c windows batch-file cl

我想使用Windows批处理脚本构建C程序,但编译器会出现致命错误。

我有一台Windows 10计算机并使用Microsoft C / C ++编译器。

我正在运行的批处理脚本名为build.bat,内容为:

SET PROJECT_COMPILER=cl

SET HOME_DIRECTORY=%~dp0
SET PROJECT_SRC=%HOME_DIRECTORY%src\
SET PROJECT_BIN=%HOME_DIRECTORY%bin\
SET PROJECT_INCLUDE=%HOME_DIRECTORY%include\

%PROJECT_COMPILER% "%PROJECT_SRC%*.c" /I"%PROJECT_INCLUDE%" /link 
/out:"%PROJECT_BIN%out.exe"

del /f .\*.obj

我从第%PROJECT_COMPILER% "%PROJECT_SRC%*.c" /I"%PROJECT_INCLUDE%" /link /out:"%PROJECT_BIN%out.exe"行获得的输出是:

C:\Users\Andrea Nardi\Documents\C project\test_project>cl "C:\Users\Andrea Nardi\Documents\C project\test_project\src\*.c"         
/I"C:\Users\Andrea Nardi\Documents\C project\test_project\include\" /link 
/out:"C:\Users\Andrea Nardi\Documents\C project\test_project\bin\out.exe"
Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25507.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9024 : unrecognized source file type 
'Nardi\Documents\C', object file assumed
 cl : Command line warning D9024 : unrecognized source file type 
 'project\test_project\bin\out.exe', object file assumed
 main.c
Microsoft (R) Incremental Linker Version 14.11.25507.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:main.exe
main.obj
Nardi\Documents\C
project\test_project\bin\out.exe
LINK : fatal error LNK1181: cannot open input file 'Nardi\Documents\C.obj'

1 个答案:

答案 0 :(得分:0)

路径C:\Users\Andrea Nardi\Documents\C project\中的空格似乎会导致您的问题。

通常避免项目路径中的空格,由于空格是命令行参数分隔符,它将成为各种 gotchas 的原因。

使用相对路径也更简单,这样您就可以从任何地方构建项目,而不是非常特定的文件夹,以及避免使用空格。在这种情况下,例如:

SET PROJECT_COMPILER=cl

REM Set working directory to that of this batch file
pushd %~dp0

REM Set paths relative to batch file path
SET PROJECT_SRC=.\src
SET PROJECT_BIN=.\bin
SET PROJECT_INCLUDE=.\include

REM Build...
%PROJECT_COMPILER% "%PROJECT_SRC%\*.c" /I"%PROJECT_INCLUDE%" /link 
/out:"%PROJECT_BIN%\out.exe"

REM  Clean-up...
del /f .\*.obj

REM Restore original working directory
popd