Babra Cunningham
Babra Cunningham

Reputation: 2967

Running mysql-connector with CMake and C++?

I can't seem to find a solution to my CMake problem.

I want to use mysql-connector-c++ and CURL, in my /Desktop/example directory I've made the following CMakeLists.txt:

cmake_minimum_required (VERSION 3.1.0)
PROJECT (main)
ADD_EXECUTABLE (main main.cpp)
target_link_libraries(main curl mysql-connector-c++)
target_compile_features(main PRIVATE cxx_range_for)

I've included the following headers in my main.cpp:

#include <curl/curl.h>

#include <mysql_connection.h>
#include <driver.h> 
#include <exception.h>
#include <resultset.h>
#include <statement.h>

I've downloaded the zip file for mysql-connector-c++, unzipped and placed this in my current directory.

I've then run cmake . and make and the following error is generated:

fatal error: 
  'mysql_connection.h' file not found

This is bizarre as when I remove the mysql-connector headers, CURL works fine.

Any Idea's as to why this is not working?

N.B The following commands run with the stated result:

g++ -std=c++14 -lcurl main.cpp  //Success
g++ -std=c++14 -lcurl -lmysqlcppconn main.cpp //Fatal eror: 'mysql_connection.h' file not found

Upvotes: 1

Views: 10837

Answers (2)

Y.nouri
Y.nouri

Reputation: 11

first install mysql library on you mac with

brew install mysql-connector-c++

and then use the above cmake text

cmake_minimum_required(VERSION 3.17)
project(connectToMysql)
set(CMAKE_CXX_STANDARD 14)
add_executable(connectToMysql main.cpp)
include_directories(/usr/local/Cellar/mysql-connector-c++/8.0.25/include)
target_link_libraries(connectToMysql -L/usr/local/Cellar/mysql-connector-c++/8.0.25/lib/)
 target_link_libraries(connectToMysql libmysqlcppconn8.2.8.0.25.dylib)

keep in mind that if you want to link a library to cmake you have to specify the name of that .dylib file

Good luck

Upvotes: 1

fedepad
fedepad

Reputation: 4609

I believe that CMake doesn't know about that directory which contains header and library as an "include" or "library" directory. One solution that you can try is to specify the include and link directories before the call

ADD_EXECUTABLE (main main.cpp)  

so your CMakeLists.txt should look like:

cmake_minimum_required (VERSION 3.1.0)
project(main)
set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR your_path_to_mysql_connectorcpp)
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include)
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/cppconn)
link_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib)
add_executable(main main.cpp)
target_link_libraries(main curl mysqlcppconn)
target_compile_features(main PRIVATE cxx_range_for)  

Few notes:

1) If you want to call driver.h in your sources then you should use the CMakeLists.txt above;

2) If you want to call cppconn/driver.h in your sources then you should use the CMakeLists.txt below:

cmake_minimum_required (VERSION 3.1.0)
project(main)
set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR your_path_to_mysql_connectorcpp)
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include)
link_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib)
add_executable(main main.cpp)
target_link_libraries(main curl mysqlcppconn)
target_compile_features(main PRIVATE cxx_range_for)  

As your CMake will get more complex, you could also define include directories and libraries on a per target basis, but the above should be ok for the stuff you have now.

Upvotes: 4

Related Questions