2016-08-18 44 views
0

程序證明了一串文本。這裏是我的代碼(主要是justify.c):在GCC中編譯C文件時出錯(一個文件中有多個文件)

justify.c

#include <string.h> 
#include "line.h" 
#include "word.h" 

#define MAX_WORD_LEN 20 

int main(void) 
{ 
    char word[MAX_WORD_LEN+2]; 
    int word_len; 

    clear_line(); 
    for(;;) 
    { 
     read_word(word, MAX_WORD_LEN+1); 
     word_len = strlen(word); 
     if(word_len ==0) 
     { 
      flush_line(); 
      return 0; 
     } 
     if (word_len >MAX_WORD_LEN) 
      word[MAX_WORD_LEN]='*'; 
     if(word_len + 1 > space_remainding()) 
     { 
      write_line(); 
      clear_line(); 
     } 
     add_word(word); 
    } 
} 

line.c

#include <stdio.h> 
#include <string.h> 
#include "line.h" 

#define MAX_LINE_LEN 60 

char line[MAX_LINE_LEN+1]; 
int line_len=0; 
int num_words=0; 

void clear_line(void) 
{ 
    line[0]='\0'; 
    line_len=0; 
    num_words=0; 
} 

void add_word(const char *word) 
{ 
    if(num_words>0) 
    { 
     line[line_len]= ' '; 
     line[line_len+1]= '\0'; 
     line_len++; 
    } 
    strcat(line,word); 
    line_len += strlen(word); 
    num_words++; 
} 

int space_remainding(void) 
{ 
    return MAX_LINE_LEN - line_len; 
} 

void write_line(void) 
{ 
    int extra_spaces, spaces_to_insert, i,j; 

    extra_spaces= MAX_LINE_LEN - line_len; 
    for(i=0; i< line_len; i++) 
    { 
     if(line[i] != ' ') 
      putchar(line[i]); 
     else 
     { 
      spaces_to_insert = extra_spaces/ (num_words - 1); 
      for(j=1; j<=spaces_to_insert +1; j++) 
       putchar(' '); 
      extra_spaces -= spaces_to_insert; 
      num_words--; 
     } 
    } 

    putchar('\n'); 
} 

void flush_line(void) 
{ 
    if (line_len > 0) 
     puts(line); 
} 

word.c

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

int read_char(void) 
{ 
    int ch= getchar(); 

    if (ch=='\n' || ch == '\t') 
     return ' '; 
    return ch; 
} 

void read_word(char *word, int len) 
{ 
    int ch, pos=0; 

    while((ch=read_char()) == ' ') 
      ; 
    while(ch != ' ' && ch !=EOF) 
    { 
     if (pos<len) 
      word[pos++]=ch; 
     ch= read_char(); 
    } 
    word[pos]= '\0'; 
} 

line.h

#ifndef LINE_H 
#define LINE_H 

void clear_line(void); 

void add_word(const char *word); 

int space_remainding(void); 

void write_line(void); 

void flush_line(void); 

#endif // LINE_H 

word.h

#ifndef LINE_H 
#define LINE_H 
void clear_line(void); 

void add_word(const char *word); 

int space_remainding(void); 

void write_line(void); 

void flush_line(void); 

#endif // LINE_H 

當我編譯所有這些代碼塊中它給了我沒有錯誤。但是,當我做同樣的GCC

的gcc -o證明justify.c line.c word.c

我得到這個:

justify.c:1:1: error: expected identifier or '(' before '<' token 
<?xml version="1.0" encoding-"UTF-8" standalone="yes" ?> 

我不能發現錯誤,我一直盯着這幾個小時。請 我真的很感謝任何幫助,我可以得到。

+0

看來你的編譯器或'string.h'在你的操作系統中出了問題。嘗試'g ++'而不是'gcc',如果這不起作用,我會嘗試升級(重新安裝)gcc – VolAnd

回答

0

嘗試編譯這個

gcc -o justify justify.c line.c word.c -I.

1

您試圖編譯代碼塊的項目文件,這應該是 稱爲項目目錄<project_name>.chp,是一般形式的XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<CodeBlocks_project_file> 
    <FileVersion major="1" minor="6" /> 
    <Project> 
    ... 
    </Project> 
</CodeBlocks_project_file> 

你的文章沒有透露你是如何犯這個奇怪的錯誤的。可能是因爲你通過某種方式將.cbp文件的內容複製到justify.c,或者通過將.cbp文件移動到justify.c或其他更加模糊的內容來破壞項目。

如果放在同一個目錄下,您發佈的五個文件將會編譯並且無錯地鏈接 (儘管沒有編譯器警告)。

gcc -o justify justify.c line.c word.c 

在該目錄中運行。

只要確保源文件和頭文件已保存並且具有運行時所期望的內容即可。事先用Codeblocks之外的其他編輯器打開並檢查它們。

當然,您應該編譯gcc ...-Wall...以啓用所有警告並修復發佈的任何編譯器警告,因爲它們可能意味着錯誤。

+0

謝謝你是這個問題。我只是將所有東西都複製到記事本中,然後放在一個文件夾中,然後從那裏進行編譯,現在就可以運行。 – tadm123

+0

@ tadm123歡迎來到Stackoverflow。爲了表明答案可以解決您的問題,並且不需要更多答案,請在答案左側勾選複選標記。請參閱[如何接受答案](http://stackoverflow.com/help/accepted-answer)。 –