Search code examples
c++cmakeundefined-referenceopenal

CMake undefined reference error on class dependency


so I downloaded CLion a couple of days ago and I'm still getting used to working with CMake.

The problem here I think has a quite simple solution, yet I can't seem to figure it out.

I'm currently coding a sine wave sound generator for a project of mine, using OpenAL. I already included the FindOpenAl.cmake file to the cmake_modules directory and made sure I set up the CMakeLists.txt file as well as I could. But when I try to build the executable, I get an undefined reference error to an OpenAL function, alGenBuffers.

//SineWaveGenerator.h

#ifndef BLINDSOUND_SINEWAVEGENERATOR_H
#define BLINDSOUND_SINEWAVEGENERATOR_H

#include <AL/al.h>
#include <AL/alc.h>

class SineWaveGenerator
{
    ALCdevice * dev;
    ALCcontext * ctx;
    ALuint buffer;

    SineWaveGenerator(ALCdevice * _dev, ALCcontext * _ctx);
    ALuint generateSineWave(double freq, double seconds, unsigned int sampleRate);
};

#endif //BLINDSOUND_SINEWAVEGENERATOR_H

//SineWaveGenerator.cpp

#include "../../include/Sound/SineWaveGenerator.h"

SineWaveGenerator::SineWaveGenerator(ALCdevice *_dev, ALCcontext *_ctx)
{
    this->dev = _dev;
    this->ctx = _ctx;

    alGenBuffers(1, &this->buffer);
}

ALuint SineWaveGenerator::generateSineWave(double freq, double seconds, unsigned int sampleRate)
{
    int buffer_size = (int)(sampleRate*seconds);
    short samples [buffer_size];
}

//CMakeLists.txt

make_minimum_required(VERSION 3.4)
project(BlindSound)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES src/main.cpp include/Sound/SineWaveGenerator.h src/Sound/SineWaveGenerator.cpp)
add_executable(BlindSound ${SOURCE_FILES})

#LIBRARIES
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})

##SFML
find_package(SFML 2.3 REQUIRED system window graphics audio)
if(SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(BlindSound ${SFML_LIBRARIES})
endif()

##OpenAL
find_package(OpenAL 1.0 REQUIRED)
if(OPENAL_FOUND)
    include_directories(${OPENAL_INCLUDE_DIR})
    target_link_libraries(BlindSound ${OPENAL_LIBRARIES})
endif()

I know I'm obviously doing something wrong, but I can't figure out what it is. My best guess is that I should link the OpenAL lib directly to SineWaveGenerator.cpp, but I don't even know if that is possible or if it should be done.

Thanks a lot in advanced.

EDIT: I forgot to add the error report. It's added below.

/home/chemicalchems/clion/bin/cmake/bin/cmake --build /home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Debug --target all -- -j 4
[ 33%] Linking CXX executable BlindSound
CMakeFiles/BlindSound.dir/src/Sound/SineWaveGenerator.cpp.o: In function `SineWaveGenerator::SineWaveGenerator(ALCdevice_struct*, ALCcontext_struct*)':
/home/chemicalchems/ClionProjects/BlindSound/src/Sound/SineWaveGenerator.cpp:12: undefined reference to `alGenBuffers'
collect2: error: ld returned 1 exit status
CMakeFiles/BlindSound.dir/build.make:124: recipe for target 'BlindSound' failed
make[2]: *** [BlindSound] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/BlindSound.dir/all' failed
make[1]: *** [CMakeFiles/BlindSound.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

EDIT 2: Detailed error report

/home/chemicalchems/clion/bin/cmake/bin/cmake --build /home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release --target BlindSound -- -j 4
/home/chemicalchems/clion/bin/cmake/bin/cmake -H/home/chemicalchems/ClionProjects/BlindSound -B/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/make -f CMakeFiles/Makefile2 BlindSound
make[1]: Entering directory '/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release'
/home/chemicalchems/clion/bin/cmake/bin/cmake -H/home/chemicalchems/ClionProjects/BlindSound -B/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release --check-build-system CMakeFiles/Makefile.cmake 0
/home/chemicalchems/clion/bin/cmake/bin/cmake -E cmake_progress_start /home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release/CMakeFiles 3
/usr/bin/make -f CMakeFiles/Makefile2 CMakeFiles/BlindSound.dir/all
make[2]: Entering directory '/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release'
/usr/bin/make -f CMakeFiles/BlindSound.dir/build.make CMakeFiles/BlindSound.dir/depend
make[3]: Entering directory '/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release'
cd /home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release && /home/chemicalchems/clion/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /home/chemicalchems/ClionProjects/BlindSound /home/chemicalchems/ClionProjects/BlindSound /home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release /home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release /home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release/CMakeFiles/BlindSound.dir/DependInfo.cmake --color=
make[3]: Leaving directory '/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release'
/usr/bin/make -f CMakeFiles/BlindSound.dir/build.make CMakeFiles/BlindSound.dir/build
make[3]: Entering directory '/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release'
[ 33%] Linking CXX executable BlindSound
/home/chemicalchems/clion/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/BlindSound.dir/link.txt --verbose=1
/usr/bin/c++    -std=c++11 -O3 -DNDEBUG   CMakeFiles/BlindSound.dir/src/main.cpp.o CMakeFiles/BlindSound.dir/src/Sound/SineWaveGenerator.cpp.o  -o BlindSound /usr/lib/x86_64-linux-gnu/libsfml-system.so /usr/lib/x86_64-linux-gnu/libsfml-window.so /usr/lib/x86_64-linux-gnu/libsfml-graphics.so /usr/lib/x86_64-linux-gnu/libsfml-audio.so 
CMakeFiles/BlindSound.dir/src/Sound/SineWaveGenerator.cpp.o: In function `SineWaveGenerator::SineWaveGenerator(ALCdevice_struct*, ALCcontext_struct*)':
SineWaveGenerator.cpp:(.text+0x11): undefined reference to `alGenBuffers'
collect2: error: ld returned 1 exit status
CMakeFiles/BlindSound.dir/build.make:127: recipe for target 'BlindSound' failed
make[3]: *** [BlindSound] Error 1
make[3]: Leaving directory '/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release'
CMakeFiles/Makefile2:70: recipe for target 'CMakeFiles/BlindSound.dir/all' failed
make[2]: *** [CMakeFiles/BlindSound.dir/all] Error 2
make[2]: Leaving directory '/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release'
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/BlindSound.dir/rule' failed
make[1]: *** [CMakeFiles/BlindSound.dir/rule] Error 2
make[1]: Leaving directory '/home/chemicalchems/.CLion15/system/cmake/generated/BlindSound-175ed20c/175ed20c/Release'
Makefile:121: recipe for target 'BlindSound' failed
make: *** [BlindSound] Error 2

Solution

  • Ok, then:

    1. Add set(CMAKE_VERBOSE_MAKEFILE ON) to your CMakeLists.txt (just after set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")). And paste the detailed error report.

    2. Try to answer these questions: Are you sure you change the Run Configuration (SHIFT+ALT+F10 or Run>Edit configurations) for Target All targets to Release mode? Are you sure every library listed in the detailed error exists and that are all the libraries you need? Is your library compiled with a C or C++ compiler?

    As you said in the comments, the problem was that:

    There exists no var named OPENAL_LIBRARIES, it's OPENAL_LIBRARY