0

Basically I'm trying to connect mysql library in my "CMakeList.txt" file.

When i compile the "main.cpp" file the compiler gives this error:

C:/Users/Acer/Desktop/Uni/CP2/872539db-gr13-repo/Project/cmake-build-debug/Project.exe:
error while loading shared libraries: libmysql.dll: cannot open shared
object file: No such file or directory

Process finished with exit code 127

The point is the file "libmysql.dll" exist.

This is my "CMakeList.txt" file:

cmake_minimum_required(VERSION 3.21)
project(Project)

set(CMAKE_CXX_STANDARD 20)
set(FULL_PATH_TO_MYSQL_DIR ../Project/libraries/mysql/mysql-8.0.28-winx64)
include_directories(${FULL_PATH_TO_MYSQL_DIR}/include)
link_directories(${FULL_PATH_TO_MYSQL_DIR}/lib)
link_libraries(libmysql)

add_executable(Project
    main.cpp)

target_link_libraries(Project libmysql)

This is my "main.cpp" file:

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

MYSQL *connection;

class Dd_response
{
public:
    static void connectionFunction()
    {
        connection= mysql_init(nullptr);

        if(connection)
            std::cout<<"Database connected!!"<<std::endl;
        else
            std::cout<<"Failed to connect!!"<<mysql_errno(connection)<<std::endl;

        connection= mysql_real_connect(connection, "127.0.0.1", "root", "", "online_shop", 3306, nullptr, 0);

        if(connection)
            std::cout<<"Database connected to mysql!!"<<connection<<std::endl;
        else
            std::cout<<"Failed to connect!!"<<mysql_errno(connection)<<std::endl;
    }
};

int main()
{
    std::cout<<"Hello world";

    Dd_response::connectionFunction();

    return 0;
}

This is my "Project" folder:

Folder "Project"

wakey
  • 2,096
  • 3
  • 28
  • 52
Maximusrain
  • 23
  • 1
  • 6
  • On Windows for being able to run an executable you need to have `dll` files in the **same folder**. See [that question](https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake) about the ways how to do that. – Tsyvarev Apr 17 '22 at 22:22
  • Rn I have this error ``Error copying file (if different) from "../Project/libraries/mysql/mysql-8.0.28-winx64/lib/libmysql.dll" to "/cygdrive/c/Users/Acer/Desktop/Uni/CP2/872539db-gr13-repo/Project/cmake-build-debug". ninja: build stopped: subcommand failed.`` – Maximusrain Apr 17 '22 at 23:14
  • It is better to use **absolute path** to the copied file. You could always refer to the directory with the `CMakeLists.txt` via `${CMAKE_CURRENT_SOURCE_DIR}` – Tsyvarev Apr 17 '22 at 23:18
  • I fixed the problem by copying the dll in the same folder as the exe. Not a good solution but it works – Maximusrain Apr 17 '22 at 23:35

0 Answers0