如何将C ++ MySQL连接器库链接到Cmake? [英] How do I link C++ MySQL Connector Libraries to Cmake?

查看:278
本文介绍了如何将C ++ MySQL连接器库链接到Cmake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CLion制作一个使用MySQL数据库处理数据的C ++程序.我已经下载了MySQL Connector和Boost,并将其链接到我的cmake文件.但是,当我尝试构建项目时,出现错误,提示对'_imp__get_driver_instance'的未定义引用".有人告诉我链接MySQL连接器库,但是由于我对C ++和使用此连接器还很陌生,所以我不知道该怎么做.

I am using CLion to make a C++ program that uses a MySQL database to handle data. I have downloaded the MySQL Connector and Boost and linked it to my cmake file. However, when I tried to build my project, an error appeared saying "undefined reference to `_imp__get_driver_instance'". Someone told me to link the MySQL Connector libraries, but since I am quite new to C++ and to using this connector, I have no idea on how to do this.

此刻,这是我的Cmake文件:

At the moment, this is my Cmake file:

cmake_minimum_required(VERSION 3.8)
project(Learn_Cpp)


set(CMAKE_CXX_STANDARD 17)

include_directories("C:/mysql-connector-c++-noinstall-1.1.9-win32/include" "C:/mysql-connector-c++-noinstall-1.1.9-win32/include/cppconn" "C:/boost_1_66_0")

set(SOURCE_FILES "C++ Tutorials/ClassFile.cpp" "C++ Tutorials/ClassFile.h" "C++ Tutorials/Learn.cpp")
add_executable(Learn_Cpp ${SOURCE_FILES})

有人知道我该如何解决这个问题?

Does anyone know how do I resolve this issue?

推荐答案

MySql文档针对

The MySql docs describe this extensively for Visual Studio and Netbeans. You need to do the respective thing for CMake.

一种快速而肮脏的方法是仅在CMakeLists.txt中对库的路径进行硬编码:

The quick and dirty way is to just hardcode the path to the library in your CMakeLists.txt:

target_link_libraries(Learn_Cpp c:/path/to/mysql/lib/mysqlcppconn.lib)

请注意,这将针对连接器的dll版本进行链接,因此,如果要在构建后运行程序,则需要将dll放置在exe可以找到它的目录中.除此之外,这应该可以工作,但实际上只能在您的计算机上使用特定的构建配置.为了使其更便携,更健壮,您可以使用 find_library 为磁盘上的配置找到正确的库文件.同样,您可以使用 find_path 来找到包含目录而不是对其进行硬编码,并使用 find_package 找到Boost .

Note that this will link against the dll version of the connector, so you need to place the dll in a directory where the exe can find it if you want to run your program after building. Other than that, this should work, but will really only work on your machine for a specific build configuration. To make it portable and more robust, you could use find_library to locate the correct library file for your configuration on disk. Similarly, you can use find_path to locate the include directories instead of hardcoding them and use find_package to locate Boost.

在现代CMake中,将find_pathfind_library调用中的MySql结果包装在

In modern CMake, it is also considered good style to wrap the results from your find_path and find_library calls for MySql in an imported target, similar to how FindBoost does it for Boost.

这篇关于如何将C ++ MySQL连接器库链接到Cmake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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