如何在Windows 10上添加连接器C ++并提升到Clion项目 [英] How to add connector c++ and boost to clion project on windows 10

查看:69
本文介绍了如何在Windows 10上添加连接器C ++并提升到Clion项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为C ++应用程序设置新服务器,并且需要与数据库进行交互.我需要包括连接器c ++并增强我的项目,但这不起作用.

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提取于: C:\ boost_1_71_0

Boost is extracted in: C:\boost_1_71_0

Mysql连接器安装在: C:\ Program Files \ MySQL \ MySQL Connector C ++ 8.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)

#include"mysql_driver.h" 表示未找到该文件.其他 .h 文件也是如此.

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

推荐答案

您的MySQL连接器路径包含空格,因此,在定义变量时,请用引号将路径括起来:

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")

此外,因为您使用过 include_directories()调用,包括带有 SOURCE_FILES 的标头似乎是多余的.如果您尝试将这些标头拖入IDE中,例如CLion,则最好单独列出每个标头(如对 .cpp 文件所做的那样).在 set 调用中将标头列出为 *.h 无效.

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.

根据您的反馈,我对进一步调整CMake文件提出了一些建议:

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

  1. 您可以使用 CMAKE_CXX_STANDARD 将C ++标准设置为C ++ 17,然后立即尝试使用 CMAKE_CXX_FLAGS 将其设置为C ++ 11.这些是矛盾的,因此只能选择一个.
  2. 提供给 add_executable()的文件仅需为源( .cpp )文件.标头将通过您的其他CMake调用包括在内.
  3. 使用 target_include_directories() 而不是 include_directories()将包含的目录仅应用于特定目标,而不应用于所有目标.这样,您的CMake所处理的其他目标就不会因为包含而拥挤不堪.
  4. 如果要将MySQL连接器库链接到可执行文件,请使用 target_link_libraries() 提供您要链接到的库的路径.
  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
)

这篇关于如何在Windows 10上添加连接器C ++并提升到Clion项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆