c++ - 克利昂 : undefined "_get_driver_instance"

标签 c++ mysql undefined-symbol

集成开发环境:CLion
系统:OS X
错误信息:

Scanning dependencies of target librarySystem
[ 66%] Building CXX object    CMakeFiles/librarySystem.dir/sqlConnection.cpp.o
[ 66%] Building CXX object CMakeFiles/librarySystem.dir/main.cpp.o
[100%] Linking CXX executable librarySystem
Undefined symbols for architecture x86_64:
  "_get_driver_instance", referenced from:
  sqlConnection::sqlConnection() in sqlConnection.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see     invocation)
make[2]: *** [librarySystem] Error 1
make[1]: *** [CMakeFiles/librarySystem.dir/all] Error 2
make: *** [all] Error 2

我写了一个名为sqlConnection的类来连接mysql。

sql连接.h

#include "sqlConnection.h"

sqlConnection::sqlConnection() {
    driver = get_driver_instance();
    con = driver->connect("567aaffa1a70e.sh.cdb.myqcloud.com:xxxx", "xxxx", "xxxx");
    con->setSchema("librarySys");
    stmt = con->createStatement();
}

bool sqlConnection::ifConnected() {

bool isConnected = false;
if(!con->isClosed()){
    std::cout << "Succeed to connect mysql";
    isConnected = true;
}else{
    std::cout << "fail to connect mysql";
}
return isConnected;
}

sqlConnection::~sqlConnection() {
delete stmt;
delete con;
}

main.cpp中的测试 main.cpp

#include <iostream>
#include "sqlConnection.h"
using namespace std;

int main() {
sqlConnection *sqlC = new sqlConnection();
sqlC->ifConnected();
return 0;
}

制作 list :

cmake_minimum_required(VERSION 3.3)
project(librarySystem)
INCLUDE_DIRECTORIES(sqlFiles/include)
INCLUDE_DIRECTORIES(sqlFiles/lib)
INCLUDE_DIRECTORIES(sqlFiles/include/cppconn)
INCLUDE_DIRECTORIES(/usr/local/lib/libmysqlcppconn.so)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x")
set(SOURCE_FILES main.cpp sqlConnection.cpp sqlConnection.h)
add_executable(librarySystem ${SOURCE_FILES})

我用mysql connector-cpp连接mysql,但是问题来了,试过网上的解决方法,还是不行。

最佳答案

遇到同样的错误,我得到了一个小示例来处理此 CMakeLists.txt 内容。即使版本不同,也可能对您有所帮助。

cmake_minimum_required(VERSION 3.5)
project(TestCPP)

#For mysql connector include..
INCLUDE_DIRECTORIES(/mypath/mysql-connector-c++-1.1.7-osx10.10-x86-64bit/include/)

#For Boost..
INCLUDE_DIRECTORIES(/opt/local/include/)

#For imported linking..
add_library(libmysqlcppconn STATIC IMPORTED)

set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION /mypath/mysql-connector-c++-1.1.7-osx10.10-x86-64bit/lib/libmysqlcppconn-static.a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)

add_executable(TestCPP ${SOURCE_FILES})

target_link_libraries (TestCPP libmysqlcppconn)

关于c++ - 克利昂 : undefined "_get_driver_instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513122/

相关文章:

我可以从程序 (DLL) 调用的 C++ 或 C 编译器

C++:类中具有自定义大小的数组

c++ - 将 Boost.Filesystem 静态链接到共享库时出现问题

c++ - C++共享库中基类的 undefined symbol 错误

c++ - 如果在同一个范围内有同名的类和变量,如何指定类?

c++ - IOStream 库有哪些重要的替代方案? (除了 cstdio)

mysql - 使用 Google 电子表格应用程序脚本中的 MySQL 数据进行数据验证

PHP & MySQL 用户登录和权限系统

php - Eloquent:根据关系过滤记录

c++ - nm 符号 "U"和 "T",这是什么意思?