问题
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.
回答1:
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.
回答2:
Try to compile with this
gcc -o justify justify.c line.c word.c -I.
来源:https://stackoverflow.com/questions/39009571/error-when-compiling-c-file-in-gcc-multiple-files-in-one