gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:39:16 25 4
gpt4 key购买 nike

我想使用 Windows 批处理脚本构建 C 程序,但编译器出现 fatal error 。

我有一台 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'

最佳答案

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

一般来说,避免在项目路径中出现空格,由于空格是命令行参数分隔符,这将导致各种陷阱

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

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

关于无法从 Windows 批处理脚本构建 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47112064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com