gpt4 book ai didi

c - 预处理 C 代码时出现未终止的无条件指令错误

转载 作者:行者123 更新时间:2023-12-01 13:38:53 25 4
gpt4 key购买 nike

我在编译 C 程序时在预处理步骤中遇到错误。

奇怪的是,我可以运行预处理而没有错误或警告:

gcc -I/usr/local/libpng-1.6.24/include -Wall -std=c99 lines.c -E -o lines

但是使用下面的表格给我错误:

cpp -I/usr/local/libpng-1.6.24/include -std=c99 lines.c -o lines

我假设 cpp 作为 gcc 的一部分运行,这使得它更奇怪。

有关信息,请参阅下面我收到的错误 - 只有其中一个,我有很多相同类型的错误:

In file included from lines.c:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdio.h:65:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/Availability.h:172:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/AvailabilityInternal.h:15284:10: error:
unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_11

不用说,出错的文件是苹果标准文件。我还必须说,让所有终止 #endif 匹配他们的 #if 指令对我来说似乎没问题

知道这里会发生什么吗?或者追踪错误的方法?

PS 顺便说一下,我在 OSX 中使用 Apple 提供的标准开发工具运行它。

--

添加附加信息作为对某些评论的回应:

lines.c源码https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-s096-introduction-to-c-and-c-january-iap-2013/final-project/starter-kit/lines.c并了解有关源代码使用位置的更多上下文,您可能希望在此处查看 https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-s096-introduction-to-c-and-c-january-iap-2013/final-project/starter-kit/

如果我在没有选项 -E 的情况下运行 gcc 实际上,由于我的文件夹结构,我会遇到链接问题,但下面的命令将正常工作 - 但我认为此信息与问题无关,因为我想比较like for like 选项 -E 在预处理后停止,cpp 只做预处理:

gcc -I/usr/local/libpng-1.6.24/include -L/usr/local/libpng-1.6.24/lib -Wall -std=c99 lines.c -lpng -o lines

--

我已将 Xcode 从 7.3 升级到 8.2.1,命令行工具从 Command_Line_Tools_OS_X_10.11_for_Xcode_7.3 升级到 Command_Line_Tools_macOS_10.12_for_Xcode_8.2,gcc 继续工作,cpp 仍然触发错误。但是这次错误发生了一点变化。见下文

In file included from lines.c:1:
In file included from /usr/include/stdio.h:65:
In file included from /usr/include/Availability.h:184:
/usr/include/AvailabilityInternal.h:20265:10: error: #else without #if
#else
/usr/include/AvailabilityInternal.h:20832:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_11_4

最佳答案

如评论中所述,我可以在装有 XCode 8 的 macOS Sierra 10.12.3 机器上重现您的问题,得到(至少)20 个错误,因此它在 20 日停止。

源文件:

#include <stdio.h>
int main(void) { puts("Hi"); return 0; }

命令行和错误输出:

$ /usr/bin/cpp -v min.c -o min.c.out >/dev/null
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name min.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 274.2 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0 -fdebug-compilation-dir /Users/jleffler/soq -ferror-limit 19 -fmessage-length 110 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -traditional-cpp -o - -x c min.c
clang -cc1 version 8.0.0 (clang-800.0.42.1) default target x86_64-apple-darwin16.4.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
In file included from min.c:1:
In file included from /usr/include/stdio.h:65:
In file included from /usr/include/Availability.h:184:
/usr/include/AvailabilityInternal.h:20265:10: error: #else without #if
#else
^
/usr/include/AvailabilityInternal.h:20832:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_11_4
^
/usr/include/AvailabilityInternal.h:20765:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_11_3
^
/usr/include/AvailabilityInternal.h:20702:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_11_2
^
/usr/include/AvailabilityInternal.h:20643:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_11
^
/usr/include/AvailabilityInternal.h:20588:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_10_3
^
/usr/include/AvailabilityInternal.h:20537:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_10_2
^
/usr/include/AvailabilityInternal.h:20490:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_10
^
/usr/include/AvailabilityInternal.h:20447:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_9
^
/usr/include/AvailabilityInternal.h:20408:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_8
^
/usr/include/AvailabilityInternal.h:20373:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_7
^
/usr/include/AvailabilityInternal.h:20342:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_6
^
/usr/include/AvailabilityInternal.h:20315:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_5
^
/usr/include/AvailabilityInternal.h:20292:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_4
^
/usr/include/AvailabilityInternal.h:20273:10: error: unterminated conditional directive
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_3
^
In file included from min.c:1:
In file included from /usr/include/stdio.h:65:
/usr/include/Availability.h:233:2: error: #else without #if
#else
^
/usr/include/Availability.h:236:2: error: #endif without #if
#endif
^
/usr/include/Availability.h:272:2: error: #endif without #if
#endif
^
/usr/include/Availability.h:299:2: error: #endif without #if
#endif
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
$

请注意,我使用的唯一选项是 -v(在开始时产生详细的输出)和 -o min.c.out(结束 -在冗长的输出中充满了 -o -;它按照它想要的方式而不是按照我告诉它的方式去做——排名不服从,真的)。因此,必须将标准输出重定向到 /dev/null 以避免看到所有输出。

如果我将 /usr/bin/cpp 替换为 /usr/bin/gcc (该路径对我来说是必需的,因为 gcc/opt/gcc/v6.2.0/bin/gcc,又名 GCC 6.2.0),我添加了 -E 选项,它工作正常:

$ /usr/bin/gcc -E -v min.c -o min.c.out
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name min.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 274.2 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0 -fdebug-compilation-dir /Users/jleffler/soq -ferror-limit 19 -fmessage-length 110 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o min.c.out -x c min.c
clang -cc1 version 8.0.0 (clang-800.0.42.1) default target x86_64-apple-darwin16.4.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
$ wc -l min.c.out
456 min.c.out
$

这强烈暗示了 /usr/bin/cpp 程序中的某种问题以及它驱动其余编译工具链的方式,但我还没有确定关键的区别在命令行中。

它还暗示了一个解决方案——不要单独使用 cpp。正如我在评论中指出的那样,2017 年 1 月有一个问题 — How to get XCode 8 C preprocessor to ignore // comments in #defines? 也指出了独立 cpp 中的一个问题。

如果您必须有可用的 cpp,请安装您自己的。例如,您可以在某个位置(例如 /opt/gcc/v6.0)安装 GCC 6.3.0(我在这台机器上很懒;我的另一台机器已经在运行 6.3.0,而不是 6.2.0)。 3.0/bin/cpp),然后在键入 cpp 时安排运行它而不是 /usr/bin/cpp — 调整 PATH ,或将符号链接(symbolic link)或脚本放在 /usr/local/bin$HOME/bin 等目录中,该目录出现在 PATH/usr/bin 之前,或在 makefile 中指定正确的 PATH,或任何需要的内容。

如果您需要有关在 Mac 上构建 GCC 的指南,请考虑:

关于c - 预处理 C 代码时出现未终止的无条件指令错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42031921/

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