error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token in C

空扰寡人 提交于 2019-12-11 08:28:51

问题


I have the following declarations

FILE *fptr; FILE *optr;

in algo.h I have the main in main.c which opens these files. I get the above error if I put the declarations in the header file. If I put it in the main.c, then I get mutiple definition errors like

src\main.o:main.c:(.bss+0xc88): multiple definition of rcount' src\new_algo.o:new_algo.c:(.bss+0xc88): first defined here src\main.o:main.c:(.bss+0xc8c): multiple definition ofcondi' src\new_algo.o:new_algo.c:(.bss+0xc8c): first defined here


回答1:


Kinda sounds like you (1) haven't included <stdio.h> where you're using FILE, and/or (2) have some non-static executable code or non-extern variable definitions in your headers (and/or are #includeing a C file).

The first would typically cause FILE not to be defined (or to be typedef'd to a type that doesn't exist, in some cases). The second would cause stuff to be defined in each translation unit that includes the file, which would confuse the linker.

To fix: (1) #include <stdio.h> in the file where FILE is used, and (2) move shared definitions from the headers into a .c file (and/or declare them as static or extern as appropriate), and only ever #include .h files.




回答2:


What you have in algo.h is a definition not a declaration. If you have FILE *fptr; FILE *optr; in both the source and the header file then you are declaring the variables twice.

You need:

algo.h

extern FILE *fptr; 
extern FILE *optr;

algo.c

FILE *fptr; 
FILE *optr;



回答3:


Sounds like you're not including stdio. Add

#include <stdio.h>

in your header file above those declarations.




回答4:


The linker errors have nothing to do with the FILE variables you posted.

There are two variables your source, named rcount and condi, that according to the linker is defined in both your source files. The reason is, I guess, that you define those variables in a header file that is included in both source files. Some old compilers still can't handle that.



来源:https://stackoverflow.com/questions/5575700/error-expected-asm-or-attribute-before-token-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!