Here is my code executed on a Linux Ubuntu machine:
#include <iostream>
#include <memory>
#include "Logger.h"
#include "Config.h"
#include "mysql.h"
using namespace std;
int main() {
shared_ptr<Config> config = make_shared<Config>("WorldServer");
std::string def = "Nothing there.";
unsigned int prot = 3306;
Logger::Log( config->GetConfigValue("LoginDatabaseInfo", def));
const char * host = config->GetConfigValue("LoginDatabase.MySQL.IP", def).c_str();
const char * mysqlUser = config->GetConfigValue("LoginDatabase.MySQL.IP", def).c_str();
const char * mysqlPassword = config->GetConfigValue("LoginDatabase.MySQL.IP", def).c_str();
const char * mysqlDatabase = config->GetConfigValue("LoginDatabase.MySQL.IP", def).c_str();
static unsigned int mysqlPort = config->GetConfigValue("LoginDatabase.MySQL.Port", prot);
MYSQL *conn; /* pointer to connection handler */
MYSQL_RES *res; /* holds the result set */
MYSQL_ROW row;
/* INITIALIZE CONNECTION HANDLER, DO NOT CHANGE */
conn = mysql_init (NULL);
/* THIS CONNECTS TO SERVER, DO NOT CHANGE ANYTHING HERE */
mysql_real_connect (conn, host, mysqlUser, mysqlPassword,
mysqlDatabase, mysqlPort, NULL, 0);
/* show tables in the database (test for errors also) */
mysql_query(conn, "SELECT * FROM accounts");
res = mysql_store_result(conn);
// get the number of the columns
int num_fields = mysql_num_fields(res);
// Fetch all rows from the result
while ((row = mysql_fetch_row(res)))
{
// Print all columns
for(int i = 0; i < num_fields; i++)
{
// Make sure row[i] is valid!
if(row[i] != NULL)
cout << row[i] << endl;
else
cout << "NULL" << endl;
// Also, you can use ternary operator here instead of if-else
// cout << row[i] ? row[i] : "NULL" << endl;
}
}
// DON'T FORGET TO CLEAN RESULT AFTER YOU DON'T NEED IT
// ANYMORE
if(res != NULL)
mysql_free_result(res);
/* disconnect from server */
mysql_close (conn);
system("pause");
return 0;
}
When I try to compile I get the following error:
[ 83%] Linking CXX executable ../../../bin/AuthServer
CMakeFiles/AuthServer.dir/AuthServer.cpp.o: In function `main':
/home/venelin/Vibranium-Core/Source/Core/AuthServer/AuthServer.cpp:37: undefined reference to `mysql_init'
/home/venelin/Vibranium-Core/Source/Core/AuthServer/AuthServer.cpp:40: undefined reference to `mysql_real_connect'
/home/venelin/Vibranium-Core/Source/Core/AuthServer/AuthServer.cpp:43: undefined reference to `mysql_query'
/home/venelin/Vibranium-Core/Source/Core/AuthServer/AuthServer.cpp:44: undefined reference to `mysql_store_result'
/home/venelin/Vibranium-Core/Source/Core/AuthServer/AuthServer.cpp:48: undefined reference to `mysql_num_fields'
/home/venelin/Vibranium-Core/Source/Core/AuthServer/AuthServer.cpp:50: undefined reference to `mysql_fetch_row'
/home/venelin/Vibranium-Core/Source/Core/AuthServer/AuthServer.cpp:70: undefined reference to `mysql_free_result'
/home/venelin/Vibranium-Core/Source/Core/AuthServer/AuthServer.cpp:73: undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
Source/Core/AuthServer/CMakeFiles/AuthServer.dir/build.make:104: recipe for target 'bin/AuthServer' failed
make[3]: *** [bin/AuthServer] Error 1
CMakeFiles/Makefile2:209: recipe for target 'Source/Core/AuthServer/CMakeFiles/AuthServer.dir/all' failed
make[2]: *** [Source/Core/AuthServer/CMakeFiles/AuthServer.dir/all] Error 2
CMakeFiles/Makefile2:216: recipe for target 'Source/Core/AuthServer/CMakeFiles/AuthServer.dir/rule' failed
make[1]: *** [Source/Core/AuthServer/CMakeFiles/AuthServer.dir/rule] Error 2
Makefile:164: recipe for target 'AuthServer' failed
make: *** [AuthServer] Error 2
In my root CMakeLists.txt
I have the following:
cmake_minimum_required(VERSION 3.16)
project(Vibranium_Core)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost 1.72.0)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories(${Boost_INCLUDE_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(MySQL REQUIRED)
include_directories(${MYSQL_INCLUDE_DIR})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Common)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Core/WorldServer)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Core/AuthServer)
set_target_properties(
Common WorldServer AuthServer
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
In my Source/Core/AuthServer/CMakeLists.txt
I have the following:
if(WIN32)
if(MSVC)
list(APPEND PRIVATE_SOURCES AuthServer.rc)
endif()
endif()
add_executable(AuthServer AuthServer.cpp ${PRIVATE_SOURCES})
target_link_libraries(AuthServer LINK_PUBLIC Common)
if(NOT EXISTS ${CMAKE_BINARY_DIR}/bin/configs/)
message(STATUS "Configs folder will be created: ${CMAKE_BINARY_DIR}/bin/configs")
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin/configs/)
endif()
if(NOT EXISTS ${CMAKE_BINARY_DIR}/bin/configs/AuthServer.conf)
message(STATUS "New AuthServer Config file in will be installed in: ${CMAKE_BINARY_DIR}/bin/configs")
add_custom_command(
TARGET AuthServer POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/AuthServer.conf.dist
${CMAKE_BINARY_DIR}/bin/configs/)
endif()
I have this installed:
apt-get install libmysqlclient-dev
What I understand is that is some sort of linking error. How Can I fix it however ?