gcc "–Werror: No such file" referring to the target or the Werror usage?

gcc "–Werror: No such file" referring to the target or the Werror usage?

我正在努力学习这门课程 (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-s096-effective-programming-in-c-and-c-january-iap-2014/getting-started/),并 运行 解决一些编译问题。

建议使用 gcc、make 和下面的 Makefile。

我不知道我收到的错误消息是否与 C 源代码中的某些内容有关(似乎不太可能),或者与我配置 gcc 选项的方式有关。

$ gcc-8 -v
Using built-in specs.
COLLECT_GCC=gcc-8
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/8.1.0/libexec/gcc/x86_64-apple-darwin17.5.0/8.1.0/lto-wrapper
Target: x86_64-apple-darwin17.5.0
Configured with: ../configure --build=x86_64-apple-darwin17.5.0 --prefix=/usr/local/Cellar/gcc/8.1.0 --libdir=/usr/local/Cellar/gcc/8.1.0/lib/gcc/8 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-8 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-checking=release --with-pkgversion='Homebrew GCC 8.1.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-nls
Thread model: posix
gcc version 8.1.0 (Homebrew GCC 8.1.0)

$ gmake -v
GNU Make 4.2.1
Built for x86_64-apple-darwin17.0.0
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ cat Makefile
CC:=gcc-8
CFLAGS:=-O0 -g -std=c99 -Wall -Wextra -Wshadow -pedantic –Werror
CXXFLAGS:=-O0 -g -std=c++11 -Wall -Wextra -Wshadow -pedantic -Weffc++ -Werror

$ cat nothing.c
int main (void){
  return 0;
}

$ gmake nothing
gcc-8 -O0 -g -std=c99 -Wall -Wextra -Wshadow -pedantic –Werror    nothing.c   -o nothing
gcc-8: error: –Werror: No such file or directory
gmake: *** [<builtin>: nothing] Error 1

这里的问题是,在您的 makefile 中,破折号 before Werror 不是标准破折号,它是 unicode 破折号(代码 8211)

-pedantic –Werror

仔细看,长度略有不同。有时文字处理器或电子邮件客户端会出于一些美观原因切换破折号...

结果是选项解析器将其视为一个文件,而不是一个选项,并尝试打开它进行编译。

当您知道以下内容时,修复就很明显了:在 CFLAGS 中使用正确的破折号(CPPFLAGS 中的破折号没问题):

-pedantic -Werror