N
N
Nerd0_02019-04-27 08:48:17
C++ / C#
Nerd0_0, 2019-04-27 08:48:17

How to include a library in C++?

I connect the header to the project like this:

#include <iostream>
#include <mariadb/mysql.h>

int main(){
    MYSQL conn;
    if(!mysql_init(&conn)){
        std::cout<<"Error create MySQL descriptor";
    }
    if(!mysql_real_connect(&conn,"localhost","stats","q1w2e3r4","statsDB",3306,NULL,0)){
        std::cout<<"Error";
    std::cout<< "Успешно";
    return 0;
}

IDE CLion is able to go inside the header, that is, everything is connected. However, when trying to build make swears:
CMakeFiles/ISPstats.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `mysql_init'
main.cpp:(.text+0x9b): undefined reference to `mysql_real_connect'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/ISPstats.dir/build.make:94: recipe for target 'bin/ISPstats' failed
make[3]: *** [bin/ISPstats] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/ISPstats.dir/all' failed
make[2]: *** [CMakeFiles/ISPstats.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/ISPstats.dir/rule' failed
make[1]: *** [CMakeFiles/ISPstats.dir/rule] Error 2
Makefile:118: recipe for target 'ISPstats' failed
make: *** [ISPstats] Error 2

Almost no experience with ++, perhaps I don’t see something completely obvious to you. Such a problem is not only with this library, but in general very often I have it, but I don’t know how to fix it. Maybe you are leaving articles to read on this topic.
I found a code example here , but Cmake swears like this:
CMake Error at CMakeLists.txt:17 (find_package):
  By not providing "FindMariaDBClient.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "MariaDBClient", but CMake did not find one.

  Could not find a package configuration file provided by "MariaDBClient"
  with any of the following names:

    MariaDBClientConfig.cmake
    mariadbclient-config.cmake

  Add the installation prefix of "MariaDBClient" to CMAKE_PREFIX_PATH or set
  "MariaDBClient_DIR" to a directory containing one of the above files.  If
  "MariaDBClient" provides a separate development package or SDK, be sure it
  has been installed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2019-04-27
@res2001

1. The library must be installed on the system so that cmake can find it. It is best to use the regular OS package manager. In addition to the library, you also need to install dev packages for it (these are the headers). dev packages are needed just to be able to build your own (or not your own) code using the library. Some Linux distributions may not have dev packages. In Windows they are not the same, MSVS has its own package manager - vcpkg.
2.You must force cmake to find the library, this is done with the find_package or find_library commands.
find_package is a more advanced version, but it requires the find module. For many popular libraries, cmake contains ready-made modules. You can search for them hereby Find<library name>. There is no ready-made module for the MySQL client. You can google, I think it will not be difficult to find a ready-made module and use it in your project.
find_library - simply finds the library in the standard paths. But for assembly, this is usually not enough, because. header files are also needed and if the package manager puts them in specific paths, they will not be available. So it's better to use find_package - it looks for everything.
3. Add the path to the library header files to the project using the command: target_include_directories
The variable containing the path must be defined in step 2 (or you set the path manually)
4. Add the library to the link list using the command: target_link_libraries
Similarly, you need to use variables, which will be defined in paragraph 2.
There are various nuances in this process that may affect the tools used, for example, what to do if the library is not in the OS repository or it is distributed only in the form of source codes.
As a result, all manipulations lead to the fact that the compiler is given the options -I (to include the directory with the library header files), -l and possibly -L - to include the library in the linking list.
All this can be done manually by setting the necessary options for the compiler using add_compile_options and add_link_options - but this will lead to the fact that most likely on another computer or in another OS you will have to edit cmakelist.txt for assembly.

V
Vitaly, 2019-04-27
@vt4a2h

If you are using conan, then:
1) Do you have all the necessary dependencies in conanfile.txt?
2) Have you connected the necessary repositories to conan?
3) Did you run conan install?
Your assembly file is more or less correct, but check what I wrote above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question