GCC Errors When Building Example Program From SDL Tutorial - c

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.

Related

project.c:43:68: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token

I dont understand what is the problem with the ->? I want to give the variable Number_Line the value of -1
project.c:43:68: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘->’ token
Entry_Machine_Code *new_entry = NULL, *entry_head = NULL; new_entry->Number_Line = -1; entry_head->Number_Line = -1;
#define NULL ((void*)0)
#define Array_Size 100
typedef struct entry_machine_code{
char entry_Name[Array_Size];
int Number_Line;
struct entry_machine_code *next;
}Entry_Machine_Code;
Entry_Machine_Code *new_entry = NULL, *entry_head = NULL; new_entry->Number_Line = -1; entry_head->Number_Line = -1;
// error here ^^
your last line is adding newlines to cut it:
Entry_Machine_Code *new_entry = NULL, *entry_head = NULL;
new_entry->Number_Line = -1;
entry_head->Number_Line = -1;
the first line define and initialize global variables, but the last two lines must be placed in a body, not at global level, the compiler does what it can to understand.
Visibly you use gcc and yes the messages produced by the compiler are funny
Out of that if you place these lines in a function and execute it you will have an other problem dereferencing NULL in the two last lines

several Compile Errors when using GCC

I am trying to upload a solution to an OJ ,the judge uses GCC
I have received the following errors and having no idea about them.
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token
void insert(int in){
^
In function ‘main’:
error: ‘struct mymultiset_int’ has no member named ‘insert’
x.insert(t);
^
error: ‘struct mymultiset_int’ has no member named ‘getmax’
printf("%d\n",x.getmax());
^
error: ‘struct mymultiset_int’ has no member named ‘_delete’
x._delete(0);
^
My code looks like this:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int t;
#define swap(a,b) t=b,b=a,a=t
/*
when using swap, I use format like swap(x,y); or swap(x,y),
*/
struct mymultiset_int{
int e[100000],end;
void insert(int in){...}
int getmax(){ return e[0]; }
void _delete(int i){...}
}x;
int main(){
x.end=0;memset(x.e,0,sizeof(x.e));
int N,t;scanf("%d",&N);
char i[2];
while (N--){
scanf("%s",i);
if (i[0]=='A'){
scanf("%d",&t);
x.insert(t);
}
else{
printf("%d\n",x.getmax());
x._delete(0);
}
}
}
you cannot define function in structure in C.
But it is possible in c++.

C program - little endian example - error

I am working on an example from book - CSPP - O'Hallaron & Bryant
on compile time i get an error -
gcc -Wall -W -Werror main.c -o endianc
also tried with cc main.c - same error
aaron#aaron-Box:~/xxx/cpp/endian$ cc main.c
main.c: In function ‘main’:
main.c:6:24: error: storage class specified for parameter ‘byte_pointer’
typedef unsigned char *byte_pointer;
^
main.c:8:17: error: expected declaration specifiers or ‘...’ before ‘byte_pointer’
void show_bytes(byte_pointer start, int len)
^
main.c:17:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
main.c:22:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
main.c:27:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
main.c:6:24: error: declaration for parameter ‘byte_pointer’ but no such parameter
typedef unsigned char *byte_pointer;
^
main.c:29:1: error: expected ‘{’ at end of input
}
actual program -
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len)
{
int i;
for (i = 0; i < len; i++)
printf(" %.2x", start[i]);
printf("\n");
}
void show_int(int x)
{
show_bytes((byte_pointer) &x, sizeof(int));
}
void show_float(float x)
{
show_bytes((byte_pointer) &x, sizeof(float));
}
void show_pointer(void *x)
{
show_bytes((byte_pointer) &x, sizeof(void *));
}
return 0;
}
thanks for your help.
You are missing the semicolon ; after void main(). (Also, it seems unlikely that you would need that line in the first place, but you need the actual implementation of the main function, and it has a return type of int, not void.)
Missing semicolon at main() that is why it is throwing the error.
The error is happening because "void main()" is immediately followed by typedef. The entire body of the main function is missing. Go back and check the book, and see what you left out.
ok i have fixed it now
replaced int main(void); with int main() notice NO simicolon -> i used it from this tutorial -> URL
and also replaced return 0; with return (0); notice the parenthesis with return.
i also noticed the examples in "c Programming Language" D.Richie -> example with main method do not include ;
1) can i please ask what are these variations in code ?
2) now i can not run the program ./endianc -> it has no response
i also tried
xx#aaron-Box:~/xxx/cpp/endian$ cc main.c
xx#aaron-Box:~/xxx/cpp/endian$ a out
a: command not found
(do i need to open separate thread for this problem?)
thanks all for your help.

Why do I get "error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’" ?

processpairs2.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘int’
Is the error I get everytime I compile my file, the error references me to line 7, which is where I declare the main() function.
my main's function is declared as
int main(int argc, char *argv[])
does anyone know what's causing this?
any help is greatly appreciated.
line 1-6 is
#include<stdio.h>
#define ROWS 4
#define COLS 10
void checkhighestpair();
int *ptotal, *pval1, *pval2
Are you not just missing the ; on the last line there?...
Append a ; after:
int *ptotal, *pval1, *pval2
so it becomes:
int *ptotal, *pval1, *pval2;

global variable compiler error

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.

Resources