ReddaHawk
ReddaHawk

Reputation: 1

How to add connector c++ and boost to clion project on windows 10

I'm setting up a new server for a c++ application and i need to interact with a DB. I need to include connector c++ and boost to my project but this doesn't work.

Boost is extracted in: C:\boost_1_71_0

Mysql connector is installed in: C:\Program Files\MySQL\MySQL Connector C++ 8.0

cmake_minimum_required(VERSION 3.14)
project(shared-editor-server)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR C:/Program Files/MySQL/Connector C++ 8.0)
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc)
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc/cppconn)
link_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14)
set(
        SOURCE_FILES

        ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc/*.h
        ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc/cppconn/*.h
        ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14
)



add_executable(shared-editor-server ${SOURCE_FILES} main.cpp connectDB.cpp server.cpp server.h connectDB.h)

The #include "mysql_driver.h" says that the file is not found. The same for other .h files.

Upvotes: 0

Views: 1230

Answers (2)

Kevin
Kevin

Reputation: 18303

Your path to the MySQL connector contains spaces, so when you define the variable, enclose the path with quotes:

set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR "C:/Program Files/MySQL/Connector C++ 8.0")

Also, because you have used the include_directories() calls, including the headers with SOURCE_FILES appears redundant. If you are trying to pull these headers into an IDE, say CLion, it is best to list each header individually (as you have done with the .cpp files). Listing the headers as *.h in the set call will not work.


Based on your feedback, I have a few suggestions for further adjusting the CMake file:

  1. You set the C++ standard to C++17 with CMAKE_CXX_STANDARD, then immediately try setting it to C++11, with CMAKE_CXX_FLAGS. These are contradictory, so only choose one.
  2. The files provided to add_executable() only need to be the source (.cpp) files. The headers will be included via your other CMake calls.
  3. Use target_include_directories() instead of include_directories() to apply the included directories only to a specific target, not all targets. This way, other targets handled by your CMake don't get over-crowded with includes.
  4. If you want to link the MySQL Connector library to your executable, use target_link_libraries() to provide the path to the library you wish to link to.

Putting this together, you get something like this:

cmake_minimum_required(VERSION 3.14)
project(shared-editor-server)
# Choose only one of these standards.
set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR "C:/Program Files/MySQL/Connector C++ 8.0")

# You only really need to include your source files here.
add_executable(shared-editor-server 
    main.cpp 
    connectDB.cpp 
    server.cpp
)

# Add the MySQL include directories to this target.
target_include_directories(shared-editor-server PRIVATE 
    ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc
)
target_include_directories(shared-editor-server PRIVATE 
    ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc/cppconn
)

# Link the MySQL library to your executable.
target_link_libraries(shared-editor-server PRIVATE 
    ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14/mysqlcppconn8.lib
)

Upvotes: 1

Stranger
Stranger

Reputation: 107

If c++ mysql connector doesn't work for you, I suggest to read more about connector - compiler dependency.

> the code that uses Connector/C++ libraries should be built with the same compiler version as the connector itself. The information about compiler version used to build connector libraries can be found inside INFO_BIN file distributed with the connector.

I had issues in cLion just cuz of compiler, I've changed cLion on VS and it solved my problem.

Upvotes: 1

Related Questions