This is my cpp code:
#include <iostream>
#include <mysql.h>
#define host "127.0.0.1"
#define username "root"
#define password ""
#define db "testdb"
int main() {
MYSQL *conn;
conn = mysql_init(NULL);
mysql_real_connect (conn, host, username, password, db, 0, NULL, 0);
MYSQL_RES *res_set;
MYSQL_ROW row;
unsigned int i;
mysql_query(conn, "SELECT * FROM Access");
res_set = mysql_store_result(conn);
unsigned int numrows = mysql_num_rows(res_set);
while ((row = mysql_fetch_row(res_set)) != NULL){
for (i=0; i<mysql_num_fields(res_set); i++)
printf("%s\n", row[i] != NULL ? row[i] : "NULL");
}
mysql_close(conn);
return 0;
}
I downloaded the libraries and trying to direct the program to them using CMakeLists.txt file
#Minimum cmake version required
cmake_minimum_required(VERSION 3.0)
SET ( MYSQL_ROOT "/home/napstar/Downloads/mariadb-connector-c-3.0.4-linux-x86_64")
SET (LIBCLIENT "/home/napstar/Downloads/libmysqlclient21_8.0.13-1ubuntu18.04_amd64")
#Project name
project (Access)
#Include subdirectories to be compiled
include_directories( ./app/src ./lib/src ${MYSQL_ROOT}/include/mariadb ${LIBCLIENT}/control ${LIBCLIENT}/data/usr/lib/x86_64-linux-gnu)
#The command to execute/build the project -NB OCR its a project name.
add_executable(Access ./app/src/main.cpp)
but i run into this errors:
Scanning dependencies of target Access
[ 50%] Building CXX object CMakeFiles/Access.dir/app/src/main.cpp.o
[100%] Linking CXX executable Access
CMakeFiles/Access.dir/app/src/main.cpp.o: In function `main':
main.cpp
.text+0xe): undefined reference to `mysql_init'
main.cpp
.text+0x44): undefined reference to `mysql_real_connect'
main.cpp
.text+0x5b): undefined reference to `mysql_query'
main.cpp
.text+0x67): undefined reference to `mysql_store_result'
main.cpp
.text+0x77): undefined reference to `mysql_num_rows'
main.cpp
.text+0x86): undefined reference to `mysql_fetch_row'
main.cpp
.text+0xa9): undefined reference to `mysql_num_fields'
main.cpp
.text+0x105): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
CMakeFiles/Access.dir/build.make:94: recipe for target 'Access' failed
make[2]: *** [Access] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Access.dir/all' failed
make[1]: *** [CMakeFiles/Access.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2