gcc/clang различное поведение в linux и macOS для неявных объявлений функций : предупреждение или ошибка

#linux #macos #gcc #clang

Вопрос:

На Ubuntu 21.04 я получаю предупреждение:

 test.c: In function ‘foo’:
test.c:3:11: warning: implicit declaration of function ‘bar’ [-Wimplicit-function-declaration]
    3 |   int x = bar();
      |           ^~~
 

в то время как на macOS Big Sur 11.2 я получаю ошибку:

 test.c:3:11: error: implicit declaration of function 'bar' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  int x = bar();
          ^
1 error generated.
 

при компиляции этого исходного файла C (test.c)

 int foo()
{
  int x = bar();
  return x;
}
 

Я использую одни и те же параметры компилятора как в Linux, так и в macOS:

 $ gcc -c test.c -o test.o
 

Версии компилятора:

  • Линукс:
     $ gcc --version
    gcc (Ubuntu 10.3.0-1ubuntu1) 10.3.0
    Copyright (C) 2020 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     
  • macOS
     $ gcc --version
    Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c  /4.2.1
    Apple clang version 12.0.0 (clang-1200.0.32.29)
    Target: x86_64-apple-darwin20.3.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
     

Почему существует разница в поведении? Как я могу превратить ошибку macOS в предупреждение?

Комментарии:

1. Потому что есть -Werror, и нет.

2. @KamilCuk Спасибо, есть ли способ отключить его в командной строке для macOS?

3. Я думаю, что нашел, как его отключить: добавьте флаг компилятора -Wno-implicit-function-declaration

4. Или -Wno-error . (Но что касается того, почему в macos эта опция присутствует по умолчанию, я не знаю.)

5. Неявное объявление функции-это ошибка. Исправьте ошибку, не возитесь с флагами.