Hallo,
Ich habe nach längerer Zeit wieder angefangen mit c++ und sdl zu programmieren und so passieren mir so manche einfache Fehler.
Ich habe mein Projekt mit VS 2013 Community aufgesetzt und solange ich meine SDL in der main.cpp Initialisiere funktioniert alles einwandfrei, sobald ich jedoch die Initialisierung in eine Funktion packe und diese in eine andere .cpp auslagere bekomme ich folgende Fehlermeldung beim Kompilieren: error LNK2005: "bool __cdecl init(struct SDL_Window *,struct SDL_Surface *)" (?init@@YA_NPAUSDL_Window@@PAUSDL_Surface@@@Z) already defined in Init.obj
Ich bin mir generell unsicher inwiefern ich SDL-Funktionen außerhalb der cpp-Datei verwenden kann in der die SDL initialisiert wurde. Damit meine ich ob es möglich ist die SDL in einer Funktion in einer Init.cpp zu initialisieren und diese Funktion in der main.cpp aufzurufen um dort ganz normal weitere SDL-Funktionen zu verwenden?
Hier mein Quellcode:
Main.cpp
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include "Init.cpp"
int main(int argc, char *argv[])
{
SDL_Window *gWindow = NULL;
SDL_Surface *gSurface = NULL;
if (!init(gWindow, gSurface))
{
printf("Failed to initialize!\n");
}
else
{
//Update the surface
SDL_UpdateWindowSurface(gWindow);
//Wait two seconds
SDL_Delay(2000);
}
SDL_DestroyWindow(gWindow);
SDL_FreeSurface(gSurface);
return 0;
}
|
Init.cpp
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#include <SDL.h>
#include <stdio.h>
bool init(SDL_Window* gWindow, SDL_Surface* gScreenSurface)
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
|
Vielen Dank im voraus für Eure/Ihre Antworten und allen ein schönes Wochenende!
Paul