用gcc和终端编译

时间:2013-08-28 02:51:55

标签: 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 };
}

song.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);
}

song.h文件

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

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

这些是从网站直接复制的。我先输入它们,但是当它不起作用时我会复制并粘贴它们。

1 个答案:

答案 0 :(得分:0)

可能你只需要改变:

main ()

void main ()