尽管已经用C ++开发了应用程序大约2至3年,但我从来不必自己建立项目。大多数项目都是预先配置的,所以我从来没有学会自己做。现在有空余时间,我对自己说:“我将创建自己的第一个CMake C ++项目”。
因为我知道我想将信息存储在数据库中,所以我开始创建一个简单的CMake项目,该项目的一个可执行文件链接到C ++的MySQL连接器,但立即失败了...
由于我-很奇怪-在其他任何地方都找不到有用的信息,所以我希望你们中的一个可以成为我的救星。
我的项目设置如下:
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.dylib
中使用CMakeLists.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
我发现了这个问题: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...
我将不胜感激。甚至没有找到我没有找到的明显解决方案的提示。我仍然有些困惑,为什么我找不到有关此主题的有用信息。因为应该有很多人在使用这项技术进行开发。
如果我想念任何东西,只是要求提供更多信息。
答案 0 :(得分:1)
您需要安装openssl和链接库libcrypto,libssl,libresolv。 我按照以下方式解决此问题:
target_link_libraries(${PROJECT_NAME}
PUBLIC
mysqlclient
mysqlcppconn-static
crypto
ssl
resolv
)