简体   繁体   English

如何链接gcc和SDL_image库?

[英]How can I link with gcc and SDL_image library?

I have the following script: 我有以下脚本:

gcc -I /Library/Frameworks/SDL2.framework/Headers \
    -F /Library/Frameworks/ -framework SDL2 test.c

that successfully compiles the following source code: 成功编译以下源代码:

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

int init();
int loadMedia();
void close();

SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;

int init()
{
    int success = 1;

    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    }
}

int main(int argc, char* args[])
{
    return 0;
}

My question is how do I compile this with the added library SDL_image? 我的问题是如何使用添加的库SDL_image编译它? Would it be something like this? 它会是这样的吗?

gcc -I /Library/Frameworks/SDL2.framework/Headers \
    -I /Library/Frameworks/SDL_image.framework/Headers \
    -F /Library/Frameworks/ -framework SDL2 test.c

When I try this I get the following error: 当我尝试这个时,我收到以下错误:

In file included from test.c:2:
/Library/Frameworks/SDL_image.framework/Headers/SDL_image.h:27:10:      fatal error: 
      'SDL/SDL.h' file not found 
`#include <SDL/SDL.h>`

You could do: 你可以这样做:

gcc -L/libs/path -I/headers/path -lSDL2 -lSDL2_image gcc -L / libs / path -I / headers / path -lSDL2 -lSDL2_image

In this case, use #include "..." for your SDL2 headers inclusion. 在这种情况下,请使用#include "..."包含SDL2标头。

If you installed SDL2 with a package manager, the previous command is reduced to: 如果使用程序包管理器安装SDL2,则上一个命令将缩减为:

gcc -lSDL2 -lSDL2_image gcc -lSDL2 -lSDL2_image

since the path of SDL2 directory is then known to the compiler. 因为编译器已知SDL2目录的路径。 In that case you should use #include <...> . 在这种情况下,您应该使用#include <...>

You should consider writing a Makefile for your project. 您应该考虑为您的项目编写Makefile。 It would become easier to compile as your project grows. 随着项目的增长,编译会变得更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM