c++ - 如何使用 MySQL Connector 和 Make 设置 C++ 项目

标签 c++ mysql cmake shared-libraries mysql-connector

尽管我已经用 C++ 开发应用程序大约两到三年了,但我从来不需要自己建立一个项目。大多数项目都是预先配置的,因此我从未学会自己做。此刻有空闲时间,我心想:“我要创建我自己的第一个 CMake C++ 项目”。

因为我知道我想在数据库中存储信息,所以我开始创建一个简单的 CMake 项目,其中一个可执行文件链接到 MySQL Connector for C++,但立即失败了...

由于我 - 奇怪的是 - 在其他地方找不到有用的信息,我希望你们中的一个人可以成为我的救星。

我的项目设置如下:

Root
- CMakeLists.txt
- build
- src
- tests
-- main.cpp
- include
-- mysql_connection.h
-- cppconn
--- driver.h
--- exception.h
--- resultset.h
--- statement.h
--- ...
-- ...
- libs
-- libmysqlcppconn.dylib -> libmysqlcppconn.7.8.0.12.dylib (symlink)
-- libmysqlcppconn.7.8.0.12.dylib
-- libmysqlcppconn8.1.8.0.12.dylib
-- libmysqlcppconn8.1.dylib  -> libmysqlcppconn8.1.8.0.12.dylib (symlink)
-- libmysqlcppconn8.dylib -> libmysqlcppconn8.1.8.0.12.dylib (symlink)
-- libssl.dylib
-- libcrypto.dylib
-- ...

我的main.cpp包含:

#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h> 
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

int main(int argc, char const *argv[])
{
    sql::Driver *driver;
    sql::Connection *con;

    driver = get_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306","root","securepw");
    return 0;
}

还有我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(EconSim)

add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
target_include_directories(EconSim PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(EconSim ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn.dylib)
target_compile_features(EconSim PUBLIC cxx_std_17)

我能够编译该应用程序,但在执行时出现以下错误:

dyld: Library not loaded: libmysqlcppconn.7.dylib
  Referenced from: /Users/aosterthun/Documents/Programming/EconSim/build/./EconSim
  Reason: image not found
Abort trap: 6

当在 CMakeLists.txt 中使用 libmysqlcppconn8.dylib 时: 还有我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(EconSim)

add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
target_include_directories(EconSim PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(EconSim ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn8.dylib)
target_compile_features(EconSim PUBLIC cxx_std_17)

我收到编译错误:

[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/aosterthun/Documents/Programming/EconSim/build --config Debug --target all -- -j 6
[build] [ 50%] Linking CXX executable EconSim
[build] Undefined symbols for architecture x86_64:
[build]   "_get_driver_instance", referenced from:
[build]       _main in main.cpp.o
[build] ld: symbol(s) not found for architecture x86_64
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[build] make[2]: *** [EconSim] Error 1
[build] make[1]: *** [CMakeFiles/EconSim.dir/all] Error 2
[build] make: *** [all] Error 2
[build] Build finished with exit code 2

我发现了这个:How do I link C++ MySQL Connector Libraries to Cmake?但遗憾的是这并没有解决我的问题。

我还发现了CLion: undefined “_get_driver_instance”这导致了这个CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(EconSim)

include_directories(${PROJECT_SOURCE_DIR}/include)
add_library(libmysqlcppconn STATIC IMPORTED)

add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn-static.a)
target_compile_features(EconSim PUBLIC cxx_std_17)
target_link_libraries(EconSim libmysqlcppconn)

这会导致此错误:

[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/aosterthun/Documents/Programming/EconSim/build --config Debug --target all -- -j 6
[build] [ 50%] Linking CXX executable EconSim
[build] Undefined symbols for architecture x86_64:
[build]   "_BIO_free", referenced from:
[build]       sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build]       caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build]   "_BIO_new_bio_pair", referenced from:
[build]       dummy_function_needed_by_xplugin() in libmysqlcppconn-static.a(my_aes_openssl.cc.o)
[build]   "_BIO_new_mem_buf", referenced from:
[build]       sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build]       caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build]   "_BN_bin2bn", refer...

如果有任何帮助,我将不胜感激。甚至我还没有找到一个明显的解决方案的提示。我仍然有点困惑为什么我没有找到有关该主题的有用信息。因为应该有很多人使用这项技术进行开发。

如果我遗漏了任何信息,请询问更多信息。

最佳答案

您需要安装 openssl 和链接库 libcrypto、libssl、libresolv。 我解决这个问题的方法如下:

target_link_libraries(${PROJECT_NAME}
PUBLIC
    mysqlclient
    mysqlcppconn-static
    crypto
    ssl
    resolv
)

关于c++ - 如何使用 MySQL Connector 和 Make 设置 C++ 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51956116/

相关文章:

c++ - 可变参数模板的模拟

php - 使用 PhP 通过 SQL 进行搜索

mysql - 如何将参数传入和传出到mysql存储过程并在nodejs代码中返回存储过程结果

c++ - 我可以为迭代器赋值吗?

c++ - ffmpeg AVFrame 获取完整的解码数据到 char*

c++ - Omnet 和 Inet 链接错误 : undefined reference to typinfo

mysql - AWS WordPress 高可用性 EFS、EC2 和 MySQL 数据库

cmake - 强制串行执行 CMake 中的特定目标

docker - 在 Docker/CMake 项目中包含外部库

c++ - 动态链接错误cmake