c++ - CMake关于类依赖的 undefined reference 错误

标签 c++ cmake undefined-reference openal

所以我几天前下载了 CLion,但我仍在习惯使用 CMake。

我认为这里的问题有一个非常简单的解决方案,但我似乎无法弄清楚。

我目前正在使用 OpenAL 为我的项目编写正弦波声音发生器。我已经将 FindOpenAl.cmake 文件包含到 cmake_modules 目录中,并确保我尽可能地设置了 CMakeLists.txt 文件。但是,当我尝试构建可执行文件时,我收到了对 OpenAL 函数 alGenBuffers 的 undefined reference 错误。

//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()

我知道我显然做错了什么,但我无法弄清楚它是什么。我最好的猜测是我应该将 OpenAL 库直接链接到 SineWaveGenerator.cpp,但我什至不知道这是否可能或是否应该这样做。

非常感谢。

编辑:我忘记添加错误报告。它添加在下面。

/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

编辑 2:详细的错误报告

/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

最佳答案

好的,那么:

  1. set(CMAKE_VERBOSE_MAKEFILE ON) 添加到您的 CMakeLists.txt(就在 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c+ 之后+11"))。并粘贴详细的错误报告。

  2. 尝试回答这些问题:Are you sure you change the Run Configuration (SHIFT+ALT+F10 or Run>Edit configurations) 为 Target All targetsRelease 模式?您确定详细错误中列出的每个库都存在并且这就是您需要的所有库吗?您的库是使用 C 或 C++ 编译器编译的吗?

正如您在评论中所说,问题在于:

There exists no var named OPENAL_LIBRARIES, it's OPENAL_LIBRARY

关于c++ - CMake关于类依赖的 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35874397/

相关文章:

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - 使用未初始化的指针作为函数参数

c++ - icu RegexMatcher 内存未释放

opencv - 生成 OpenCV 2.4.6(和一点)时,关于绝对和相对目标的大量 CMake 错误

c++ - AIX CMakeTest 没有添加正确的 -lpthread

c++ - 在 Windows 上的 Qt Creator 中编译 libmodbus 会引发 undefined reference to '_imp_....' 错误

c++ - 在 C++ 中比较文件更新的最佳方法?

c++ - 如何用空格填充 QString?

cmake - 使用 CMake GUI 设置交叉编译器文件

C 错误 : undefined reference to function, 但已定义