gpt4 book ai didi

c++ - 链接 cmake 时出现 undefined reference 错误

转载 作者:行者123 更新时间:2023-12-02 10:13:33 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What is an undefined reference/unresolved external symbol error and how do I fix it?

(37 个回答)



CMake link to external library

(5 个回答)


1年前关闭。




我正在尝试在我的项目中包含 portaudio 和 opus,但是每当我编译时,我都会收到此错误

Scanning dependencies of target PeersChat
[ 50%] Building CXX object CMakeFiles/PeersChat.dir/main.cpp.obj
[100%] Linking CXX executable PeersChat.exe
CMakeFiles\PeersChat.dir/objects.a(main.cpp.obj): In function `main':
F:/Documents/Programming/Projects/PeersChat/main.cpp:13: undefined reference to `opus_encoder_create'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\PeersChat.dir\build.make:86: PeersChat.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/PeersChat.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/PeersChat.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: PeersChat] Error 2
这是我的 main.cpp
#include <iostream>
#include <opus.h>

int main() {

opus_int32 sampleRate = 8000;
int channels = 2;

int error;

OpusEncoder *enc;
enc = opus_encoder_create(sampleRate, channels, OPUS_APPLICATION_VOIP, &error);

return 0;
}

编辑
我目前有这是我的 cmake
cmake_minimum_required(VERSION 3.16)
project(PeersChat)

set(CMAKE_CXX_STANDARD 17)

# Opus Library
add_library(opus STATIC IMPORTED GLOBAL)
set_target_properties(opus PROPERTIES IMPORTED_LOCATION "F:/Documents/Programming/Libraries/opus")

add_executable(PeersChat main.cpp)
target_link_libraries(PeersChat opus)
现在我收到此错误
fatal error: opus.h: No such file or directory
#include <opus.h>
^~~~~~~~
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles\PeersChat.dir\build.make:62: CMakeFiles/PeersChat.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/PeersChat.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/PeersChat.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: PeersChat] Error 2

最佳答案

您需要在 CMakeLists.txt 的末尾添加一行以将库链接到您的可执行文件:

target_link_libraries (PeersChat PRIVATE <the_libraries_to_link>)

关于c++ - 链接 cmake 时出现 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62677676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com