Reference SDL2/SDL.h导致非SDL代码中的引用未定义

Reference SDL2/SDL.h导致非SDL代码中的引用未定义,reference,g++,sdl,undefined,codeblocks,Reference,G++,Sdl,Undefined,Codeblocks,我一直有一个非常奇怪的问题,我似乎无法理解。我几乎确信这是一个编译器错误 xTech:xIncludes.hh #ifndef _xIncludes_ #define _xIncludes_ #define SDL_MAIN_HANDLED #include <string.h> #include <stdio.h> #include <stdarg.h> #include <stdint.h> #include <vector>

我一直有一个非常奇怪的问题,我似乎无法理解。我几乎确信这是一个编译器错误

xTech:xIncludes.hh

#ifndef _xIncludes_
#define _xIncludes_

#define SDL_MAIN_HANDLED

#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <vector>
#include <SDL2/SDL.h>

#if defined _WIN32
    #include <winsock.h>
#endif

#endif
\ifndef\u包括_
#定义_xIncludes_
#定义SDL_MAIN_HANDLED
#包括
#包括
#包括
#包括
#包括
#包括
#如果已定义_WIN32
#包括
#恩迪夫
#恩迪夫
xTech:xSound.cc

#include "xSound.hh"

int xOGGStreamSource::_stream(ALuint Buffer) {
    char data[BufferSize];
    int size = 0;
    int section;
    int result;

    while (size < BufferSize) {
        result = ov_read(&_oggstream, data + size, BufferSize - size, 0, 2, 1, &section);

        if (result > 0)
            size += result;
        else
            if (result < 0)
                return result;
            else
                break; //This seems a little redundant.... deal with it after it works.
    }

    if (size == 0) return 0;
    alBufferData(Buffer, _format, data, size, _vorbisinfo->rate);
    return 1;
}

void xOGGStreamSource::_empty() {
    int queued;

    alGetSourcei(_source, AL_BUFFERS_QUEUED, &queued);

    while (queued--) {
        ALuint Buffer;
        alSourceUnqueueBuffers(_source, 1, &Buffer);
    }
}

int xOGGStreamSource::Open(xString path) {
    int result;

    _oggfile = xOpenFile(path, "rb");

    if (_oggfile.Buffer == NULL) {
        xLogf("Audio", "Error in OGG File '%s', file does not exist.", path);
        return -3;
    }

    if (result = ov_open(_oggfile.Buffer, &_oggstream, NULL, 0) < 0) {
        xLogf("Audio", "Error in OGG File '%s', file is non-OGG.", path);
        xCloseFile(_oggfile);
        return -2;
    }

    _vorbisinfo = ov_info(&_oggstream, -1);
    _vorbiscomment = ov_comment(&_oggstream, -1);

    if (_vorbisinfo->channels == 1)
        _format = AL_FORMAT_MONO16;
    else
        _format = AL_FORMAT_STEREO16;

    alGenBuffers(2, _buffers);
    alGenSources(1, &_source);

    return 1;
}

void xOGGStreamSource::Close() {
    alSourceStop(_source);
    _empty();
    alDeleteSources(1, &_source);
    alDeleteBuffers(1, _buffers);

    ov_clear(&_oggstream);
}

int xOGGStreamSource::Playback() {
    if (Playing()) return 1;
    if (!_stream(_buffers[0])) return 0;
    if (!_stream(_buffers[1])) return 0;

    alSourceQueueBuffers(_source, 2, _buffers);
    alSourcePlay(_source);

    return 1;
}

int xOGGStreamSource::Playing() {
    ALenum state;
    alGetSourcei(_source, AL_SOURCE_STATE, &state);
    return (state == AL_PLAYING);
}

int xOGGStreamSource::Update(xVec3f_t Pos, xVec3f_t Vloc, xVec3f_t Dir, float Vol) {
    int processed;
    int active = 1;

    alSource3f(_source, AL_POSITION,        Pos.X, Pos.Y, Pos.Z);
    alSource3f(_source, AL_VELOCITY,        Vloc.X, Vloc.Y, Vloc.Z);
    alSource3f(_source, AL_DIRECTION,       Dir.X, Dir.Y, Dir.Z);
    alSourcef (_source, AL_GAIN, Vol);
    alSourcei (_source, AL_SOURCE_RELATIVE, AL_TRUE);

    alGetSourcei(_source, AL_BUFFERS_PROCESSED, &processed);

    while(processed--) {
        ALuint Buffer;
        alSourceUnqueueBuffers(_source, 1, &Buffer);
        active = _stream(Buffer);
        alSourceQueueBuffers(_source, 1, &Buffer);
    }

    return active;
}

xSound::xSound(xOGGStreamSource xss) { _source = xss; }

int xSound::PlaySound(float Volume, xVec3f_t Location) {
    if (!_source.Playback()) return -3;

    while(_source.Update(Location, xVec3f_t(0,0,0), xVec3f_t(0,0,0), Volume)) {
        if (!_source.Playing()) {
            if (!_source.Playback()) return -2;
            else return -1;
        }
    }
    _source.Close();
    return 1;
}

xSoundManager::xSoundManager(){}

int xSoundManager::Init() {
    _device = alcOpenDevice(NULL);
    if (!_device) return -2;

    _context = alcCreateContext(_device, NULL);
    if (alcMakeContextCurrent(_context) == ALC_FALSE || !_context) return -1;

    if (!Volume) {
        xLogf("Error", "Volume in Audio is not set properly. Setting to default");
        Volume = DEFAULT_VOLUME;
    }
    alListenerf(AL_GAIN, Volume);

    if (!BufferSize) {
        xLogf("Error", "Buffer size in Audio is not set properly. Setting to default");
        BufferSize = DEFAULT_BUFFER_SIZE;
    }

    return 0;
}

xSound* xSoundManager::LoadOGG(xString file) {
    xOGGStreamSource ogg;
    if (ogg.Open(file) < 0) return NULL;

    return new xSound(ogg);
}
#包括“xSound.hh”
int xOGGStreamSource::_流(ALuint缓冲区){
字符数据[BufferSize];
int size=0;
int段;
int结果;
while(大小0)
大小+=结果;
其他的
如果(结果<0)
返回结果;
其他的
break;//这似乎有点多余……等它工作后再处理。
}
如果(size==0)返回0;
alBufferData(缓冲区、格式、数据、大小、信息->速率);
返回1;
}
void xOGGStreamSource::_empty(){
int排队;
alGetSourcei(源、缓冲区排队和排队);
while(排队--){
明矾缓冲液;
alSourceUnqueueBuffers(_源、1和缓冲区);
}
}
int xOGGStreamSource::Open(xString路径){
int结果;
_oggfile=xOpenFile(路径,“rb”);
if(_oggfile.Buffer==NULL){
xLogf(“音频”,“OGG文件“%s”中有错误,文件不存在。”,路径);
返回-3;
}
if(result=ov_open(_oggfile.Buffer,&u oggstream,NULL,0)<0){
xLogf(“音频”,“OGG文件“%s”中出错,文件为非OGG”,路径);
xCloseFile(_oggfile);
返回-2;
}
_vorbisinfo=ov_信息(&_oggstream,-1);
_vorbiscomment=ov_comment(&_oggstream,-1);
如果(_vorbisinfo->channels==1)
_format=AL_format_MONO16;
其他的
_格式=AL_格式_16;
阿尔金缓冲区(2,_缓冲区);
阿尔及利亚资源(1,&U资源);
返回1;
}
void xOGGStreamSource::Close(){
alSourceStop(_来源);
_空();
alDeleteSources(1,&_来源);
alDeleteBuffers(1,_缓冲区);
ov_clear(&U oggstream);
}
int xOGGStreamSource::Playback(){
如果(Playing())返回1;
如果(!\u流(\u缓冲区[0])返回0;
如果(!_流(_缓冲区[1])返回0;
alSourceQueueBuffers(_源,2,_缓冲区);
alSourcePlay(_来源);
返回1;
}
int xOGGStreamSource::Playing(){
阿伦状态;
alGetSourcei(_source、AL_source、STATE和STATE);
返回(状态==AL_播放);
}
int xOGGStreamSource::Update(xVec3f_t Pos、xVec3f_t Vloc、xVec3f_t Dir、float Vol){
int处理;
int-active=1;
另一来源3F(来源、位置、位置X、位置Y、位置Z);
另一个震源3f(_震源,AL_速度,Vloc.X,Vloc.Y,Vloc.Z);
alSource3f(_源,AL方向,Dir.X,Dir.Y,Dir.Z);
alSourcef(源,增益,体积);
alSourcei(_source,AL_source,AL_RELATIVE,AL_TRUE);
alGetSourcei(源、所有缓冲区、已处理和已处理);
while(已处理--){
明矾缓冲液;
alSourceUnqueueBuffers(_源、1和缓冲区);
活动=_流(缓冲区);
alSourceQueueBuffers(_源、1和缓冲区);
}
主动返回;
}
xSound::xSound(xoggstreamsourcexss){{u source=xss;}
int xSound::PlaySound(浮动音量,xVec3f_t位置){
如果(!\u source.Playback())返回-3;
while(_source.Update(位置,xVec3f_t(0,0,0),xVec3f_t(0,0,0),卷)){
如果(!\u source.Playing()){
如果(!\u source.Playback())返回-2;
否则返回-1;
}
}
_source.Close();
返回1;
}
xSoundManager::xSoundManager(){}
int-xSoundManager::Init(){
_设备=alcOpenDevice(空);
如果(!\u设备)返回-2;
_context=alcreatecontext(_设备,NULL);
if(alcMakeContextCurrent(_context)==ALC_FALSE | |!_context)返回-1;
如果(!卷){
xLogf(“错误”,“音频中的音量设置不正确。设置为默认值”);
音量=默认音量;
}
alListenerf(AL_增益,体积);
如果(!缓冲区大小){
xLogf(“错误”,“音频中的缓冲区大小设置不正确。设置为默认值”);
BufferSize=默认的缓冲区大小;
}
返回0;
}
xSound*xSoundManager::LoadOGG(xString文件){
xogg;
if(ogg.Open(file)<0)返回NULL;
返回新的xSound(ogg);
}
xTechLibTest:main.cc

int main() {
    xSetLogFile("xTechLibTest.log");

    xSoundManager* audio = new xSoundManager();
    if (audio->Init() < 0) return -1;

    xSound* testsound1 = audio->LoadOGG("testsound.ogg");
    if (testsound1 == NULL) return -2;
    testsound1->PlaySound(1.0, xVec3f_t(1.0,0.5,0.3));
}
intmain(){
xSetLogFile(“xTechLibTest.log”);
xSoundManager*audio=newxsoundmanager();
if(audio->Init()<0)返回-1;
xSound*testsound1=audio->LoadOGG(“testsound.ogg”);
if(testsound1==NULL)返回-2;
testsound1->PlaySound(1.0,xVec3f_t(1.0,0.5,0.3));
}

上面的代码以及与之相关的所有内容(字符串实现等)都可以正常工作,没有任何问题。直到我包括SDL.h;对于我定义的每个函数,我都会得到未定义的引用,而编译器以前可以毫无问题地找到它们。似乎仅仅包含SDL.h就完全否定了我所做的任何定义。你知道这里发生了什么吗?

你是否正确地包含了SDL库的链接

如果您自己构建了二进制文件,则需要包含路径和库。在linux系统上,如果您自己构建了静态库,您将拥有一个名为libSDL2.a的二进制文件,但是要链接,您需要指定SDL2作为链接库

另请注意,您的xsound.h文件上是否有冗余的include保护(通过
#ifdefxsound
..)


p、 它将帮助其他用户,如果你指定你的环境是如何设置的;编译器、系统操作系统、IDE。

查看编译器/链接器的输出会很有用

在windows机器上使用Cygwin时,我在网络相关代码方面也遇到过类似的问题。在没有SDL的情况下,我的套接字工作得很好,只要我加入SDL,就会出现消息,说找不到某些头文件和引用<