gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:48:07 28 4
gpt4 key购买 nike

尽管我已经用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com