如何使用mysql连接器设置c++项目并使

gz5pxeao  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(430)

尽管我已经用c开发了大约2到3年的应用程序,但我从不需要自己设置一个项目。大部分项目都是预先配置的,所以我从来没有学会自己做。有空的时候,我对自己说:“我要创建我自己的第一个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

而不是使用 libmysqlcppconn8.dylibCMakeLists.txt :还有我的 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

我发现:如何将c++mysql连接器库链接到cmake?但遗憾的是,这并没有解决我的问题。
我还发现clion:undefined“\u get\u driver\u 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...

如果有任何帮助,我将不胜感激。即使是一个明显的解决办法的暗示我也没有找到。我仍然有点困惑为什么我没有找到关于这个主题的有用信息。因为应该有很多人在用这项技术发展。
如果我漏掉了什么,请询问更多的信息。

6rqinv9w

6rqinv9w1#

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

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

相关问题