1

我已经用谷歌搜索了我的错误的解决方案,但它似乎从来没有解决我的问题。我不确定问题出在哪里,因为#includes 似乎已签出。谢谢您的帮助。

错误:

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

生成文件:

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 $@ $(OBJECTS) $(LFLAGS)
$(OBJECTS) : $(SOURCES)
    gcc $< -c -o $@ $(CFLAGS)

.PHONY: clean

clean:
    del /Q *.o *.exe

测试.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);

}

测试.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

按钮.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);
}

按钮.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

布尔.h:

#ifndef idF3FFBDD5_86AB_4992_88037678AEADE02B
#define idF3FFBDD5_86AB_4992_88037678AEADE02B

typedef enum{false, true} bool;

#endif
4

2 回答 2

3

前 2 行清楚地显示了错误:

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

你正在编译test.c两次。

$<这是由于在 Makefile 中使用不正确造成的。它旨在用于模式规则,例如

.c.o:
        cc -c $< -o $@

%.o或带有和的更高级(不太便携)的 GNU 模式规则之一%.c

$(OBJECTS): $(SOURCES)的不是模式规则。这只是一个常规规则,其中插入了一些预先计算的文件名列表。

于 2013-05-25T23:23:51.660 回答
0

在我看来,您多次包含 bool.h 。如果包含包含 bool.h 的 Button.h,则无需像在 Button.c 或 test.c 中那样再次包含 bool.h。这可能会导致这样的奇怪行为。

于 2013-05-25T23:00:50.093 回答