0

I'm learning C++ and CMake recently, but I'm having a little problem building a C++ demon that connects to the database. Please help me to see.

The problem is that when cmake --build ., the error is

$cmake ../src
...
$cmake --build .
Scanning dependencies of target MYSQL_CPP
[ 50%] Building CXX object CMakeFiles/MYSQL_CPP.dir/main.cxx.o
[100%] Linking CXX executable MYSQL_CPP
CMakeFiles/MYSQL_CPP.dir/main.cxx.o: In function `main':
main.cxx:(.text+0x7b): undefined reference to `mysql_init'
main.cxx:(.text+0x8f): undefined reference to `mysql_error'
main.cxx:(.text+0x104): undefined reference to `mysql_real_connect'
collect2: ld returned 1 exit status
gmake[2]: *** [MYSQL_CPP] Error 1
gmake[1]: *** [CMakeFiles/MYSQL_CPP.dir/all] Error 2
gmake: *** [all] Error 2


I downloaded the source code of mariadb-connector-c-3.0.9 and saved it in src/lib/

Here is file tree

.
|-- bin
`-- src
    |-- CMakeLists.txt
    |-- lib
    |   `-- gcc-4.1.2
    |       `-- mariadb-connector-c-3.0.9
    |           |-- bin
    |           |   `-- mariadb_config
    |           |-- include
    |           |   `-- mariadb
    |           |       |-- errmsg.h
    |           |       |-- ma_list.h
    |           |       |-- ma_pvio.h
    |           |       |-- ma_tls.h
    |           |       |-- mariadb
    |           |       |   `-- ma_io.h
    |           |       |-- mariadb_com.h
    |           |       |-- mariadb_ctype.h
    |           |       |-- mariadb_dyncol.h
    |           |       |-- mariadb_stmt.h
    |           |       |-- mariadb_version.h
    |           |       |-- mysql
    |           |       |   |-- client_plugin.h
    |           |       |   |-- plugin_auth.h
    |           |       |   `-- plugin_auth_common.h
    |           |       |-- mysql.h
    |           |       `-- mysqld_error.h
    |           `-- lib
    |               |-- mariadb
    |               |   |-- libmariadb.so -> libmariadb.so.3
    |               |   |-- libmariadb.so.3
    |               |   |-- libmariadbclient.a
    |               |   |-- libmariadbclient_static.a
    |               |   `-- plugin
    |               |       |-- auth_gssapi_client.so
    |               |       |-- caching_sha2_password.so
    |               |       |-- dialog.so
    |               |       |-- mysql_clear_password.so
    |               |       |-- remote_io.so
    |               |       `-- sha256_password.so
    |               `-- pkgconfig
    |                   `-- libmariadb.pc
    `-- main.cxx

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)

# set the project name
project(CRUD_CPP)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lib/gcc-4.1.2/mariadb-connector-c-3.0.9/include/)
LINK_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lib/gcc-4.1.2/mariadb-connector-c-3.0.9/lib/mariadb/)

add_executable(CRUD_CPP main.cxx)

main.cxx

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

const char *host = "XXX";
const char *user = "XXX";
const char *pwd = "XXX";
const char *db = "XX";
const int port = XX;

int main(){
      MYSQL *mysql = mysql_init(NULL);
      if(mysql==NULL){
          std::cout << "Eror:" << mysql_error(mysql) << std::endl;
          return 1;
      }

      mysql = mysql_real_connect(mysql, host, user, pwd, db, port, NULL, 0); //connect
      if(mysql){
          std::cout << "Success!" << std::endl;
      }
      else std::cout << "Failed!" << std::endl;
      return 0;
}
  • you need to link to `libmariadbclient`. The easiest option is probably to make use of `libmariadb.pc` via https://cmake.org/cmake/help/latest/module/FindPkgConfig.html – Alan Birtles Jul 16 '22 at 14:35
  • Thank you for the idea, I researched in this direction, but there is no new progress, can you give the code, please – James Maxwell Jul 16 '22 at 15:57

0 Answers0