Findrzkeeprz
Findrzkeeprz

Reputation: 95

Undefined reference question

When I try compiling this:

#include "OriAudioCache.hpp"

int main()
{
    System *audioSystem(0);
    FMOD_RESULT result;
    result = System_Create(&audioSystem);
    FMOD_CHECK_STATE(result);
    OriAudioCache cache(audioSystem, 20);
    string title("Ambitious Girl");
    string path("/home/findrzkeeprz/Desktop/Resources/The_Ambitious_Girl.mp3");
    cache.LoadSound(title, path, Default);
    vector<OriSound>::iterator v_iter(cache.FindSound(title));
    cache.PlaySound(v_iter->sound());
}

Which uses these files: OriAudioCache.hpp

#ifndef ORI_AUDIO_CACHE_HPP_
#define ORI_AUDIO_CACHE_HPP_

#include "OriSound.hpp"
#include "OriChannel.hpp"

class OriAudioCache
{
    public:
         OriAudioCache(System *audioSystem, int maxChannels);

        ~OriAudioCache()
        {
            vector<OriSound>::iterator v_iter(audioCache_.begin());
            for(; v_iter != audioCache_.end(); ++v_iter)
            {
                v_iter->~OriSound();
            }
                delete audioSystem_;
        }

        void LoadSound(string const& title, string const& path, AudioLoadMode mode);
        vector<OriSound>::iterator FindSound(string const& title);
        void RemoveSound(string const& title);
        void PlaySound(Sound* sound);
        vector<OriChannel>::iterator RequestChannel(bool &allocStatus, FMOD_CHANNELINDEX &allocMode);
        void ReleaseChannel(Channel *channel);

    private:
        void inline SortChannels() {sort(channels_.begin(),channels_.end());}

        vector<OriSound> audioCache_;
        vector<OriChannel> channels_;
        System *audioSystem_;
};

#endif

and OriAudioCache.cpp

#include "OriAudioCache.hpp"

OriAudioCache::OriAudioCache(System *audioSystem, int maxChannels)
        :audioSystem_(audioSystem), channels_(maxChannels){}

void OriAudioCache::LoadSound(string const& title, string const& path, AudioLoadMode mode)
{
    OriSound sound(title, path, audioSystem_, mode);
    vector<OriSound>::iterator pos =lower_bound(audioCache_.begin(), audioCache_.end(), sound);
    audioCache_.insert(pos, sound);
}

vector<OriSound>::iterator OriAudioCache::FindSound(string const& title)
{
    vector<OriSound>::iterator v_iter(audioCache_.begin());
    for(; v_iter != audioCache_.end(); ++v_iter) //Would better if I could use a binary search here
    {
        if(v_iter->title() == title) return v_iter;
        else continue;
    }
    return audioCache_.end(); 
}
void OriAudioCache::RemoveSound(string const& title)
{
    vector<OriSound>::iterator v_iter(audioCache_.begin());
    for(; v_iter != audioCache_.end(); ++v_iter) //Would better if I could use a binary search here
    {
        if(v_iter->title() == title) audioCache_.erase(v_iter);
        else continue;
    }
}

void OriAudioCache::PlaySound(Sound* sound)
{
    bool channelAlloc(false);
    FMOD_CHANNELINDEX allocMode = FMOD_CHANNEL_FREE;
    vector<OriChannel>::iterator oriChannel = RequestChannel(channelAlloc, allocMode);
    if(channelAlloc)
    {
        FMOD_RESULT result;
        Channel *chnl = oriChannel->channel();
        result = audioSystem_->playSound(allocMode, sound, false, &chnl);
        FMOD_CHECK_STATE(result);
        bool isPlaying(false);
        chnl->isPlaying(&isPlaying);

        while(isPlaying)
        {
            chnl->isPlaying(&isPlaying);
        }

        bool paused(false);
        chnl->getPaused(&paused);
        if(!paused)
        {
            ReleaseChannel(chnl);
        }
        SortChannels(); //sort channels, reoder for channel requests
    }
}

vector<OriChannel>::iterator OriAudioCache::RequestChannel(bool &allocStatus, FMOD_CHANNELINDEX &allocMode)
{
    vector<OriChannel>::iterator vOri_iter(channels_.begin());
    if(vOri_iter->status() == false)
    {
        if(vOri_iter->channel() == 0) 
        {
            allocMode = FMOD_CHANNEL_FREE;
            vOri_iter->setStatus(true); // flag channel as being used
            return vOri_iter;
        }
        else allocMode = FMOD_CHANNEL_REUSE;
        vOri_iter->setStatus(true); // flag channel as being used
        return vOri_iter;
    }
    else return channels_.end();
}

void OriAudioCache::ReleaseChannel(Channel *channel)
{
    bool playing(false);
    bool paused(false);
    channel->isPlaying(&playing);
    channel->getPaused(&paused);
    if(!playing && !paused)
    {
        vector<OriChannel>::iterator vOri_iter(channels_.begin());
        for(; vOri_iter != channels_.end(); ++vOri_iter)
        {
            if(vOri_iter->channel() == channel) vOri_iter->setStatus(false);
        }
    }
}

I get undefined reference errors:

findrzkeeprz@Aardvak:~/Documents/Chidori/Engine/Audio$ make
    g++ -ggdb -I../../ -I../../Engine -I../../Include -I../../Public -o audio main.cpp ../../Libraries/FMODEX/libfmodex.so
    /tmp/cctNhPVy.o: In function `main':
    /home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:9: undefined reference to `OriAudioCache::OriAudioCache(FMOD::System*, int)'
    /home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:12: undefined reference to `OriAudioCache::LoadSound(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, AudioLoadMode)'
    /home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:13: undefined reference to `OriAudioCache::FindSound(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
    /home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:14: undefined reference to `OriAudioCache::PlaySound(FMOD::Sound*)'
    collect2: ld returned 1 exit status
    make: *** [audio] Error 1

What I'm I doing wrong here?

Upvotes: 2

Views: 697

Answers (2)

Thomas Matthews
Thomas Matthews

Reputation: 57749

In general, the term "Undefined reference" from a compiler (actually the linker) means that some code fragment is accessing a symbol that the linker could not find.

Common causes of these errors:

  • Source file containing the definition is not included in the build process.
  • A source fragment (code lines) were not removed.
  • A header file (class declaration) declares the given method or symbol (which was not removed or needs to be implemented).
  • Incorrect or missing scope resolution operator (namespace issue).
  • Build process using wrong version of source files (e.g. file not checked into CMS.).

Most cases of these errors are not about the C++ reference operator nor dereferencing pointers.

Solutions:

  • Code review by independent eye(s).
  • Code inspection tools: Cppcheck, Valgrind, Klocwork, etc.
  • Make fewer changes, compiler more often.
  • Use Test Driven Development. ;-)

Upvotes: 0

Dr. Snoopy
Dr. Snoopy

Reputation: 56397

You're compiling OriAudioCache.hpp, when you should be compiling OriAudioCache.cpp, that's assuming that file that contains the implementation.

Upvotes: 1

Related Questions