简体   繁体   English

用gcc和终端编译

[英]Compiling with gcc and terminal

I am attempting to compile C files from http://cocoadevcentral.com/articles/000081.php that I literally copy and pasted, so I do not believe there is any type of syntax error, yet I keep getting errors. 我试图从http://cocoadevcentral.com/articles/000081.php编译并逐字粘贴的C文件,因此我不相信有任何类型的语法错误,但我一直在出错。 I do not know what I am doing wrong. 我不知道我在做什么错。 I'm using a text editor called TextWrangler to save my write and save my files. 我正在使用一个名为TextWrangler的文本编辑器来保存我的写和保存我的文件。 I have Xcode downloaded along with its terminal tools so that shouldn't be an issue either. 我已经下载了Xcode及其终端工具,因此也不应该成为问题。

Here are the errors: 错误如下:

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’

Here is the command I'm using to compile my program: 这是我用来编译程序的命令:

gcc testc4.c song.c -o testc4

If needed I can post the files but they are copied right from the tutorial. 如果需要,我可以发布文件,但是可以直接从教程中复制它们。 I named my test file testc4.c instead of whatever it wanted. 我将测试文件命名为testc4.c而不是它想要的名称。

Edit: (I figured you'd need to code) 编辑:(我认为您需要编码)

testc4.c file: 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 file: 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 file song.h文件

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

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

These are straight copied from the site. 这些是从站点直接复制的。 I typed them out first but when it didn't work I copy and pasted them. 我先把它们打出来,但是当它不起作用时,我复制并粘贴了它们。

Probably you just have to change: 可能您只需要更改:

main ()

to

void main ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM