M
M
mr-ZA2019-03-13 08:54:42
C++ / C#
mr-ZA, 2019-03-13 08:54:42

Linking with a C++ library?

Hello everyone, here's an example:

#include <iostream>
#include <windows.h>

int main() {
    mciSendString("play C:\\WINDOWS\\Media\\onestop.mid", NULL, 0, NULL);
    std::cout << "played...\npress enter for stop";
    std::cin.get();
    mciSendString("stop C:\\WINDOWS\\Media\\onestop.mid", NULL, 0, NULL);
    std::cout << "stopped...\npress enter for exit";
    std::cin.get();
    return 0;
}

link with winmm.lib, additionally handle possible errors of calling mciSendString()
It is better to use some third-party library and play mp3, mod, it, ogg, etc. Midi with a standard Windows sequencer sounds very poor. ( https://otvet.mail.ru/question/82279023)
How to link the library and source and where to get the winmm.lib library?
The second example with a similar goal is to play music in the console:
#include <iostream>
#include <Windows.h>
#include "MMSystem.h"
using namespace std;

int main()
{
PlaySound(TEXT("LHSRLD11.wav"), NULL, SND_SYNC);

system("pause");
return 0;
}

How to manually link libraries from source in cmd? I tried to manually download the lib, place it in the directory with the file and from the console [g++ -c main.cpp mmsystem.h] but it gives errors:
5c889908573d6146546776.png
Also, please tell me how to connect the SFML library to Qt Creator? SFML needs to be used in the project, I downloaded it from the office. site but there for separate compilers (in my case, the version for Visual C ++ 15 (2017) - 64-bit is suitable)
How can I now embed it in Qt so that I can add it to the .pro file in the project and use it?
5c889abdd94ae411803573.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2019-03-13
@mr-ZA

mmsystem.h and winmm.lib are a standard Windows header file and library provided with the Windows SDK, which is included with Visual Studio. mmsystem.h is available from the start in the studio, just use it in #include, you don't need to copy it to your project. winmm.lib is also available, but it must be included in the project - the -lwinmm.lib option of the compiler or set in the project properties as an additional library used if you are using an IDE. It is not enabled by default.
In general, to connect third-party libraries to any C / C ++ project, you need to:
1. add the path where the library header files are located as an option -I<path header files>to the compiler launch command.
2. add the path where the lib files (lib for msvc) of the library are located to the option-L<path to lib files>. If there is only one library file, then this step can be skipped and the full path can be specified in step 3. If there are several files, then it is easier to specify the path to the directory in the -L option, and in step 3, specify only the name of a specific library file.
3. add the name of the library file (lib file) to the option -l<lib name>or the full path to the lib file.
If you are building from the IDE, then the same steps must be done in the project settings. Compiler options are almost universal - the same options (and similar approach) are used in msvc, gcc, clang and other compilers.
For building libraries with a Qt project:
In the pro file:
LIBS variable - compiler options -L and -l are set (p.2 and p.3)
INCLUDEPATH variable - compiler options -I are set (p.1)
For example:

INCLUDEPATH += -Ipath/to/header/files
LIBS += -Lpath/to/lib/files -lmylibfile

It's all easy to google.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question