c++macoscmakemysql-connector

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

Solution

  • 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.