【发布时间】:2022-01-26 18:17:48
【问题描述】:
当我调用 SDL_Init 传入 SDL_AUDIO_INIT 时,函数返回失败。
我调用了 SDL_GetError() 但似乎没有任何消息。 我没有问题让 VIDEO 初始化,只有音频。编译时我没有收到任何错误。我已经在我的 makefile 中链接了 SDL 混音器库。
这是一个最小可重现的示例代码:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string>
int main( int argc, char* args[] )
{
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO < 0))
{
printf( "SDL VIDEO could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else if ( SDL_Init( SDL_INIT_AUDIO) < 0)
{
printf( "SDL AUDIO could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else
{
printf("video and audio initialized");
}
return 0;
}
输出:SDL AUDIO 无法初始化! SDL 错误:
(没有错误信息)
这是生成文件
#OBJS specifies which files to compile as part of the project
OBJS = 21_sound_effects_and_music.cpp
#CC specifies which compiler we're using
CC = g++
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -g -Wall
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = sdl_program
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
我使用的是 Ubuntu 20.04
【问题讨论】:
-
我正在使用我帖子底部链接中的确切代码。我使用 apt 安装了 SDL2。仅使用视频的程序可以正常初始化。
-
输出仍然缺失。也请直接开始在线搜索该错误消息。很有可能,其他人已经遇到了问题并解决了它。顺便说一句,作为这里的新用户,请使用tour 并阅读How to Ask。
-
if( SDL_Init( SDL_INIT_VIDEO < 0))应该是if( SDL_Init( SDL_INIT_VIDEO) < 0)- 注意括号。 -
我看到括号是错误的。修复它们并没有解决问题。