使用 gcc 和终端编译

标签 c gcc terminal

我正在尝试从 http://cocoadevcentral.com/articles/000081.php 编译 C 文件我从字面上复制和粘贴,所以我不相信有任何类型的语法错误,但我不断收到错误。我不知道我做错了什么。我正在使用一个名为 TextWrangler 的文本编辑器来保存我的文字和文件。我下载了 Xcode 及其终端工具,因此这也不成问题。

错误如下:

testc4.c:4: error: expected identifier or ‘(’ before ‘)’ token
song.c:5: error: function definition declared ‘typedef’
song.c:5: error: return type is an incomplete type
song.c: In function ‘make_song’:
song.c:6: error: ‘Song’ undeclared (first use in this function)
song.c:6: error: (Each undeclared identifier is reported only once
song.c:6: error: for each function it appears in.)
song.c:6: error: expected ‘;’ before ‘newSong’
song.c:8: error: ‘newSong’ undeclared (first use in this function)
song.c:12: warning: ‘return’ with a value, in function returning void
song.c: At top level:
song.c:15: error: expected ‘)’ before ‘theSong’

这是我用来编译程序的命令:

gcc testc4.c song.c -o testc4

如果需要,我可以发布文件,但它们是直接从教程中复制的。我将我的测试文件命名为 testc4.c,而不是它想要的任何名称。

编辑:(我想你需要编码)

testc4.c文件:

#include <stdio.h>
#include "song.h"

main ()
{
  Song firstSong  = make_song (210, 2004);
  Song secondSong = make_song (256, 1992);

  Song thirdSong  = { 223, 1997 };
  display_song ( thirdSong );

  Song fourthSong = { 199, 2003 };
}

歌曲.c文件:

#include <stdio.h>
#include "song.h"

Song make_song (int seconds, int year)
{
  Song newSong;

  newSong.lengthInSeconds = seconds;
  newSong.yearRecorded    = year;
  display_song (newSong);

  return newSong;
}

void display_song (Song theSong)
{
  printf ("the song is %i seconds long ", theSong.lengthInSeconds);
  printf ("and was made in %i\n", theSong.yearRecorded);
}

歌曲.h文件

typedef struct {
  int lengthInSeconds;
  int yearRecorded;
} Song;

Song  make_song    (int seconds, int year);
void  display_song (Song theSong);

这些是直接从网站复制的。我先将它们打出来,但当它不起作用时,我复制并粘贴了它们。

最佳答案

可能你只需要改变:

main()

void main ()

关于使用 gcc 和终端编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18478630/

相关文章:

c - 为什么我们不能在一开始就定义指向 null 的结构指针

calloc 调用失败,代码崩溃

c++ - 将整数文字分配给指针?

gcc - 如何在 beagleboard/gumstix 上为 Linux 用户空间程序使用 __sync_fetch_and_add

c++ - 使用 Clang 而不是 GCC 构建(有问题的)示例时内存损坏

c - Linux cc 项目内目录中的库编译?

c - 加速循环执行 unsigned long long 模运算的性能

linux - fatal error : 'AWSHTTPSConnection' object has no attribute 'server_hostname'

linux - 从 Linux 终端命令行启动 Beyond Compare 4

c - session 中的现有进程将如何了解和获取 session 领导者刚刚获取的控制终端?