Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

hadrienlearn

New Coder
Hello
I post this because I have some problem with my var
my code look like this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>

void SDL_InitApplication(void)

typedef struct
{
    SDL_Window *window;
    SDL_Renderer *renderer;
} App;

int main(int argc, char *argv[])
{
    memset(&app, 0, sizeof(App));

    SDL_InitApplication();

    return 1;
}

void SDL_InitApplication(void)
{
    SDL_Init(SDL_INIT_VIDEO);
    app.window = NULL;
    app.window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
    app.renderer = NULL;
    app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_SOFTWARE);
    return;
}
When I compile it.

C:\Users\boite\OneDrive\Bureau\Nouveau dossier (2)\Nouveau Projecr>gcc src/main.c -o bin/prog -I include -L lib -lmingw32 -lSDL2main -lSDL2
src/main.c: In function 'SDL_main':
src/main.c:14:17: error: 'app' undeclared (first use in this function); did you mean 'App'?
14 | memset(&app, 0, sizeof(App));
| ^~~
| App
src/main.c:14:17: note: each undeclared identifier is reported only once for each function it appears in
src/main.c:16:9: warning: implicit declaration of function 'SDL_InitApplication' [-Wimplicit-function-declaration]
16 | SDL_InitApplication();
| ^~~~~~~~~~~~~~~~~~~
src/main.c: At top level:
src/main.c:21:6: warning: conflicting types for 'SDL_InitApplication'; have 'void(void)'
21 | void SDL_InitApplication(void)
| ^~~~~~~~~~~~~~~~~~~
src/main.c:16:9: note: previous implicit declaration of 'SDL_InitApplication' with type 'void(void)'
16 | SDL_InitApplication();
| ^~~~~~~~~~~~~~~~~~~
src/main.c: In function 'SDL_InitApplication':
src/main.c:24:9: error: 'app' undeclared (first use in this function); did you mean 'App'?
24 | app.window = NULL;
| ^~~
| App

Can Anyone help me ?
 
Test this -
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>

void SDL_InitApplication(void);

typedef struct App
{
    SDL_Window *window;
    SDL_Renderer *renderer;
} App;

App app;

int main(int argc, char *argv[])
{
    memset(&app, 0, sizeof(App));

    SDL_InitApplication();

    return 1;
}

void SDL_InitApplication(void)
{
    SDL_Init(SDL_INIT_VIDEO);
    app.window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
    app.renderer = NULL;
    app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_SOFTWARE);
    return;
}
 
Test this -
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>

void SDL_InitApplication(void);

typedef struct App
{
    SDL_Window *window;
    SDL_Renderer *renderer;
} App;

App app;

int main(int argc, char *argv[])
{
    memset(&app, 0, sizeof(App));

    SDL_InitApplication();

    return 1;
}

void SDL_InitApplication(void)
{
    SDL_Init(SDL_INIT_VIDEO);
    app.window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
    app.renderer = NULL;
    app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_SOFTWARE);
    return;
}
Hello
Thank's for the answer but that doesn't work

src/fonction.c: In function 'SDL_InitApplication':
src/fonction.c:9:9: error: 'app' undeclared (first use in this function)
9 | app.window = NULL;
| ^~~
src/fonction.c:9:9: note: each undeclared identifier is reported only once for each function it appears in
src/fonction.c: In function 'SDL_CleanApplication':
src/fonction.c:40:13: error: 'app' undeclared (first use in this function)
40 | if (app.renderer != NULL)
 
Here it is compiling in CB.
View attachment 1145
Ok thanks that's compiling :)

But I still have a problem with it.
I can't use it in my other file.
I used to make a structure for my programme like:
main.c
C:
#include "header.h"

App app;

int main(int argc, char *argv[])
{   
    memset(&app, 0, sizeof(App));

    SDL_InitApplication();

    return 1;
}

// gcc src/*.c -o bin/prog -I include -L lib -lmingw32 -lSDL2main -lSDL2_image -lSDL2
fonction.c
C:
#include "header.h"

void SDL_InitApplication(void)
{
    SDL_Init(SDL_INIT_VIDEO);
    app.window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
    app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_SOFTWARE);
    return;
}
header.h
C:
#ifndef __HEADER__H__
#define __HEADER__H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <SDL.h>
#include <SDL_image.h>

#include "objects.h"

void SDL_InitApplication(void);

#endif
objects.h
C:
#ifndef __OBJECTS__H__
#define __OBJECTS__H__

typedef struct App
{
    SDL_Window *window;
    SDL_Renderer *renderer;
} App;


#endif
The header is like the backbone of my programme but when I compil I still have a problem.
C:\Users\boite\OneDrive\Bureau\CodeDir\Prob>gcc src/*.c -o bin/prog -I include -L lib -lmingw32 -lSDL2main -lSDL2_image -lSDL2
src/fonction.c: In function 'SDL_InitApplication':
src/fonction.c:6:9: error: 'app' undeclared (first use in this function); did you mean 'App'?
6 | app.window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
| ^~~
| App
src/fonction.c:6:9: note: each undeclared identifier is reported only once for each function it appears in
 
main.c
C:
#include "header.h"

int main(int argc, char *argv[])
{
    memset(&app, 0, sizeof(App));

    SDL_InitApplication();

    return 1;
}

function.c
C:
#include "header.h"
#include "objects.h"

void SDL_InitApplication(void)
{
    SDL_Init(SDL_INIT_VIDEO);
    app.window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
    app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_SOFTWARE);
    return;
}

objects.h
C:
#ifndef __OBJECTS__H__
#define __OBJECTS__H__

typedef struct App
{
    SDL_Window *window;
    SDL_Renderer *renderer;
} App;

App app;

#endif

header.h
C:
#ifndef __HEADER__H__
#define __HEADER__H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <SDL.h>
#include <SDL_image.h>

#include "objects.h"

void SDL_InitApplication(void);

#endif
 
main.c
C:
#include "header.h"

int main(int argc, char *argv[])
{
    memset(&app, 0, sizeof(App));

    SDL_InitApplication();

    return 1;
}

function.c
C:
#include "header.h"
#include "objects.h"

void SDL_InitApplication(void)
{
    SDL_Init(SDL_INIT_VIDEO);
    app.window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
    app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_SOFTWARE);
    return;
}

objects.h
C:
#ifndef __OBJECTS__H__
#define __OBJECTS__H__

typedef struct App
{
    SDL_Window *window;
    SDL_Renderer *renderer;
} App;

App app;

#endif

header.h
C:
#ifndef __HEADER__H__
#define __HEADER__H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <SDL.h>
#include <SDL_image.h>

#include "objects.h"

void SDL_InitApplication(void);

#endif
Hello
Thanks for the Reply but unfortunetely that make another error when it compile.
ERROR:
c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\boite\AppData\Local\Temp\ccsfaJQ4.o:main.c:(.bss+0x0): multiple definition of `app'; C:\Users\boite\AppData\Local\Temp\cccP5WGz.o:fonction.c:(.bss+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

I use this command to compile :
Bash:
gcc src/*.c -o bin/prog -I include -L lib -lmingw32 -lSDL2main -lSDL2_image -lSDL2
 

Attachments

  • Code.zip
    7.1 MB · Views: 1
Hello
Thanks for the Reply but unfortunetely that make another error when it compile.
ERROR:
c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\boite\AppData\Local\Temp\ccsfaJQ4.o:main.c:(.bss+0x0): multiple definition of `app'; C:\Users\boite\AppData\Local\Temp\cccP5WGz.o:fonction.c:(.bss+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

I use this command to compile :
Bash:
gcc src/*.c -o bin/prog -I include -L lib -lmingw32 -lSDL2main -lSDL2_image -lSDL2
main.c and fonction.c are both declaring (via header.h) the variable app. Since variables in C are global and public (at least in gcc) the second source file (main.c) gets the error, saying rightly it already has been defined in fonction.c. You could consider making app extern in fonction.c, but that is a bit of a hassle because of the intertwining include files. Personally I'd pass app as a reference parameter to SDL_InitApplication.

I'm not convinced by the convoluted way you define app
C:
typedef struct App
{
    SDL_Window *window;
    SDL_Renderer *renderer;
} App;

App app;

Keep it simple ! Why not just this:

C:
struct
{
    SDL_Window *window;
    SDL_Renderer *renderer;
} app;
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom