Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-18-2019, 03:33 AM   #1
LQ Newbie
Registered: Jan 2019
Posts: 1

Rep: Reputation: Disabled
Unhappy Hi all, i am new to cmake in linux so i am trying to design a c++ project that connects to mariadb using cmake, but i run into an error.

This is my cpp code:

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

#define host ""
#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;
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");

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Slackware-current: /usr/share/cmake and /usr/share/cmake-3.3 directories igor29768 Slackware 1 11-07-2015 12:37 AM
CMake Strange "Install" behaviour with home-grown software [Linux x86_64, CMake 2.8] ajschaeffer Programming 0 10-24-2011 03:21 AM
[SOLVED] Database design using IDs as opposed as Candidate keys as Primary key (UML design) angel115 Programming 1 07-27-2011 08:58 AM
cmake: Using find_package(Boost) when FindBoost.cmake is not in the default location damien_d Programming 3 10-27-2010 03:40 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:28 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration