i have defined some extern variables at a header file named variables.h like so :
#ifndef VARIABLES_H
#define VARIABLES_H
extern int var1;
extern int var2;
#endif
Then i add it to my source files.
The compiler warns me the following:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘var1’
That goes on for every variable and ends at the final variable.
What is the problem?
The error appears at variables.h for every variable.
file.h :
#ifndef FILE_H
#define FILE_H
void do_sth(void);
void do_sth_else(void);
#endif
file.c :
#include "variables.h"
/* Quit */
void do_sth(void) {
/* do sth */
}
void do_sth_else(void) {
/* do sth else */
}
thats all.
The error is:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘var1’
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘var2’
One obvious issue with the headers that you posted is that they are declaring variables of types that may not be in scope. For example, you declare
extern GtkLabel *status_label;
but there is no
#include <gtk/gtk.h>
at the top of your file. When you include variables.h from main.c, you should be OK, because <gtk/gtk.h> is included ahead of variables.h. In all other files you will have a problem, because GtkLabel is an unknown type.
To correct this issue, include <gtk/gtk.h> at the top of your variables.h file. Then create a simple project with just the variables.h and a simple main.c that includes variables.h:
main.c
#include "variables.h"
int main() {
return 0;
}
Keep adding the missing headers until this simple main.c compiles. Then add your variables.h to the real project, and the problem should go away.
Related
I am currently getting the error message:
In file included from /usr/include/errno.h:35:0,
from lex.yy.c:21:
/usr/include/x86_64-linux-gnu/bits/errno.h:50:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
extern int *__errno_location (void) __THROW __attribute__ ((__const__));
^
(along with many others) when attempting a to compile a file generated by lex using the command:
gcc lex.yy.c
lex.l (file passed to lex to generate lex.yy.c) source:
%%
"hello world" printf("goodbye");
. ;
%%
This problem occurs when I try to compile any file which has been generated by lex or flex (I've tried both)
As I mentioned there are also many more errors but maybe fixing this one will solve some of the others. After looking for some common errors with errno.h and finding nothing of any use, I'm asking here.
I am using Ubuntu 16.04 LTS. Let me know if you would like/need more information regarding the problem and I'll do my best to help.
Thanks for any advice :)
Edit for 'rici':
The first 21 lines of my lex.yy.c file are as follows:
#line 3 "lex.yy.c"
#define YY_INT_ALIGNED short int
/* A lexical scanner generated by flex */
#define FLEX_SCANNER
#define YY_FLEX_MAJOR_VERSION 2
#define YY_FLEX_MINOR_VERSION 6
#define YY_FLEX_SUBMINOR_VERSION 0
#if YY_FLEX_SUBMINOR_VERSION > 0
#define FLEX_BETA
#endif
/* First, we deal with platform-specific or compiler-specific issues. */
/* begin standard C headers. */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
/* end standard C headers. */
Edit for #sepp2k:
I used vimdiff to compare the 2 files.
Things that are in my file but aren't in yours:
#ifdef __cplusplus
/* The "const" storage-class-modifier is valid. */
#define YY_USE_CONST
#else /* ! __cplusplus */
/* C99 requires __STDC__ to be defined as 1. */
#if defined (__STDC__)
#define YY_USE_CONST
#endif /* defined (__STDC__) */
#endif /* ! __cplusplus */
#ifdef YY_USE_CONST
===============================================================
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#line 476 "lex.yy.c"
There is nothing really which is in your file that is not in mine
Any other differences seem to just be differences of formatting.
I also tested the four header files in a standard C program (see what you mean now) and I can confirm it is errno.h which is causing the error.
A hello world in C with errno.h included threw up the following error:
In file included from /usr/include/errno.h:35:0,
from test.c:2:
/usr/include/x86_64-linux-gnu/bits/errno.h:50:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
extern int *__errno_location (void) __THROW __attribute__ ((__const__));
^
In file included from test.c:4:0:
/usr/include/stdio.h:13:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
int printf(const char* __restrict, ...);
^
edit for #rici:
Here is the full dump of errors thrown when i run "gcc lex.yy.c":
https://gist.github.com/raygarner/0601e57f5be21e16e0ae4ee34643b121
edit for #sepp2k:
earlier on, i tested this exact same compilation process on a fresh install of debian 9 in a VM and I got the exact same error I am doing here, on Ubuntu after fixing errno.h
Here is what it looks like:
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
As we've found out in the comments, the original error was due to a messed up errno.h on your system and you fixed that by reinstalling the file.
With that solved, your code will compile fine, but won't link as a standalone application because of missing yywrap and main functions. You can fix the former by defining yywrap or using %option noyywrap. The latter can be fixed by defining a main function or linking against an object file that defines main (if the lexer is part of a larger project that already defines its own main function).
You can also fix both problems by linking -lfl, which defines the yywrap and main functions to read either from stdin or the file names from argv and then prints the resulting tokens to stdout. Of course that's only useful for testing purposes as you'll want to do more than that in your main in real projects.
I want to define an enumeration type ONCE and have that type be shared across all the other files when I include the file, however I keep getting the following errors:
$ gcc -std=c99 main.c invoc.h invoc.c
main.c: In function ‘main’:
main.c:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘pr_alg’
main.c:12: error: ‘pr_alg’ undeclared (first use in this function)
main.c:12: error: (Each undeclared identifier is reported only once
main.c:12: error: for each function it appears in.)
main.c:13: error: ‘FIFO’ undeclared (first use in this function)
invoc.c:7: error: expected ‘)’ before ‘myalg’
The code is as follows:
invoc.h:
#define INVOC_H
#ifndef INVOC_H
typedef enum {FIFO, SECOND_CHANCE, RANDOM, NRU, CLOCK, AGING} alg_t;
void func1(alg_t myalg);
#endif
invoc.c:
#include "invoc.h"
void func1(alg_t myalg) {
myalg = NRU;
}
main.c:
#include "invoc.h"
int main(int argc, char **argv) {
extern alg_t pr_alg;
pr_alg = FIFO;
printf("PR_ALG: %d\n", pr_alg);
return 0;
}
Is there any way that I can define an enumeration in a .h file, and include it in all other files so that I can both create different variables of that type and pass it to functions?
You have an error in your invoc.h file:
#define INVOC_H
#ifndef INVOC_H
...
#endif
You first define a macro INVOC_H, then check if it does not exists (it does), so the code inside is removed by the preprocessor and not parsed by the compiler.
It should be:
#ifndef INVOC_H
#define INVOC_H
...
#endif
After this change your code will work fine.
You don't compile .h files, only .c files. That's why we put all definitions in .c files, and only declarations in .h files. To compile, just do:
gcc -std=c99 mmu.c invoc.c
You declare pr_alg in main() as extern variable. If the line you provided is the whole compilation line, the compile will issue linker error as variable pr_alg is nowhere defined. Remove extern or define variable pr_alg with global storage duration in one of the .c files.
I'm trying to build this example program dots.c from an old SDL tutorial here. http://www.dreamincode.net/forums/topic/64143-game-programming-in-linux-for-windows-programmers-part-2/ I'm getting errors from GCC which I think may be related to missing C escape sequences which were omitted either by my copy-pasting or the original HTML representation. But I don't know enough about C to say one way or the other. I'd like to know what changes the code needs to clear these build errors, ideally with how you identified those. Thank you.
dots.c:41:94: error: expected declaration specifiers before ‘/’ token
void set_sdl_pixel(SDL_PixelFormat *what,void *pixel,int width,int x,int y,int r,int g,int B)/>
^
dots.c:66:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
dots.c:91:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
dots.c:113:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
dots.c:132:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
dots.c:163:1: error: expected ‘{’ at end of input
}
^
40-42
/* Sets a pixel in a surface, paying attention to pixel format */
void set_sdl_pixel(SDL_PixelFormat *what,void *pixel,int width,int x,int y,int r,int g,int B)/>
{
64-66
/* Draws things using direct access to the surface */
void draw_pixels(SDL_Surface *screen)
{
89-91
/* Initializes the game */
void game_init()
{
111-113
/* Processes everything in the game for a frame */
void game_main()
{
130-132
/* Main */
int main()
{
158-163
}
/* Exit SDL */
SDL_Quit();
/* Exit */
return 0;
}
There was stray HTML in the tutorial code in the form of />'s on lines 41,51, 55, 59. These had to be deleted before GCC would successfully build. Thanks to tkausl for suggesting the solution.
bool somemethod(int number){
return true;
}
I keep getting this error message when I try to compile code with this method
/Users/user/Desktop/test.c:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘somemethod’
Thing is bool isn't a true keyword in C. Include stdbool.h if you need it - this should work with C99 implementations.
Yes, bool is not a keyword in C so from the prospective of the compiler you didn't include a return, hence the error.
#include <stdbool.h>
Should fix your problem, I have been on systems before where it was not possible to include this header file... if you do not want to/can not include stdbool.h you have a few other options:
#define true 1
#define false 0
typedef char bool
Something along these lines will allow you to continue as normal.
Well, I have a header (my_prog.h) which looks like this:
#ifndef __MY_HEADER_H
#define __MY_HEADER_H
#include <stddef.h>
typedef struct {
size_t something;
size_t something_else;
}
void my_func();
#endif
and implementation file (my_prog.c) where I put:
#include "my_prog.h"
static size_t min(size_t a, size_t b) {...}
void my_func() {...}
When I try to compile my_prog.c to object file (I need it for linking with other files) I fet:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘min’
The command I use for compiling is:
gcc -c my_prog.c -o my_prog.h
There's no error saying that it couldn't find the source. When I include in implementation file it compiles wihtout issues.
Remove the ... from the function body. Having them is a syntax error.
You have not given a typedef name to the structure and the ; is missing:
typedef struct {
size_t something;
size_t something_else;
} foo;
^^^^
In the compile line, following the -o you are specifying the name of your header file. This is incorrect. If the compilation goes fine(it will if you fix 1 and 2 above) , the compiler the wipe the original contents of my_prog.h and will overwrite it with the object file. Instead do :
gcc -c my_prog.c -o my_prog.o