简体   繁体   中英

Cannot build C program from Windows batch script

I want to build a C program using a Windows batch script, but the compiler gets a fatal error.

I have a Windows 10 computer and using Microsoft C/C++ Compiler.

the batch script that I am running is called build.bat and the content is:

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

and the output that I get from the line %PROJECT_COMPILER% "%PROJECT_SRC%*.c" /I"%PROJECT_INCLUDE%" /link /out:"%PROJECT_BIN%out.exe" is:

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'

It would appear that the spaces in the path C:\\Users\\Andrea Nardi\\Documents\\C project\\ is causing your problems.

In general avoid spaces in project paths, it will be the cause of all sorts of gotchas due to space being a command-line argument delimiter.

It is also far simpler to use relative paths so that you can build your project from anywhere rather then a very specific folder as well as avoid spaces. In this case something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM