问题
in my main.h
(which is included in all other src file), I have:
char* buffer;
This compiles and workes fine.
For some other reason, I tried to initialize buffer
, both as
char* buffer="";
and
char* buffer="\0";
Now, building it is giving error:
src/search.o:(.data+0x0): multiple definition of `buffer'
src/bib.o:(.data+0x0): first defined here
src/mkbib.o:(.data+0x0): multiple definition of `buffer'
src/bib.o:(.data+0x0): first defined here
src/update_file.o:(.data+0x0): multiple definition of `buffer'
src/bib.o:(.data+0x0): first defined here
src/view.o:(.data+0x0): multiple definition of `buffer'
src/bib.o:(.data+0x0): first defined here
src/file_util.o:(.data+0x0): multiple definition of `buffer'
src/bib.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [mkbib] Error 1
The problem is bib.o is generated from a bison file (bison -d
). buffer is NOT defined there:
$ grep buffer src/bib.y
$
buffer is also never defined ANYWHERE else, but used:
$grep buffer src/*.c
src/file_util.c: 44 : g_file_set_contents(filename, buffer, -1, &Err);
src/file_util.c: 51 :// g_free(buffer);
src/file_util.c: 75 : g_file_set_contents(filename, buffer, -1, &Err);
src/file_util.c: 76 :// g_free(buffer);
src/file_util.c: 140 : g_file_get_contents(filename, &buffer, &length , &error);
src/file_util.c: 152 :// g_free(buffer);
src/search.c: 99 : GtkTextBuffer *gs_buf=gtk_text_view_get_buffer(GTK_TEXT_VIEW(gs_txt));
src/search.c: 100 : gtk_text_buffer_get_start_iter (gs_buf, &start);
src/search.c: 101 : gtk_text_buffer_get_end_iter (gs_buf, &end);
src/search.c: 102 : gchar *gs_text = gtk_text_buffer_get_text (gs_buf, &start, &end, FALSE);
src/search.c: 103 : strcat(buffer, gs_text);
src/update_file.c: 106 : GString *str=g_string_new(buffer);
src/update_file.c: 132 : buffer=str->str;
src/view.c: 38: yyin=fmemopen(buffer,strlen(buffer),"r");
src/view.c: 178: // g_free(buffer);
src/view.c: 259: buffer=str->str;
$
NB: I can also recall having same type of error adding another character variable in main.h few days back, left the problem that time.
回答1:
Use:
extern char *buffer; // declares buffer, but doesn't define it
in your main.h
and be sure your definition of buffer
appears only once in one .c
file as:
char *buffer = ""; // not equivalent to = "\0"
来源:https://stackoverflow.com/questions/17054979/program-givin-error-when-gchar-is-initialized