c++ - macOS 上的 SDL2 和 KISS_SDL 链接问题
问题描述
我在将 KISS_SDL 安装和链接到我的 C++ 项目时遇到问题。
我正在运行 macOS v11.2.3 并已使用 brew 安装了 SDL2、SDL2_image 和 SDL2_ttf。
下面是 main.cpp 中的代码,它将打开一个简单的 SDL2 窗口:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include "kiss_sdl.h"
int main()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Failed to initialize the SDL2 library\n";
return -1;
}
SDL_Window * window = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
680, 480,
SDL_WINDOW_SHOWN);
if(!window)
{
std::cout << "Failed to create window\n";
return -1;
}
SDL_Surface * window_surface = SDL_GetWindowSurface(window);
if(!window_surface)
{
std::cout << "Failed to get the surface from the window\n";
return -1;
}
SDL_UpdateWindowSurface(window);
bool isquit = false;
SDL_Event event;
while (!isquit) {
if (SDL_PollEvent( & event)) {
if (event.type == SDL_QUIT) {
isquit = true;
}
}
}
}
使用下面的终端命令,我尝试编译和链接所需的库。
$ g++ -Wall -o main main.cpp -lSDL2 -lSDL2_image -lSDL2_ttf
当我在没有“#include”kiss_sdl.h“”的情况下编译时,程序按预期编译和执行。当我添加此包含时,我收到以下错误:
In file included from main.cpp:6:
./kiss_sdl.h:34:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
^~~~~~~
1 error generated.
这是来自 Kiss_sdl.h 的包含,我在第 34 行添加了一条注释,其中代码不起作用:
#ifndef _kiss_sdl_h
#define _kiss_sdl_h
#ifndef RESDIR
#define RESDIR ""
#endif
#if defined(_MSC_VER)
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#elif defined(__APPLE__)
#include <SDL.h> /* Line 34 */
#include <SDL_ttf.h>
#include <SDL_image.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#endif
#include <sys/types.h>
#ifdef _MSC_VER
#include <direct.h>
#include <io.h>
#else
#include <unistd.h>
#include <dirent.h>
#endif
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
解决方案
请查看项目中的问题 https://github.com/actsl/kiss_sdl,issue #21 就是基于此开始的。简而言之,这只是使用命令行构建,而不使用 makefile。但是使用的 g++ 命令行是错误的。在 Mac 中,头文件和库是由框架提供的,但命令行中缺少这些。由于该框架,也无需指定 SDL2 目录。这适用于许多使用 Mac 的人,并且框架在 makefile 中指定。这是一个简短的答案,它将在 Mac 中进行测试,但是这样做的人可能很忙,现在没有足够的时间。
推荐阅读
- java - 如何在解组期间强制 JAXB/Moxy 遵循顺序?
- flutter - Flutter:基于内容具有动态高度的选项卡
- r - 传单图例和调色板的对数刻度
- python-3.x - 如何使用 Python 在 fpdf 中居中表格?
- spring - 如何处理 SI HttpRequestExecutingMessageHandler 中的“Transfer-Encoding=chunked”
- flutter - 在真实设备上运行时,GeoLocation 包无法正常工作
- python - Cumsum Pandas groupby 两列
- python - 无法安装枕头模块
- flutter - Bloc Delegate:类只能扩展其他类。尝试指定不同的超类,或删除 extends 子句
- c++ - 循环在不应该的地方终止程序