4

我在 CentOS 5.7 系统上运行。

我从其他人那里下载了一个源包和一个 .spec 文件。我正在尝试使用以下香草命令从源代码构建 RPM:

% rpmbuild -ba ~/rpmbuild/SPECS/foo.spec
...
Configuration summary:
======================

  Host type................: x86_64-redhat-linux-gnu
  CC.......................: gcc
  CFLAGS...................: -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Werror

  Package..................: sudosh2
  Version..................: 1.0.4

  Installation prefix......: /usr
  Man directory............: /usr/share/man
  sysconfdir...............: /etc
  recording input..........: no

但是,此构建失败。代码有点草率,并产生了一些警告。这个工具链的某些部分是启用-Werror标志,这使得“所有警告都变成错误”。因此,构建失败并出现错误:

gcc -DHAVE_CONFIG_H -I. -I..     -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Werror -MT parse.o -MD -MP -MF .deps/parse.Tpo -c -o parse.o parse.c
cc1: warnings being treated as errors
sudosh.c: In function 'main':
sudosh.c:486: warning: unused variable 'written'
sudosh.c:112: warning: unused variable 'found'
cc1: warnings being treated as errors
parse.c: In function 'parse':
parse.c:20: warning: unused variable 'y'
parse.c:14: warning: unused variable 'opt'
parse.c:14: warning: unused variable 'cmt'
parse.c:14: warning: unused variable 'arg'
parse.c:10: warning: unused variable 'i'
parse.c:10: warning: unused variable 'line_number'
make[2]: *** [sudosh.o] Error 1

我知道正确的解决方法是让作者修复代码,但我想在短期内解决这个问题。我需要一个有效的 RPM。

它看起来像./configure或者autoconf正在自动添加-Werror标志。-Werror除了自己编辑 Makefile 之外,如何禁用构建的标志?

更新以响应@pwan 的回答:

.spec 文件非常通用,并且没有指定任何特殊标志:

%build
%configure \
    --program-prefix="%{?_program_prefix}"
%{__make} %{?_smp_mflags}
4

2 回答 2

1

除了自己编辑 Makefile 之外,如何为我的构建禁用 -Werror 标志?

GCC 手册在3.8 Options to Request or Suppress Warnings中有解决方案:

您可以使用以 -Wno-' 开头的选项来请求许多特定警告以-W', for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning关闭警告;例如,-Wno-隐式。本手册仅列出了两种形式中的一种,以非默认形式为准。

所以,设置-Wno-errorCFLAGS 环境变量对我有用。现在我知道我在寻找什么,我可以看到 Stackoverflow 有很多答案:https ://stackoverflow.com/search?q=%22-Wno-%22 ,特别是如何在以下情况下禁用特定警告 -墙已启用

在我的具体情况下,由于“警告:未使用的变量”,构建失败,我可以使用-Wno-unused-variable.

由于我使用的是 rpmbuild,因此我需要按照@pwan 的建议将其放入 .spec 文件中。我能解决这个问题的唯一方法是:

  1. 构建 rpm 一次:

    rpmbuild -v -bb ~/rpmbuild/SPECS/sudosh.spec
    
  2. 搜索由生成的 CFLAGS./configure

    Configuration summary:
    ======================
    
      Host type................: x86_64-redhat-linux-gnu
      CC.......................: gcc
      CFLAGS...................: -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Werror
    
  3. 并手动将这些 CFLAGS 附加到我的 .spec 文件的 `%configure% 部分:

    %configure \
        --program-prefix="%{?_program_prefix}"
    #%{__make} %{?_smp_mflags}
    %{__make} %{?_smp_mflags} CFLAGS="-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Werror -pedantic-errors -Wno-unused-variable -ansi -std=c99"
    

以上内容有点骇人听闻,因为如果上游更改了 ./configure 脚本,我可能需要重新调整 .spec 文件中的 CFLAGS。

于 2011-09-30T22:07:09.397 回答
1

没有足够的信息来给出详细的答案,但您可能应该关注规范文件中的 %prep 部分。

如果幸运的话,本节将调用配置。然后您应该能够查看配置脚本提供的参数并找出哪个参数负责 -Wall 标志。运行 'configure --help' 可能会列出可用的标志。

然后您应该能够更新规范文件,以便配置调用包含将跳过将 -Wall 添加到 CFLAGS 的标志

您也可以在配置调用期间设置 CFLAGS 环境变量,但配置脚本可能会忽略或覆盖这些。

CFLAGS="-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Werror" configure --whatever
于 2011-09-29T19:17:42.997 回答