libremidi icon indicating copy to clipboard operation
libremidi copied to clipboard

Can't compile midiprobe.cpp on Visual Studio Code

Open ruiseixasm opened this issue 3 years ago • 5 comments

Hi, I'm trying to compile the midiprobe.cpp however I'm getting the following error on my Visual Studio Code:

 *  Executing task: C/C++: MSVC Compiler - Version 19.29.30146 for x86 

Starting build...
cl.exe /Zi /std:c++17 /IC:\Users\rui\Documents\GitHub\libraries_implementations\projects\midi_libremidi\include /EHsc /nologo /FeC:\Users\rui\Documents\GitHub\libraries_implementations\projects\midi_libremidi\tests\midiprobe.exe C:\Users\rui\Documents\GitHub\libraries_implementations\projects\midi_libremidi\tests\midiprobe.cpp
midiprobe.cpp
midiprobe.obj : error LNK2019: unresolved external symbol "class std::vector<enum libremidi::API,class std::allocator<enum libremidi::API> > __cdecl libremidi::available_apis(void)" (?available_apis@libremidi@@YA?AV?$vector@W4API@libremidi@@V?$allocator@W4API@libremidi@@@std@@@std@@XZ) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: __thiscall libremidi::midi_in::midi_in(enum libremidi::API,class std::basic_string_view<char,struct std::char_traits<char> >,unsigned int)" (??0midi_in@libremidi@@QAE@W4API@1@V?$basic_string_view@DU?$char_traits@D@std@@@std@@I@Z) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: __thiscall libremidi::midi_in::~midi_in(void)" (??1midi_in@libremidi@@QAE@XZ) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: enum libremidi::API __thiscall libremidi::midi_in::get_current_api(void)const " (?get_current_api@midi_in@libremidi@@QBE?AW4API@2@XZ) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall libremidi::midi_in::get_port_count(void)" (?get_port_count@midi_in@libremidi@@QAEIXZ) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall libremidi::midi_in::get_port_name(unsigned int)" (?get_port_name@midi_in@libremidi@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: __thiscall libremidi::midi_out::midi_out(enum libremidi::API,class std::basic_string_view<char,struct std::char_traits<char> >)" (??0midi_out@libremidi@@QAE@W4API@1@V?$basic_string_view@DU?$char_traits@D@std@@@std@@@Z) referenced in function "public: __thiscall libremidi::midi_out::midi_out(void)" (??0midi_out@libremidi@@QAE@XZ)
midiprobe.obj : error LNK2019: unresolved external symbol "public: __thiscall libremidi::midi_out::~midi_out(void)" (??1midi_out@libremidi@@QAE@XZ) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: enum libremidi::API __thiscall libremidi::midi_out::get_current_api(void)" (?get_current_api@midi_out@libremidi@@QAE?AW4API@2@XZ) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall libremidi::midi_out::get_port_count(void)" (?get_port_count@midi_out@libremidi@@QAEIXZ) referenced in function _main
midiprobe.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall libremidi::midi_out::get_port_name(unsigned int)" (?get_port_name@midi_out@libremidi@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z) referenced in function _main
C:\Users\rui\Documents\GitHub\libraries_implementations\projects\midi_libremidi\tests\midiprobe.exe : fatal error LNK1120: 11 unresolved externals

Build finished with error(s).

 *  The terminal process terminated with exit code: -1. 
 *  Terminal will be reused by tasks, press any key to close it. 

As you can see, I'm getting a lot of these errors concerning error LNK2019! I wold like to use this library on my projects, but for some reason it never compiles...

ruiseixasm avatar Sep 19 '22 02:09 ruiseixasm

Hello! This looks like vscode isn't linking against the libremidi library. Are you using its cmake integration (e.g. opening the folder as a CMake project) ? It should work automatically if you do that..

jcelerier avatar Sep 19 '22 17:09 jcelerier

Note that in the general case you cannot expect to just build random C++ files like this - some kind of system has to setup the include directories, compiler flags, link flags, etc. It's a bit weird that vscode provides this features as it will rarely if ever work in real-world projects

jcelerier avatar Sep 19 '22 17:09 jcelerier

Hi, so I have to use the make file to compile this library. I wonder if that is possible in Windows OS and how to use it with my code, it seems I need to create my own make file too. Thanks, I will check more about make.

ruiseixasm avatar Sep 19 '22 17:09 ruiseixasm

Nope, don't use make! we use cmake which will generate everything needed automatically.

If you have the right extensions installed just "opening" the libremidi folder in vscode should be enough. These extensions are listed here: https://github.com/gvcallen/CMake-VSCode-Tutorial You also need to install cmake (latest release installer here: https://cmake.org/download/) and when it asks, make sure that "add CMake to PATH" is checked in the installer. You may have to reboot for this to take effect as it's ms windows :-)

jcelerier avatar Sep 19 '22 17:09 jcelerier

Ok, I get it, in either case I always need a form of make (cmake). Then, maybe I can use the outputted binaries as third part libraries and then I won't need to do any make again when using them on my own software in some form of binary include. Nevertheless I see cmake is the way to go even for my personal software if it gets too big.

ruiseixasm avatar Sep 19 '22 22:09 ruiseixasm

Hi @ruiseixasm,

here is a basic project structure:

example
│   CMakeLists.txt    
│
└───src
│   │   main.cpp
│   
└───3rdparty
    │
    └───libremidi
        │   ...

... and the corresponding CMakeLists.txt that works for me

cmake_minimum_required(VERSION 3.13)

project(exampleProject)

set(CMAKE_CXX_STANDARD 17)
set(LIBREMIDI_HEADER_ONLY ON)
set(LIBREMIDI_EXAMPLES ON)

add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty/libremidi)
add_executable(example src/main.cpp)
target_link_libraries(example libremidi)

Stmpf avatar Oct 19 '22 15:10 Stmpf