2

I have been trying to develop a simple c++ program to connect to mysql using the mysql connector/c++ library. I have been running into issues after issues. This is the next hurdle. I am new to CMake & Make, but have been trying to use it to build this project. I've downloaded all the recent packages, dependencies, softwares, etc.: MySQL 9.3, MySQL Connector/C++ 9.3, OpenSSL@3. I am working on my Mac terminal.

My program: /Users/{user}/Documents/Projects/SQL/src/main.cpp

#include <iostream>
#include "/usr/local/mysql-connector-c++-9.3.0/include/mysql/jdbc.h"

using std::cout;

int main(){
  sql::mysql::MySQL_Driver *driver;
  driver = sql::mysql::get_mysql_driver_instance();

  cout<<"Hello"<<"\n";
  return 0;
}

CMake file (CMakeLists.txt): /Users/{user}/Documents/Projects/SQL/

cmake_minimum_required(VERSION 4.0.1)

project(mysql_tut)
#set some path variables 
set(MYSQL_PATH "/usr/local/mysql-connector-c++-9.3.0/")
set(MYSQL_LIB_PATH "${MYSQL_PATH}lib64/")

#create a variable to hold all necessary library files
file(GLOB LIB_FILES
   "${MYSQL_LIB_PATH}libmysqlcppconn.10.9.3.0.dylib"
   "${MYSQL_LIB_PATH}libcrypto.3.dylib"
   "${MYSQL_LIB_PATH}libssl.3.dylib"
)

#set std=c++11 standard needs to run during compilation
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

#set path for builder to look for cmake configure file "mysql-conncpp-config.cmake"
set(CMAKE_PREFIX_PATH "${MYSQL_PATH}")

#find library's cmake config file "mysql-conncpp-config.cmake"
find_package(mysql-conncpp REQUIRED)

#set target and source file
add_executable(main src/main.cpp)

#set target library files
target_link_libraries(main PRIVATE "${LIB_FILES}")

#set target include directory
target_include_directories(main PRIVATE "${MYSQL_PATH}include/mysql")

The MySQL connector library was installed via .dmg in the directory /usr/local/, containing the respective subdirectories for the include files, library files, cmake configuration files, etc.

I created a build directory (same level as the src) /Users/{user}/Documents/Projects/SQL/build/, and changed into it. There, I ran cmake .. command for the Cmake file to generate the make file in the build directory. Following, I ran the make command and the project was built successfully. However, when I try to execute the binary file ./main I receive this error:

dylb[44606] Library not loaded: libcrypto.3.dylib
  Reference from: <57D4XXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX> /Users/{user}/Documents/Projects/SQL/build/main
  Reason: tried: 'libcryto.3.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcrypto.3.dylib'(no such file), 
'libcrypto.3.dylib' (no such file), '/Users/{user}/Documents/Projects/SQL/build/libcrypto.3.dylib' 
(no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/{user}/Documents/Projects/SQL/build/libcrypto.3.dylib' 
(no such file), /Users/{user}/Documents/Projects/SQL/build/libcrypto.3.dylib' (no such file)
zsh: abort ./main

In certain cases, I get an error that says no such files for libssl.3.dylib. I am not sure what is going on, because I added in the cmake file, the library files to the target_link_libraries command. I have checked my OpenSSL and I have the latest installed via homebrew at /opt/homebrew/Cellar/. What can be causing this issue?

0

1 Answer 1

3

After add executable link the library using target_link_libraries(main mysql::concpp) . You don't need to find the library files or define the include directory for this. fund_package ensures that they are populated for your project.

Sign up to request clarification or add additional context in comments.

2 Comments

I edited the cmake file with your suggestion and went to build the project, then I received a linking error. Which is caused by the functioned I called in my program get_mysql_driver_instance(). I don't get that linking, when I add those files in the target link libraries command.
dev.mysql.com/doc/dev/connector-cpp/latest/usage.html : This is an official document providing how to build a project with the library from the mysql website. It is congruent with your suggestion, but it is not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.