1

I am trying to include portaudio and opus in my project, but whenever i compile, i get this error

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

This is my 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;
}

EDIT I currently have this is my 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)

and am now getting this error

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
blurrpy
  • 13
  • 5
  • `target_link_libraries(PeersChar PRIVATE )` – KamilCuk Jul 01 '20 at 13:17
  • `include_directories("F:\\Documents\\Programming\\Libraries\\opus-1.3.1\\include")` with cmake you probably want to use `find_package()` instead of a full path (which is likely only valid on your current machine) to some headers. – drescherjm Jul 01 '20 at 13:36
  • 1
    Does this answer your question? [CMake link to external library](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library). You are missing the link command to link the external libraries to your executable, hence the link errors... – Kevin Jul 01 '20 at 14:44
  • The new error message is because you did not setup the include directory. You probably need `target_include_directories()` [https://cmake.org/cmake/help/v3.16/command/target_include_directories.html](https://cmake.org/cmake/help/v3.16/command/target_include_directories.html) – drescherjm Jul 01 '20 at 21:24
  • @drescherjm I added `target_include_directories(PeersChat PUBLIC "F:/Documents/Programming/Libraries/opus/include")` and i get the error `F:/Documents/Programming/MinGW-W64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find F:/Documents/Programming/Libraries/opus: Permission denied` – blurrpy Jul 01 '20 at 21:57
  • I think you have `IMPORTED_LOCATION "F:/Documents/Programming/Libraries/opus"` wrong. I think this needs to be the path to the `.a` file – drescherjm Jul 01 '20 at 21:59
  • You may want to take the approach like the last example in the Imported targets here: [https://gitlab.kitware.com/cmake/community/-/wikis/doc/tutorials/Exporting-and-Importing-Targets#importing-targets](https://gitlab.kitware.com/cmake/community/-/wikis/doc/tutorials/Exporting-and-Importing-Targets#importing-targets) and what I mean by that is to use `find_library()` to find the library. – drescherjm Jul 01 '20 at 22:02
  • Is the `.a` file supposed to be the lib file? My folder does not contain that file. How would I get it? – blurrpy Jul 01 '20 at 22:03
  • An `.a` file is a static library produced by gcc. Although it could be `.lib` – drescherjm Jul 01 '20 at 22:07
  • The library does not contain a `.a` or `.lib` file. This is one of the problems I was encountering when trying other fixes. Do I have to do anything to create the file? – blurrpy Jul 01 '20 at 22:14
  • Maybe you have to build it from source. – drescherjm Jul 01 '20 at 23:01
  • I built it and I got a libopus.a file and added it to the `set_target_properties()` but I am now back at my original error of `undefined reference to...` I don't know if i built it correctly or whatever, but Im back at square one again. – blurrpy Jul 01 '20 at 23:07

1 Answers1

1

You need to add a line at the end of your CMakeLists.txt to link the libraries to your executable :

target_link_libraries (PeersChat PRIVATE <the_libraries_to_link>)
totok
  • 1,436
  • 9
  • 28
  • What exactly do I put in the ? I have no idea what that is asking for. Do I put a location on my disk where something is located or what? – blurrpy Jul 01 '20 at 13:28
  • 2
    You put whatever library that `opus_encoder_create` is defined in. – drescherjm Jul 01 '20 at 13:34
  • 2
    If you're lucky, it would be `target_link_libraries (PeersChat PRIVATE portaudio opus)` in your case. If not, try to find what are the names of the lib files that need to be linked. – totok Jul 01 '20 at 13:39
  • Your header file for 'opus' does not contain the definition of the symbols declared in its header file. You should have some definition files in the directory that you included (e.g. opus_encoder.c). One ideal way to do that is to build a target using definition files/source file for 'opus', attach the header locations to the target and then use that target name at `target_link_libraries` function – SALEH Jul 01 '20 at 13:41
  • I found [this question](https://stackoverflow.com/questions/43417564/linking-port-audio-library-to-c-project) that has the `portaudio` keyword in their linked libraries for the portaudio lib. – totok Jul 01 '20 at 14:06
  • 2
    The instructions for how to use `portaudio` with `CMake` are here: [http://portaudio.com/docs/v19-doxydocs/compile_cmake.html](http://portaudio.com/docs/v19-doxydocs/compile_cmake.html) – drescherjm Jul 01 '20 at 14:15
  • 1
    This looks like a finder module for opus: [https://gitlab.com/xonotic/daemon/blob/7a2f01df2783c121852047f8c8230403ad799579/cmake/FindOpus.cmake](https://gitlab.com/xonotic/daemon/blob/7a2f01df2783c121852047f8c8230403ad799579/cmake/FindOpus.cmake) – drescherjm Jul 01 '20 at 14:18