2013-05-25 2 views
0

У меня есть решения для моей ошибки, но это, похоже, не решает мою проблему. Я не уверен, где проблема, потому что #includes, кажется, проверяют. Спасибо за помощь.C- Множественные определения и неопределенные ссылки с SDL

Ошибка:

gcc test.c -c -o test.o 
gcc test.c -c -o Button.o 
gcc -o test test.o Button.o -lmingw32 -lSDLmain -lSDL -lSDL_image -Wl,-subsystem 
,windows 
Button.o:test.c:(.text+0x0): multiple definition of `SDL_main' 
test.o:test.c:(.text+0x0): first defined here 
Button.o:test.c:(.text+0x152): multiple definition of `init' 
test.o:test.c:(.text+0x152): first defined here 
Button.o:test.c:(.text+0x1c3): multiple definition of `load_image' 
test.o:test.c:(.text+0x1c3): first defined here 
Button.o:test.c:(.text+0x252): multiple definition of `apply_surface' 
test.o:test.c:(.text+0x252): first defined here 
test.o:test.c:(.text+0x15): undefined reference to `Button_size' 
test.o:test.c:(.text+0x91): undefined reference to `Button_constr' 
test.o:test.c:(.text+0xb8): undefined reference to `Button_handleEvents' 
test.o:test.c:(.text+0x11b): undefined reference to `Button_show' 
Button.o:test.c:(.text+0x15): undefined reference to `Button_size' 
Button.o:test.c:(.text+0x91): undefined reference to `Button_constr' 
Button.o:test.c:(.text+0xb8): undefined reference to `Button_handleEvents' 
Button.o:test.c:(.text+0x11b): undefined reference to `Button_show' 
collect2: ld returned 1 exit status 
mingw32-make: *** [test] Error 1 

Makefile:

LFLAGS = -lmingw32 -lSDLmain -lSDL -lSDL_image -Wl,-subsystem,windows 
CFLAGS = 
SOURCES =test.c Button.c 
EXECUTABLE =test 

OBJECTS = $(SOURCES:.c=.o) 

all: $(EXECUTABLE) 
$(EXECUTABLE): $(OBJECTS) 
    gcc -o [email protected] $(OBJECTS) $(LFLAGS) 
$(OBJECTS) : $(SOURCES) 
    gcc $< -c -o [email protected] $(CFLAGS) 

.PHONY: clean 

clean: 
    del /Q *.o *.exe 

test.c

#include "SDL/SDL.h" 
#include "SDL/SDL_image.h" 
#include "bool.h" 
#include "Button.h" 

#define TEST_SCREEN_WIDTH 640 
#define TEST_SCREEN_HEIGHT 480 
#define TEST_SCREEN_BPP 32 

SDL_Surface *load_image(char* filename); 
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* dest, SDL_Rect* clip); 
bool init(SDL_Surface** screen); 
bool load_files(SDL_Surface** image); 
void clean_up(SDL_Surface* image); 

int main(int argc, char* args[]) 
{ 

    SDL_Surface* screen = NULL; 
    SDL_Surface* buttonSheet = NULL; 
    Button* myButton; 
    myButton = malloc(Button_size()); 
    SDL_Event event; 

    if(!init(&screen)) return 1; 
    bool quit = false; 

    if(! (buttonSheet = load_image("button.png"))) return 1; 

    if(!Button_constr(myButton, buttonSheet, 170,120,320,240)); 

    while(!quit) 
    { 

     while(SDL_PollEvent(&event)) 
     { 

      if(event.type == SDL_QUIT) quit = true; 
      else Button_handleEvents(myButton, &event); 

     } 

     SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF)); 

     Button_show(myButton, screen); 

     if(SDL_Flip(screen)) return 1; 

    } 

    free(myButton); 
    SDL_Quit(); 
    return 0; 
} 



bool init(SDL_Surface** screen) 
{ 

    if(SDL_Init(SDL_INIT_EVERYTHING)) return false; 

    (*screen) = SDL_SetVideoMode(TEST_SCREEN_WIDTH,TEST_SCREEN_HEIGHT,TEST_SCREEN_BPP,SDL_SWSURFACE); 

    if(!(*screen)) return false; 

    SDL_WM_SetCaption("Event test", NULL); 
    return true; 

} 

SDL_Surface *load_image(char* filename) 
{ 

    SDL_Surface* loadedImage = NULL; 
    SDL_Surface* optimizedImage = NULL; 

    loadedImage = IMG_Load(filename); 

    if(loadedImage) 
    { 

     optimizedImage = SDL_DisplayFormat(loadedImage); 
     SDL_FreeSurface(loadedImage); 

     if(optimizedImage) 
     { 

      Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF); 
      SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey); 

     } 

    } 

    return optimizedImage; 


} 

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* dest, SDL_Rect *clip) 
{ 

    SDL_Rect offset; 
    offset.x = x; 
    offset.y = y; 

    SDL_BlitSurface(source, clip, dest, &offset); 

} 

test.h:

#ifndef id250CBE4C_8C40_40EC_B4D7264D6B1E451C 
#define id250CBE4C_8C40_40EC_B4D7264D6B1E451C 

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* dest, SDL_Rect* clip); 

#endif 

button.c:

#include "SDL/SDL.h" 
#include "Button.h" 
#include "test.h" 
#include "bool.h" 

#define CLIP_MOUSEOVER 0 
#define CLIP_MOUSEOUT 1 
#define CLIP_MOUSEDOWN 2 
#define CLIP_MOUSEUP 3 

void Button_PRIVATE_setClips(Button* self); 

struct Button 
{ 

    SDL_Rect box; 
    SDL_Rect *clip; 
    SDL_Surface* image; 
    SDL_Rect clips[4]; 

}; 

int Button_size() 
{ 

    return sizeof(Button); 

} 

void Button_PRIVATE_setClips(Button* self) 
{ 

    self->clips[CLIP_MOUSEOVER].w = self->clips[CLIP_MOUSEOUT].w = self->clips[CLIP_MOUSEDOWN].w 
     = self->clips[CLIP_MOUSEUP].w = 320; 
    self->clips[CLIP_MOUSEOVER].h = self->clips[CLIP_MOUSEOUT].h = self->clips[CLIP_MOUSEDOWN].h 
     = self->clips[CLIP_MOUSEUP].h = 240; 

    self->clips[CLIP_MOUSEOVER].x = self->clips[CLIP_MOUSEOVER].y = 0; 
    self->clips[CLIP_MOUSEOUT].x = 320, self->clips[CLIP_MOUSEOUT].y = 0; 
    self->clips[CLIP_MOUSEDOWN].x = 0, self->clips[CLIP_MOUSEDOWN].y = 240; 
    self->clips[CLIP_MOUSEUP].x = 320, self->clips[CLIP_MOUSEUP].y = 240; 

} 

bool Button_constr(Button* self, SDL_Surface* image, int x, int y, int w, int h) 
{ 

    Button_PRIVATE_setClips(self); 

    self->box.x=x; 
    self->box.y=y; 
    self->box.w=w; 
    self->box.h=h; 

    self->clip = &self->clips[CLIP_MOUSEOUT]; 

} 

void Button_handleEvents(Button* self, SDL_Event* event) 
{ 

    int x=0, y=0; 
    SDL_GetMouseState(&x,&y); 
    self->clip = &self->clips[CLIP_MOUSEOUT]; 

    if((x> self->box.x) && (x< self->box.x+self->box.w) 
     && (y> self->box.y) && (y< self->box.y+self->box.h)) 
    { 

     if(event->type == SDL_MOUSEMOTION) self->clip = &self->clips[CLIP_MOUSEOVER]; 
     else if(event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) 
      self->clip = &self->clips[CLIP_MOUSEDOWN]; 
     else if(event->type == SDL_MOUSEBUTTONUP && event->button.button == SDL_BUTTON_LEFT) 
      self->clip = &self->clips[CLIP_MOUSEUP]; 

    } 


} 

void Button_show(Button* self, SDL_Surface* screen) 
{ 

    apply_surface(self->box.x, self->box.y, self->image, screen, self->clip); 
} 

button.h:

#ifndef id5CB3FF3A_7168_477D_AAFF0F481C46197C 
#define id5CB3FF3A_7168_477D_AAFF0F481C46197C 

#include "bool.h" 

typedef struct Button Button; 
//constructor 
bool Button_constr(Button* self, SDL_Surface* image, int x, int y, int w, int h); 
int Button_size(); 
void Button_handleEvents(Button* self, SDL_Event* event); 
void Button_show(Button* self, SDL_Surface* screen); 

#endif 

bool.h:

#ifndef idF3FFBDD5_86AB_4992_88037678AEADE02B 
#define idF3FFBDD5_86AB_4992_88037678AEADE02B 

typedef enum{false, true} bool; 

#endif 

ответ

3

Первые 2 строки показывают ошибку ясно:

gcc test.c -c -o test.o 
gcc test.c -c -o Button.o 

Вы собираете test.c дважды.

Это вызвано неправильным использованием $< в файле Makefile. Это предназначено для использования в шаблонных правилах, как

.c.o: 
     cc -c $< -o [email protected] 

или один из более причудливых (менее портативных) шаблонных правил GNU с %.o и %.c.

Ваш $(OBJECTS): $(SOURCES) не является правилом шаблонов. Это просто регулярное правило с некоторыми предварительно вычисленными списками имен файлов, интерполированными в нем.

+0

спасибо! – caps

0

Мне кажется, вы в том числе bool.h несколько раз. Если вы включили Button.h, который включает в себя bool.h, вам не нужно включать bool.h снова, как в Button.c или test.c. Это может вызвать странное поведение, подобное этому.

Смежные вопросы