本文介绍了使用 gcc 编译 SDL 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用 Cmake 的情况下在 linux 命令行中使用 gcc 编译 sdl 项目?

编辑;

gcc SDLGAME.c pkg-config --cflags --libs sdl2

但我得到错误.

gcc: error: Pkg-config: No such file or directorygcc:错误:sdl2:没有那个文件或目录gcc:错误:无法识别的命令行选项--cflags"gcc:错误:无法识别的命令行选项--libs";你的意思是--libs ="?

@HolyBlackCat

源代码--->>

#include #include int main(int argc,char *argv[]){如果 (SDL_Init(SDL_INIT_VIDEO) !=0){printf(错误SDL");返回0;}SDL_Window* win=SDL_CreateWindow(游戏",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,500,500,0);返回0;}

我收到此错误--->>

error: XDG_RUNTIME_DIR 未在环境中设置.错误 SDL
解决方案

警告: 这不是一个完整的解决方案,而是一些建议,并以顶级评论和 OP 下的评论开头 [现已删除]回答.


要审查...

使用以下方法修复原始问题后:

gcc -o SDLGAME SDLGAME.c `pkg-config --cflags --libs sdl2`

运行程序的OP产生:

error: XDG_RUNTIME_DIR 未在环境中设置.

这可能是关于 ubuntu 安装本身的一个普遍问题.一些资源:https://askubuntu.com/questions/872792/what-is-xdg-runtime-dirhttps://askubuntu.com/questions/456689/error-xdg-runtime-dir-not-set-in-the-environment-when-attempting-to-run-naut

解决方法可能是:

export XDG_RUNTIME_DIR=/tmp/dirmkdir -p/tmp/dir

但是,我在我的家庭系统上成功运行了该程序,运行 Fedora 29,我的 ubuntu 系统运行 18.04.5

在我的系统上,XDG_RUNTIME_DIR 设置为 /run/user/1000.但是,有/没有解决方法,甚至在我的系统上执行 unset XDG_RUNTIME_DIR 工作.

然而:在我的 ubuntu 系统上,我在一年前删除了标准的 libsdl2 包,并从 source 包中重建和安装了它由于我遇到的一些问题.

因此,如果解决方法不起作用,我建议 libsdl2 从源代码重建/重新安装.

即使标准包正在运行,在调试您的应用程序时,能够查阅 libsdl2 源代码也是有帮助的.

请注意,我对您的应用所做的一项更改是在底部添加了 sleep(3),以便您可以看到窗口出现.


这是我用来从源代码构建/安装的方法:

可能需要卸载/删除 binary libsdl2 包.所以,你必须做(例如)

sudo apt-get remove libsdl2 libsdl2-dev

或者,无论二进制包叫什么[我忘记了].但是,那些也来自:apt-cache search libsdl2

所以,一旦清理干净,我所做的是:

  1. 创建一个目录(例如):$HOME/aptsrc
  2. cd $HOME/aptsrc
  3. 下载源码包[没有 sudo]:apt-get source libsdl2
  4. 这会提取几个文件(例如 *.tar.gz*.tar.xz*.dsc 和一个目录.在我的系统是:libsdl2-2.0.8+dfsg1,但对你来说可能不同.做(例如):DIR=$HOME/aptsrc/libsdl2-2.0.8+dfsg1
  5. cd $DIR
  6. 配置:$DIR/configure
  7. 运行 cmake:cmake $DIR
  8. 使用以下命令运行 make:make
  9. 安装:sudo make install

请注意,这来自我创建的内部脚本.即使在 cd $DIR 之后,我认为有必要在命令上使用完整路径[在指示的地方].

现在,库应该安装在/usr/local下.pkg-config --cflags --libs sdl2 的输出应该反映这一点:

-D_REENTRANT -I/usr/local/include/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -Wl,--enable-new-dtags -lSDL2

此命令的原始输出如下所示:

-I/usr/include/SDL2 -D_REENTRANT -lSDL2

这是针对二进制包的标准安装,所以如果你仍然安装了它,二进制包可能仍然被安装.

否则,您现在应该能够使用原始的 gcc 命令重建您的应用程序.现在,它应该附加到库的源构建版本.您可以使用:ldd ./SDLGAME 确认这一点,但只需运行它可能会更容易.

How can I compile a sdl project using gcc in the linux command line without using Cmake?

EDIT;

gcc SDLGAME.c pkg-config --cflags --libs sdl2

but i get error.

gcc: error: Pkg-config: No such file or directory
gcc: error: sdl2: No such file or directory
gcc: error: unrecognized command line option ‘--cflags’
gcc: error: unrecognized command line option ‘--libs’; did you mean ‘--libs=’?

@HolyBlackCat

source code --->>


#include <stdio.h>
#include <SDL2/SDL.h>

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

    if (SDL_Init(SDL_INIT_VIDEO) !=0)
    {
        printf("error SDL");
        return 0;
    }
    SDL_Window* win=SDL_CreateWindow("Game",
                    SDL_WINDOWPOS_CENTERED,
                    SDL_WINDOWPOS_CENTERED,
                    500,500,0);
    return 0;
}

I get this error--->>

error: XDG_RUNTIME_DIR not set in the environment.
error SDL
解决方案

Caveat: This isn't a total solution but some suggestions and is prefaced by top comment's and comments under OP's [now deleted] answer.


To review ...

After fixing the original issue by use of:

gcc -o SDLGAME SDLGAME.c `pkg-config --cflags --libs sdl2`

OP running the program produces:

error: XDG_RUNTIME_DIR not set in the environment.

This may be a general issue about the ubuntu install itself. Some resources for that: https://askubuntu.com/questions/872792/what-is-xdg-runtime-dir andhttps://askubuntu.com/questions/456689/error-xdg-runtime-dir-not-set-in-the-environment-when-attempting-to-run-naut

A workaround may be:

export XDG_RUNTIME_DIR=/tmp/dir
mkdir -p /tmp/dir

But, I ran the program successfully on my home system, running fedora 29 and my ubuntu system running 18.04.5

On my systems, XDG_RUNTIME_DIR was set to /run/user/1000. However, with/without the workaround and even doing unset XDG_RUNTIME_DIR worked on my systems.

However: On my ubuntu system, I had removed the standard libsdl2 package and rebuilt and installed it from the source package a year ago due to some issues I had.

So, if the workaround doesn't work, I recommend libsdl2 rebuild/reinstall from source.

Even if the standard package is working, when debugging your app, it can be helpful to be able to consult the libsdl2 source.

Note that one change I made to your app was to add a sleep(3) at the bottom so you can see the window come up.


Here is the method I used to build/install from source:

It's probably necessary to uninstall/remove the binary libsdl2 package. So, you'll have to do (e.g.)

sudo apt-get remove libsdl2 libsdl2-dev

Or, whatever the binary package is called [I forget]. But, those also came from: apt-cache search libsdl2

So, once that's cleaned out, what I did was:

  1. Create a directory (e.g.): $HOME/aptsrc
  2. cd $HOME/aptsrc
  3. Download the source package [without sudo]: apt-get source libsdl2
  4. This extracts several files (e.g. *.tar.gz, *.tar.xz, *.dsc and a directory. On my system, it was: libsdl2-2.0.8+dfsg1, but for you it may be different. Do (e.g.): DIR=$HOME/aptsrc/libsdl2-2.0.8+dfsg1
  5. cd $DIR
  6. Configure with: $DIR/configure
  7. Run cmake: cmake $DIR
  8. Run make with: make
  9. Install with: sudo make install

Note that this comes from an internal script I created. Even after the cd $DIR, I think it's necessary to use full path on the commands [where indicated].

Now, the library should be installed under /usr/local. The output of pkg-config --cflags --libs sdl2 should reflect this:

-D_REENTRANT -I/usr/local/include/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -Wl,--enable-new-dtags -lSDL2

The original output of this command would have looked like:

-I/usr/include/SDL2 -D_REENTRANT -lSDL2

This is for the standard install from the binary package, so if you still have that, the binary package may still be installed.

Otherwise, you should now be able to rebuild your app using the original gcc command. Now, it should be attached to the source built version of the library. You can confirm this with: ldd ./SDLGAME but just running it might be easier.

这篇关于使用 gcc 编译 SDL 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:56