我正在尝试从我复制并粘贴的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);
这些是直接从网站复制的。我先把它们打出来,但当它不起作用时,我复制并粘贴它们。