在GCC中编译C文件时出错(多个文件在一个文件中) [英] Error when compiling C file in GCC (multiple files in one)

查看:308
本文介绍了在GCC中编译C文件时出错(多个文件在一个文件中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序证明了一串文本。这里是我的代码(主要是justify.c):

justify.c

  #include< string.h> 
#includeline.h
#includeword.h

#define MAX_WORD_LEN 20

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

clear_line(); (;;)
{
read_word(word,MAX_WORD_LEN + 1);

word_len = strlen(word);
if(word_len == 0)
{
flush_line();
返回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>
#includeline.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('\\\
');


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

word.c

  #include< stdio.h> 
#includeword.h

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

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


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

while(
while(ch!=''&& ch!= EOF)
{
if(pos 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 em>

  #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

当我在Code中编译它们时阻止它给我没有错误。但是,当我在GCC中做同样的事情时,我们也可以做到这一点。



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



我得到这个:

  justify.c:1:1:错误:预期的标识符或'('之前'<'令牌
<?xml version =1.0encoding-UTF-8 standalone =yes?>

我找不到错误,我一直在盯着这个好几个小时,请
我真的非常感谢任何帮助,我可以得到。 你试图编译Codeblocks项目文件应该是
,在项目目录中称为< project_name> .chp ,并且是一般形式的XML文件:

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

您的文章并未透露您是如何犯这个奇怪的错误的。这可能是因为你已经通过某种方式将 .cbp 文件的内容复制到 justify.c 中来破坏项目,或者将 .cbp 文件移动到 justify.c 或其他更隐晦的地方。



如果放在同一个目录中,您发布的五个文件将使用该命令进行编译和链接,而且没有错误
(尽管不是没有编译器警告)。

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

运行在该目录中。



只要确保源文件和头文件已保存并且具有运行时所期望的内容即可。使用Codeblocks之外的其他编辑器打开并检查它们。



当然,您应该使用 gcc ...- Wall进行编译。 。启用所有警告并修复发布的任何编译器警告,因为它们可能意味着错误。


The program justifies a string of text. Here is my code (main is justify.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

When I compile all of them in Code Blocks it gives me no errors. But when I do the same in GCC

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

I get this:

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

I cannot find the error and I'd been staring at this for hours. Please I'd really appreciate any help I could get.

解决方案

You are attempting to compile the Codeblocks project file, which should be called <project_name>.chp in the project directory and is an XML file of the general form:

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

Your post does not reveal how you made this strange mistake. It may be that you have broken the project by somehow copying the contents of the .cbp file into justify.c, or by moving the .cbp file to justify.c, or something more obscure.

The five files you have posted if put in the same directory will compile and link without errors (though not without compiler warnings) with the command.

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

run in that directory.

Just make sure that the source and header files are saved and have the contents that you expect when you run it. Open and inspect them beforehand with some other editor than Codeblocks.

As a matter of course you should compile with gcc ...-Wall... to enable all warnings and fix any compiler warnings that are issued, as they may mean bugs.

这篇关于在GCC中编译C文件时出错(多个文件在一个文件中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆