Linking shared library mysql on ubuntu - c++

I've been trying to create a small test application to start working with CMake and linking libraries.
The following is done:
- I installed the libmysqlcppconn5 library on Ubuntu (ie. it is now located in /usr/lib/libmysqlcppconn5.so, just the default installation)
- I created a small .cpp file with the following content
#include <stdlib.h>
#include <iostream>
#include <mysql/mysql.h>
int main(int argc, char **argv) {
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
std::cout << "mysql init failed\n";
exit(1);
}
conn_ptr = mysql_real_connect (conn_ptr, "localhost", "root", "pw", "db", 0, NULL, 0);
if (conn_ptr) {
std::cout << "connection success\n";
} else {
std::cout << "connection failed\n";
}
mysql_close(conn_ptr);
How should I construct my CMakeLists.txt so it will use this shared library?

How about something like:
# Find and make sure the system have the header file
find_path(MYSQL_HEADER mysql/mysql.h)
if(MYSQL_HEADER STREQUAL "MYSQL_HEADER-NOTFOUND")
message(FATAL_ERROR "Could not find the mysql/mysql.h header file")
endif()
# Find and make sure the system have the library
find_library(MYSQL_LIBMYSQLCPPCONN5 mysqlcppconn5)
if(MYSQL_LIBMYSQLCPPCONN5 STREQUAL "MYSQL_LIBMYSQLCPPCONN5-NOTFOUND")
message(FATAL_ERROR "Could not find the mysqlcppconn5 library")
endif()
# Link the library to the target
target_link_libraries(your_target ${MYSQL_LIBMYSQLCPPCONN5})

Related

Linking external libraries in C++ in Visual Studio Code

Good afternoon! I have a following program in C++, in VS Code. The program reads all data from a table called student. I compile this program with the command g++ retrieveDataFromTable.cpp -I "C:\Program Files\MySQL\MySQL Server 8.0\include" -L "C:\Program Files\MySQL\MySQL Server 8.0\lib" -l mysql -o test, and it perfectly gets compiled. It's my first time that I use an external library in my programs. So, I watched some videos about how linking works, and how to add an external library to our program. Basically, in those videos they used Visual Studio or Codeblocks for adding libraries, but I'm using VS Code. Eventually, I managed to make the program work, but I have some questions. First one is about the way I include header file "mysql.h". I mean it does not look professional. If I were to run this program in other device, of course that would not make any sense. So would anybody like to help me out how I could make it better? My second question is that the program does terminate if I did not have libmysql.dll in my project folder. I'm guessing that it's because I do dynamic linking with the library (please, correct me if I'm wrong). So does anybody know about how I could link this particular library statically? In general, I would really appreciate if anyone would give me some piece of advice about how I could improve this program, and what I should learn to know about these kind of things. Thank you)
#include <iostream>
#include "C:/Program Files/MySQL/MySQL Server 8.0/include/mysql.h"
struct CONNECTION
{
const char *server = "localhost";
const char *user = "root";
const char *password = "password";
const char *database = "project";
};
MYSQL *connection_to_database(CONNECTION connection)
{
MYSQL *newConnection = mysql_init(NULL);
if (!newConnection)
{
std::cout << "Failed to create an object" << std::endl;
std::cout << mysql_error(newConnection) << std::endl;
exit(0);
}
if (!mysql_real_connect(newConnection, connection.server, connection.user,
connection.password, connection.database, 3306, NULL, 0))
{
std::cout << "Failed to connect to database:" << std::endl;
std::cout << mysql_error(newConnection) << std::endl;
exit(0);
}
return newConnection;
}
MYSQL_RES *execute_query(MYSQL *connection, const std::string query)
{
if (mysql_query(connection, query.c_str()))
{
std::cout << "MYSQL query error:\n"
<< mysql_error(connection) << std::endl;
exit(0);
}
return mysql_store_result(connection);
}
int main()
{
CONNECTION id;
MYSQL* connection = connection_to_database(id);
MYSQL_RES* res = execute_query(connection, "SELECT * FROM students");
int rows = mysql_affected_rows(connection);
std::cout << rows << " rows were affected" << std::endl;
int columns = mysql_num_fields(res);
std::cout << columns << " columns in the table" << std::endl;
MYSQL_ROW row;
while((row = mysql_fetch_row(res)))
{
for (int i = 0; i < columns; i++){
std::cout << row[i] << " ";
}
std::cin.get();
}
return 0;
}
I looked some questions but I could not find something useful.
How about using CMake?
You can easily link external libraries into your target executable.
On top of that, is it perfectly supported on vscode by CMake tools extension.
cmake_minimum_required(VERSION 3.25)
project(myProject)
include_directories(${CMAKE_SOURCE_DIR}/include)
link_directories(${CMAKE_SOURCE_DIR}/lib)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} libmysql.so)
Thank you guys for your answers! I managed to solve this problem, but I need to do some research why it actually worked (if you could help me I would really appreciate it). Basically I made three directories: build, include, lib. "include" and "lib" directories I copied from MYSQL Server 8.0 directory. The outputs of cmake are stored in "build" directory. I have the following command in CMakeLists.txt:
cmake_minimum_required(VERSION 3.25)
project(myProject)
include_directories(${CMAKE_SOURCE_DIR}/include)
link_directories(${CMAKE_SOURCE_DIR}/lib)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} mysql)
And it builds successfully. HOWEVER (!!!) when I try to run it, it just terminates. So I put libmysql.dll in "build" directory and then it ran. I'm really new to these things guys, and this solution of mine probably is not professional. If you guys could recommend me how to improve this procedure, or how to manage to make the program work without libmysql.dll file, I would really appreciate it.

Why won't music play using SDL

#include<iostream>
#include <SDL_mixer.h>
#include <SDL.h>
using namespace std;
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_AUDIO);
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
cout << "Error: " << Mix_GetError() << std::endl;
}
Mix_Music* song = Mix_LoadMUS("sample.wav");
Mix_PlayMusic(song, 1);
Mix_FreeMusic(song);
song = nullptr;
Mix_Quit();
SDL_Quit();
return 0;
}
I am trying to play a simple song, that I converted to WAV. It is located within the program's folder. I didn't get any errors/warnings when compling and the libraries seem to be linked correctly. On failure, loadMUS returns NULL (which isn't the case) and PlayMusic returns -1 upon failure (which again isn't the case). Yet, when I run the program nothing is heard. This is my first time working with SDL.
To just play a wave file this sample will work with SDL2:
#include <SDL.h>
#include <SDL_audio.h>
int main() {
SDL_Init(SDL_INIT_AUDIO);
SDL_AudioSpec spec{};
Uint8 *audio_buf{};
Uint32 audio_len{};
SDL_LoadWAV("path_to.wav", &spec, &audio_buf, &audio_len);
SDL_AudioSpec ob_spec{};
auto device = SDL_OpenAudioDevice(NULL, 0, &spec, &ob_spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
SDL_QueueAudio(device, audio_buf, audio_len);
SDL_PauseAudioDevice(device, 0);
SDL_Delay(5000);
SDL_CloseAudioDevice(device);
SDL_FreeWAV(audio_buf);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
BTW to link easy to SDL2 just use CMake with
cmake_minimum_required(VERSION 3.19)
project(sample_sdl VERSION 1.0)
find_package(SDL2 REQUIRED)
add_executable(${PROJECT_NAME} src/main.cpp)
set_property(TARGET ${PROJECT_NAME} PROPERTY LINKER_LANGUAGE CXX)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2 SDL2::SDL2main)

How to mix dynamic and static boost libraries in one cmake project?

I'm trying to link my C++ program with static Boost::program_options and dynamic Boost::log libraries.
Here is my CMakeList.txt file:
project(test)
add_executable(testprog "main.cpp")
add_definitions(-DBOOST_ALL_NO_LIB)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS program_options)
target_link_libraries(testprog PRIVATE Boost::program_options)
set(Boost_USE_STATIC_LIBS OFF)
add_definitions(-DBOOST_LOG_DYN_LINK)
find_package(Boost COMPONENTS log)
target_link_libraries(testprog PRIVATE Boost::log)
and my main.cpp file:
#define BOOST_ALL_NO_LIB
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include <boost/log/trivial.hpp>
int main(int argc, char* argv[])
{
namespace po = boost::program_options;
po::options_description desc("allowed arguments");
desc.add_options()("log", po::value<std::string>(), "log level");
po::variables_map options;
po::store(po::command_line_parser(argc, argv).options(desc).run(), options);
po::notify(options);
if (options.count("log")) {
std::cout << "log level: " << options["log"].as<std::string>() << std::endl;
}
BOOST_LOG_TRIVIAL(info) << "test message";
return 0;
}
I'm getting boost_filesystem.dll is missing from your computer error.
If I get rid of set(Boost_USE_STATIC_LIBS OFF) line, I'm getting unresolved external symbols boost::log.... Looks like Boost_USE_STATIC_LIBS can be set only once during the configuration time, but I just cannot find the way to tell cmake to split static/dynamic libraries...
Thanks.

Connecting to MySQL in C++

I am trying to learn C++ and I am having a bit of nightmare doing a test where I connect to a MySQL database.
I've had issues with the MySQL connector not linking properly then was getting issues related to relocation truncated to fitr_x86_64_32 against symbol.
I think I have fixed that by adding a compiler flag and now the app successfully builds and links.
When I run the app, it gets as far as calling get_driver_instance but then it exits. No exception is thrown, no errors nothing just exit code 0.
Below is my DBManager class
#include "DBConnectionManager.h"
using namespace std;
DBConnectionManager::DBConnectionManager() {
cout << "Starting DBConnectionManager - Updated" << endl;
try {
cout << "Getting driver instance" << endl;
driver = get_driver_instance();
cout << "Got driver instance" << endl;
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
conn->setSchema("bugs");
cout << "Connected to database" << endl;
}
catch (SQLException ex) {
cout << "Error connecting to DB: " << ex.what() << endl;
}
catch (...) {
cout << "Something has gone wrong" << endl;
}
}
Below is the header file
#ifndef MYSQLTEST_DBCONNECTIONMANAGER_H
#define MYSQLTEST_DBCONNECTIONMANAGER_H
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
using namespace sql;
class DBConnectionManager
{
private:
sql::Driver *driver;
sql::Connection *conn;
sql::Statement *statement;
sql::ResultSet *res;
public:
DBConnectionManager();
void performSql();
};
#endif //MYSQLTEST_DBCONNECTIONMANAGER_H
Below is my main method
#include "DBConnectionManager.h"
int main() {
DBConnectionManager dbConnectionManager;
dbConnectionManager.performSql();
return 0;
}
Below is my CMakeLists.txt file
cmake_minimum_required(VERSION 3.6)
project(MySQLTest)
include_directories("C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\include\\cppconn" "C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\lib\\opt")
SET(GCC_COVERAGE_LINK_FLAGS "-m64 -Wl,--image-base -Wl,0x10000000 -lpthread -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -Wl,--image-base -Wl,0x10000000 -lpthread -pthread ")
set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
add_executable(MySQLTest ${SOURCE_FILES})
add_library(mysqlcppconn.lib)
set_target_properties(MySQLTest PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(mysqlcppconn.lib PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(MySQLTest "C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\lib\\opt\\mysqlcppconn.lib")
When I create the instance of my DBConnectionManager class it successfully calls the query and prints Starting DBConnectionManager - Updated followed by Getting Driver Instance but then it exits with Process finished with exit code 0 with no clues as to what went wrong.
Update
I'm finally getting somewhere. I found there are some MySQL client libraries within Cygwin so I have download them and referenced them in the cmake file.
My cmake file now looks like this:
cmake_minimum_required(VERSION 3.6)
project(MySQLTest)
SET(CPPCONN_PUBLIC_FUNC=)
SET(GCC_COVERAGE_LINK_FLAGS "-g -m64 -DCPPCONN_PUBLIC_FUNC= -Dmysqlcppconn_EXPORTS -lpthread -pthread -Wl,--image-base -Wl,0x10000000 -lz")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCPPCONN_PUBLIC_FUNC= -Dmysqlcppconn_EXPORTS -std=c++11 -g -m64 -Wl,--image-base -Wl,0x10000000 -lpthread -pthread -lz")
include_directories("C:/mysql_connector/include")
include_directories("C:/boost_1_61_0")
set(BOOST_INCLUDE_DIR C:/boost_1_61_0)
set(BOOST_LIBRARY_DIR C:/boost_1_61_0/libs)
set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
add_executable(MySQLTest ${SOURCE_FILES})
find_package(Boost COMPONENTS REQUIRED)
link_directories(C:/mysql_connector/lib)
target_link_libraries(MySQLTest "C:/mysql_connector/lib/mysqlcppconn.dll" "C:/Program Files/MySQL/MySQL Server 5.7/lib/libmysql.dll" "C:/mysql_connector/lib/libmysqlclient.dll.a" "C:/mysql_connector/lib/libmysqlclient_r.dll.a" ${Boost_LIBRARY_DIR})
Notice how I have linked the libraries libmysqlclient.dll.a and libmysqlclient_r.dll.a which is what I got from Cygwin.
When I run the app now it successfully gets the driver instance and to the console is outputted
Starting DBConnectionManaged - Updated
Getting driver instance
Got driver instance
But when I try and connect with driver->connect I then get the following error
0 [main] MySQLTest 2976 C:\Users\Chris\.CLion2016.2\system\cmake\generated\MySQLTest-8702ae13\8702ae13\Debug\MySQLTest.exe: *** fatal error - Internal error: TP_NUM_C_BUFS too small: 50
When I put it through the debugger, it fails on the driver->connect with
gdb: unknown target exception 0xe06d7363 at 0x7fff11347788
Program received signal ?, Unknown signal.
0x00007fff11347788 in RaiseException () from /cygdrive/c/WINDOWS/System32/KERNELBASE.dll
Update 2
Everything I've read points the mysql connector binaries should work fine, so I started again. Below is now the contents of my cmake file
cmake_minimum_required(VERSION 3.6)
project(MySQLTest)
#add_compile_options("-v")
SET(GCC_COVERAGE_LINK_FLAGS )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_INCLUDE_DIR C:/boost_1_61_0)
set(BOOST_LIBRARY_DIR C:/boost_1_61_0/libs)
include_directories("C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/include" "C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/include/cppconn" ${BOOST_INCLUDE_DIR})
set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
add_executable(MySQLTest ${SOURCE_FILES})
find_package(Boost COMPONENTS REQUIRED)
link_directories(C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/lib/opt)
target_link_libraries(MySQLTest C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/lib/opt/mysqlcppconn.lib ${Boost_LIBRARY_DIR})
Now when I compile I get the original error
C:/Program Files/MySQL/MySQL Connector C++ 1.1.7/lib/opt/mysqlcppconn.lib(mysqlcppconn.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_get_driver_instance' defined in .idata$5 section in C:/Program Files/MySQL/MySQL Connector C++ 1.1.7/lib/opt/mysqlcppconn.lib(mysqlcppconn.dll.b)
That sounds like to me like my app is compiling as 32 bit instead of 64 bit. As a test I ran the following code:
cout << "Int size is: " << sizeof(int) << endl;
The code above prints 4 (shouldn't it be 8 if it was compiled as 64 bit).
If my thinking is correct, why isn't it compiling it as 64 bit, I've tried setting the compiler flag -m64 but makes no difference. I've installed the Cygwinx64 as well which CLion is using.
You can use MySQL Connector C++
This is how to configure using Cmake
cmake_minimum_required(VERSION 3.7)
project(projectname)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Include the dirs where you extracted the mysql-connector-cpp - download
include_directories(/usr/local/include/mysql-connector-cpp/include)
Create a cmake variable containing the project source files
set(SOURCE_FILES main.cpp)
Create your executable
add_executable(projectname ${SOURCE_FILES})
Link after creating executable
target_link_libraries(projectname mysqlcppconn)
Your CMakeList should at least have this or look like this in this order
cmake_minimum_required(VERSION 3.7)
project(projectname)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(/usr/local/include/mysql-connector-cpp/include)
set(SOURCE_FILES main.cpp)
add_executable(projectname ${SOURCE_FILES})
target_link_libraries(projectname mysqlcppconn)
You can use mysql++ library to connect to mysql from c++.
For that install mysql++,
(Ubuntu)
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysqlclient-dev libmysql++-dev libmysqlcppconn-dev
(Mac)
brew install mysql++
include library by adding,
/usr/include/mysql++
/usr/include/mysql
and add linkers,
-lmysqlpp -lmysqlclient
Example code,
#include <iostream>
#include <string>
#include <mysql++.h>
#include <mysql.h>
#define dbname "dbname"
#define server "localhost"
#define user "username"
#define pass "password"
using namespace std;
using namespace mysqlpp;
int main() {
Connection con(true);
try {
con.connect(dbname, server, user, pass);
cout << "Connected to database\n";
string s = "SELECT * FROM mirrors_mee WHERE id=1";
Query q = con.query(s);
StoreQueryResult sq = q.store();
StoreQueryResult::iterator it;
it = sq.begin();
while (it != sq.end()) {
Row row = *it;
cout << row[5] << " " << row[6] << " " << row[7] << endl;
it++;
}
} catch (Exception &e) {
cout << e.what() << endl;
}
return 0;
}
You will get full documentation of the library here.

include mysql into cmake

I'm writing a console application in kdevelop (integrated with cmake) in which I want to connect to mysql. I have installed libmysqlclient16-dev. My main.cpp file looks like this:
#include <stdlib.h>
#include <iostream>
#include <mysql/mysql.h>
int main(int argc, char **argv) {
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
std::cout << "mysql init failed\n";
exit(1);
}
conn_ptr = mysql_real_connect (conn_ptr, "localhost", "user", "pass", "db", 0, NULL, 0);
if (conn_ptr) {
std::cout << "connection success\n";
} else {
std::cout << "connection failed\n";
}
mysql_close(conn_ptr);
return 0;
}
and it compiles and works correctly, when I compile it manually:
g++ main.cpp -lmysqlclient -o main
But I want to include it into cmake somehow. The CMakeLists.txt, generated by kdevelop, looks like the following:
project(finances)
add_executable(finances main.cpp)
What should I add to cmake to make it include mysqlclient library?
target_link_libraries(finances mysqlclient)
Seems to work.
target_link_libraries(projectName mysqlclient)
Change projectName with your current project name