【问题标题】:Is it possible to display compiler warnings in Arduino?是否可以在 Arduino 中显示编译器警告?
【发布时间】:2011-10-18 06:49:13
【问题描述】:

我希望 Arduino IDE 显示编译器警告,我宁愿不必在终端中编译一次以获取警告,然后再次通过 Arduino 生成 hex 文件。

在单击“验证”按钮的同时按住 shift 键会显示中间命令,但不会显示任何输出。如果不破解 Arduino IDE,这可能吗?

【问题讨论】:

  • o_O - 它不显示警告?!?!谷歌快速确认其他地方对此有投诉。如果您愿意破解 IDE,我找到了 this bug report and patch;它只涉及在两个地方将-w 更改为-Wall。我没有找到不涉及入侵 IDE 的解决方案。

标签: arduino


【解决方案1】:

默认的编译器命令行选项包括 -w,它禁止警告,但您可以通过编辑 C:\Program Files (x86)\Arduino\hardware\arduino\avr\platform.txt 来更改它。由于库可能包含警告,但我自己想要 -Werror,我删除了 -w 选项,然后添加到我的源代码中:

#pragma GCC diagnostic error "-Wall"
#pragma GCC diagnostic error "-Werror"

【讨论】:

  • 这很好用!然而,这本身也引入了一个 new 警告(通过一些较新版本的 GCC 的更改?): "warning: '-Werror' 不是控制警告的选项[-Wpragmas]。#pragma GCC 诊断错误“-Werror”。这在 GCC 版本 7.3.0(对应于 Arduino IDE 1.8.12)中观察到。这是必须手动忽略的噪音。到目前为止,我能找到的最接近的是 Compile Warning in GCC 6.3.0 #561。如果你找到了抑制警告的方法(不抑制重要警告),你可以添加它吗?
  • 哇!这要容易得多!而且,它中止编译,所以它永远不会忽略
  • 相关:-Werror causes compiler to stop on #warning. What can I do to prevent this? - “发出警告并避免来自 -Wpedantic 的这些额外消息/错误的正确方法是使用 #pragma 消息。”
【解决方案2】:

这里的大多数答案似乎已经过时了。从 Arduino.app 1.5 开始,您必须找到文件 preferences.txt (https://www.arduino.cc/en/Hacking/Preferences) 并将行 compiler.warning_level=none 更改为 compiler.warning_level=all 重要提示:首先退出 Arduino,编辑,然后再次启动 IDE。

【讨论】:

    【解决方案3】:

    使用 Arduino IDE 1.6.4 及更新版本,可以通过菜单文件首选项编译器警告:轻松调整警告级别。

    使用 Arduino AVR Boards,通过此选项设置的编译器标志为:

    • “无”:-w
    • “默认”:
    • “更多:-Wall
    • “全部”:-Wall-Wextra

    【讨论】:

      【解决方案4】:

      此功能已添加到最新的 Arduino 源代码中,但尚未发布(请参阅 Showing compilation warnings when verbose output is enabled)。

      计划包含在下一个主要的 Arduino IDE 版本(1.0 版)中,目前为 planned for release at the end of October 2011current release candidate 有此修复(截至 2011 年 10 月 25 日)。

      要在 Arduino IDE 中启用编译器警告,请打开菜单 FilePreferences,然后勾选 Show verbose output during: compilation 和/或在上传期间显示详细输出

      【讨论】:

      • 要让它检测到更多错误(例如,意外从函数调用中忽略() 到具有零参数的函数),更改选项 “编译器警告”“默认”“更多”“全部”
      【解决方案5】:

      described by Matthew 的功能不仅显示警告,还显示很多关于编译器如何调用的分散注意力的信息。

      请参阅我的问题的附录 Have the Arduino IDE set compiler warnings to error。它实现了对 Arduino 脚本的更改:

      -export PATH="${APPDIR}/java/bin:${PATH}"
      +export ORGPATH="${APPDIR}/java/bin:${PATH}"
      +export PATH="${APPDIR}/extra:${ORGPATH}"
      

      然后制作extra/avr-g++:

      #! /usr/bin/env python
      
      import os
      import sys
      import subprocess
      
      checklibpat = [
          'SoftwareSerial',
          'GSM_GPRS',
      ]
      
      werr = '-Werror'
      wall = '-Wall'
      
      cmd = ['avr-g++'] + sys.argv[1:]
      os.environ['PATH'] = os.environ['ORGPATH']
      fname = sys.argv[-2][:]
      extend = False
      warn = False
      if cmd[-2].startswith('/tmp'):
          extend = True
          warn = True
      if not extend:
          for l in checklibpat:
              if l in cmd[-2]:
                  warn = True
                  break
      if warn:
          #print len(fname), list(fname)
          for i, c in enumerate(cmd):
              if c == '-w':
                  cmd[i] = wall
                  break
          if extend:
              cmd.insert(1, werr)
      ## to enable deprecated stuff (Print.cpp) with gcc 4.7.0
      #cmd.insert(1, '-D__PROG_TYPES_COMPAT__=1')
      subprocess.call(cmd)
      

      如果您不希望编译器将源代码中的警告解释为错误,请注释掉 extend = True

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多