ld:找不到库

时间:2014-02-03 19:02:08

标签: c++ build stdio

所以我刚刚开始做一些SDL教程,当我突然收到一条错误消息“ld:找不到-lstring的库”时,我得到了一个。关于stdio也是如此。

我尝试添加路径和库。我使用MacOS X C ++链接器(MacOSX GCC工具链),但我似乎无法让它工作。有什么想法吗?

我正在使用Eclipse btw。

/*
 * Main.cpp
 *
 *  Created on: 29 jan 2014
 *      Author: CaptainAwesome
 */

//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <string>

//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Free media and shuts down SDL
void close();
//Loads individual image
SDL_Surface* loadSurface(std::string path);

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
enum KeyPressSurfaces
{
    KEY_PRESS_SURFACE_DEFAULT,
    KEY_PRESS_SURFACE_UP,
    KEY_PRESS_SURFACE_DOWN,
    KEY_PRESS_SURFACE_LEFT,
    KEY_PRESS_SURFACE_RIGHT,
    KEY_PRESS_SURFACE_TOTAL
};

//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image that corresponds to a keypress
SDL_Surface* gKeyPressSurfaces[ KEY_PRESS_SURFACE_TOTAL];
//The image we will load and show on the screen
SDL_Surface* gCurrentSurface = NULL;


bool init()
{
    //Init flag
    bool success = true;

    //Init 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, SCREEN_WIDTH, SCREEN_HEIGHT, 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;
}

SDL_Surface* loadSurface(std::string path)
{
    //Load image at specified path
    SDL_Surface* loadedSurface = IMG_Load(path.c_str());
    if(loadedSurface == NULL)
    {
        printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
    }
    return loadedSurface;
}

bool loadMedia()
{
    //Load success flag
    bool success = true;

    //Load default surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("Assets/maxresdefault.jpg");
    if(gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == NULL)
    {
        printf("Unable to load default image %s\n!");
        success = false;
    }
    //Load up surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = loadSurface("Assets/1.jpg");
    if(gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == NULL)
    {
        printf("Unable to load up image %s\n!");
        success = false;
    }
    //Load down surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = loadSurface("Assets/duty_calls");
    if(gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == NULL)
    {
        printf("Unable to load down image %s\n!");
        success = false;
    }
    //Load up surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] = loadSurface("Assets/mario-land.jpg");
    if(gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] == NULL)
    {
        printf("Unable to load left image %s\n!");
        success = false;
    }
    //Load up surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] = loadSurface("Assets/ML.jpg");
    if(gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] == NULL)
    {
        printf("Unable to load right image %s\n!");
        success = false;
    }

    return success;
}

void close()
{
    //Deallocate surface
    SDL_FreeSurface(gCurrentSurface);
    gCurrentSurface = NULL;

    //Destroooooy window
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    //Quit SDL subsystems
    SDL_Quit();
}

int main (int argc, char* args[])
{
    //Start up SDL and create window
    if(!init())
    {
        printf("Fail to init!\n");
    }
    else
    {
        //Load media
        if(!loadMedia())
        {
            printf("Failed to load media!\n");
        }
        else
        {
            //Main loop flag
            bool quit = false;

            //Event handler
            SDL_Event e;

            //Set default current surface
            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];

            //While app is running
            while(!quit)
            {
                while(SDL_PollEvent(&e) != 0)
                {
                    //User presses esc
                    if(e.type == SDL_QUIT)
                    {
                        quit = true;
                    }
                    //User presses a key (up, down, left, right)
                    else if(e.type)
                    {
                        //Select surfaces based on key pressed
                        switch(e.key.keysym.sym)
                        {
                        case SDLK_UP:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
                            break;
                        case SDLK_DOWN:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
                            break;
                        case SDLK_LEFT:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
                            break;
                        case SDLK_RIGHT:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
                            break;
                        case SDLK_ESCAPE:
                            quit = true;
                            break;
                        default:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
                            break;

                        }
                    }
                //Apply image
                SDL_BlitScaled(gCurrentSurface, NULL, gScreenSurface, NULL);
                //Update surface
                SDL_UpdateWindowSurface(gWindow);
                }
            }
        }
    }

    //Free resources and close SDL
    close();

    return 0;
}

0 个答案:

没有答案