c - 为什么 gcc99 指向无意义的错误而标准 gcc 没有?

标签 c gcc c99

我正在通过 this article 学习 ALSA 编程我尝试编译以下示例:

/*

This example opens the default PCM device, sets
some parameters, and then displays the value
of most of the hardware parameters. It does not
perform any sound playback or recording.

*/

/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_API

/* All of the ALSA library API is defined
 * in this header */
#include <alsa/asoundlib.h>

int main() {
  int rc;
  snd_pcm_t *handle;
  snd_pcm_hw_params_t *params;
  unsigned int val, val2;
  int dir;
  snd_pcm_uframes_t frames;

  /* Open PCM device for playback. */
  rc = snd_pcm_open(&handle, "default",
                    SND_PCM_STREAM_PLAYBACK, 0);
  if (rc < 0) {
    fprintf(stderr,
            "unable to open pcm device: %s\n",
            snd_strerror(rc));
    exit(1);
  }

  /* Allocate a hardware parameters object. */
  snd_pcm_hw_params_alloca(&params);

  /* Fill it in with default values. */
  snd_pcm_hw_params_any(handle, params);

  /* Set the desired hardware parameters. */

  /* Interleaved mode */
  snd_pcm_hw_params_set_access(handle, params,
                      SND_PCM_ACCESS_RW_INTERLEAVED);

  /* Signed 16-bit little-endian format */
  snd_pcm_hw_params_set_format(handle, params,
                              SND_PCM_FORMAT_S16_LE);

  /* Two channels (stereo) */
  snd_pcm_hw_params_set_channels(handle, params, 2);

  /* 44100 bits/second sampling rate (CD quality) */
  val = 44100;
  snd_pcm_hw_params_set_rate_near(handle,
                                 params, &val, &dir);

  /* Write the parameters to the driver */
  rc = snd_pcm_hw_params(handle, params);
  if (rc < 0) {
    fprintf(stderr,
            "unable to set hw parameters: %s\n",
            snd_strerror(rc));
    exit(1);
  }

  /* Display information about the PCM interface */

  printf("PCM handle name = '%s'\n",
         snd_pcm_name(handle));

  printf("PCM state = %s\n",
         snd_pcm_state_name(snd_pcm_state(handle)));

  snd_pcm_hw_params_get_access(params,
                          (snd_pcm_access_t *) &val);
  printf("access type = %s\n",
         snd_pcm_access_name((snd_pcm_access_t)val));

  snd_pcm_hw_params_get_format(params, (snd_pcm_format_t *) &val);
  printf("format = '%s' (%s)\n",
    snd_pcm_format_name((snd_pcm_format_t)val),
    snd_pcm_format_description(
                             (snd_pcm_format_t)val));

  snd_pcm_hw_params_get_subformat(params,
                        (snd_pcm_subformat_t *)&val);
  printf("subformat = '%s' (%s)\n",
    snd_pcm_subformat_name((snd_pcm_subformat_t)val),
  snd_pcm_subformat_description(
                        (snd_pcm_subformat_t)val));

  snd_pcm_hw_params_get_channels(params, &val);
  printf("channels = %d\n", val);

  snd_pcm_hw_params_get_rate(params, &val, &dir);
  printf("rate = %d bps\n", val);

  snd_pcm_hw_params_get_period_time(params,
                                    &val, &dir);
  printf("period time = %d us\n", val);

  snd_pcm_hw_params_get_period_size(params,
                                    &frames, &dir);
  printf("period size = %d frames\n", (int)frames);

  snd_pcm_hw_params_get_buffer_time(params,
                                    &val, &dir);
  printf("buffer time = %d us\n", val);

  snd_pcm_hw_params_get_buffer_size(params,
                         (snd_pcm_uframes_t *) &val);
  printf("buffer size = %d frames\n", val);

  snd_pcm_hw_params_get_periods(params, &val, &dir);
  printf("periods per buffer = %d frames\n", val);

  snd_pcm_hw_params_get_rate_numden(params,
                                &val, &val2);
  printf("exact rate = %d/%d bps\n", val, val2);

  val = snd_pcm_hw_params_get_sbits(params);
  printf("significant bits = %d\n", val);

  snd_pcm_hw_params_get_tick_time(params,
                                  &val, &dir);
  printf("tick time = %d us\n", val);

  val = snd_pcm_hw_params_is_batch(params);
  printf("is batch = %d\n", val);

  val = snd_pcm_hw_params_is_block_transfer(params);
  printf("is block transfer = %d\n", val);

  val = snd_pcm_hw_params_is_double(params);
  printf("is double = %d\n", val);

  val = snd_pcm_hw_params_is_half_duplex(params);
  printf("is half duplex = %d\n", val);

  val = snd_pcm_hw_params_is_joint_duplex(params);
  printf("is joint duplex = %d\n", val);

  val = snd_pcm_hw_params_can_overrange(params);
  printf("can overrange = %d\n", val);

  val = snd_pcm_hw_params_can_mmap_sample_resolution(params);
  printf("can mmap = %d\n", val);

  val = snd_pcm_hw_params_can_pause(params);
  printf("can pause = %d\n", val);

  val = snd_pcm_hw_params_can_resume(params);
  printf("can resume = %d\n", val);

  val = snd_pcm_hw_params_can_sync_start(params);
  printf("can sync start = %d\n", val);

  snd_pcm_close(handle);

  return 0;
}

问题是如果我编译它

gcc test.c -std=c99 -lasound

我得到以下信息:

test.c: In function ‘main’:
test.c:36:3: warning: implicit declaration of function ‘alloca’ [-Wimplicit-function-declaration]
   snd_pcm_hw_params_alloca(&params);
   ^

但是如果我不使用就不会出现这个警告

-std=c99

  1. 为什么我什至会收到此警告?
  2. 为什么标准C版没有这个问题?

最佳答案

根据 man alloca 你应该添加 alloca.h当你使用 alloca() 时添加到你的包含中:

 #include <alloca.h>

这不是通常的函数,因为它应该直接修改调用它的函数的堆栈。它在编译器中专门处理,例如在 gcc 中(如描述 in the man )

Notes on the GNU version

  Normally, gcc(1) translates calls to alloca() with inlined code.
   This is not done when either the -ansi, -std=c89, -std=c99, or the
   -std=c11 option is given and the header <alloca.h> is not included.
   Otherwise, (without an -ansi or -std=c* option) the glibc version of
   <stdlib.h> includes <alloca.h> and that contains the lines...

您正在使用 alloca因为snd_pcm_info_alloca被定义为带有 alloca 的宏(谢谢你,马特麦克纳布)

#define snd_pcm_info_alloca(ptr) do { assert(ptr); *ptr = (snd_pcm_info_t *) alloca(snd_pcm_info_sizeof()); memset(*ptr, 0, snd_pcm_info_sizeof()); } while (0)

不完整ALSA doc about snd_pcm_info_alloca

snd_pcm_hw_params_alloca(ptr) allocate an invalid snd_pcm_hw_params_t using standard alloca

但是 alloca 不是标准的;当你使用它时,你应该包括 alloca.h

我认为 http://www.linuxjournal.com/article/6735?page=0,1 中的那个例子是不正确的;和很好的例子,比如 http://webpages.lss.supelec.fr/perso/kowalski/?download=playback.chttps://gitorious.org/alsa/alsa-tools/source/f768eb0ff4124ac7e795cee09acb797fd01b63f1:ac3dec/output.c#include <alloca.h> .

snd_pcm_hw_params_alloca宏需谨慎使用,不正确的使用可能会导致使用后免费报错:http://mailman.alsa-project.org/pipermail/alsa-devel/2008-March/006700.html

关于c - 为什么 gcc99 指向无意义的错误而标准 gcc 没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28932305/

相关文章:

c++ - collect2.exe : error: ld returned 1 exit status

c - 递归程序中编译器的优化

c++ - 如何将 GCC 的 printf 格式属性与 C++11 可变参数模板一起使用?

c - 是否可以将第三种#include 指令中的 "glue"预处理器标记放入双引号中?

c - OpenCl 中的结构具有大量内存地址

c - 为什么在C99中SCHAR_MIN定义为-127?

c++ - C++ 中嵌套 C 结构的 offsetof()

c - 如果两个任务尝试同时访问结构的不同部分(在 c 中)会发生什么?

c - 四舍五入以避免在随后的求和中四舍五入

计算文件中的段落数