1

The IDE : CLion
System: OS X
Error message:

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

I write a class named sqlConnection to connect mysql.

sqlConection.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;
}

The test in the main.cpp main.cpp

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

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

cmakeList:

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})

I used mysql connector-cpp to connect mysql.But the problem came.Have tried the solution on web,but they did't work.

Rocky Zh
  • 11
  • 3
  • 2
    Possible duplicate of [C++ / mysql Connector - undefined reference to get\_driver\_instance - already tried the easy stuff](http://stackoverflow.com/questions/15995319/c-mysql-connector-undefined-reference-to-get-driver-instance-already-tri) – Piotr Król Dec 29 '15 at 14:58
  • I tried but it did't work. My system is os x,so I download fink to get apt-get but the result is :Reading Package Lists... Done Building Dependency Tree... Done E: Couldn't find package libmysqlcppconn-dev So it failed. – Rocky Zh Dec 29 '15 at 16:30
  • did you tried [this](https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-installation-source-unix.html) ? There also can be problem with your compilation flags, please provide your `CMakeLists.txt`. – Piotr Król Dec 29 '15 at 16:33
  • yes I have tried. But even the error message is same. – Rocky Zh Dec 29 '15 at 16:45
  • so you succeed with installing `libmysqlcppconn.so` ? Can you search you system, if this library exist ? Maybe this is just compilation flag needed in your Makefile. – Piotr Król Dec 29 '15 at 16:47
  • maybe you are right.I can't find libmysqlcppconn.so . cmakeLists.txt is already here – Rocky Zh Dec 29 '15 at 16:50
  • Please make sure you successfully installed library and that library is accessible in default dynamic library path for your OS. – Piotr Król Dec 29 '15 at 17:00
  • I can't find 'libmysqlcppconn.so'. And I don't know how to get it. Do you know how ? :) – Rocky Zh Dec 29 '15 at 17:32
  • Please follow [Installing Connector/C++ from Source on Unix and Unix-Like Systems](https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-installation-source-unix.html) that mention above. This is the way of installing libraries in your system. – Piotr Król Dec 29 '15 at 17:35
  • That mentioned 'mysql-connector-cpp' and I use this one.So during this,I got the same Error message – Rocky Zh Dec 29 '15 at 17:36
  • So you have to donwload and install [Connector/C](http://dev.mysql.com/downloads/connector/c/). According to [this](https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-installation-source-prerequisites.html): `Typically, the MySQL client library is installed when the MySQL Server is installed. However, check your operating system documentation for other installation options. Alternatively, you can install the client library by installing Connector/C.` – Piotr Król Dec 29 '15 at 17:39
  • I have download Connector/C++. I need to download C? – Rocky Zh Dec 29 '15 at 17:41
  • Yes. Please install Connector/C it contain required libraries. – Piotr Król Dec 30 '15 at 00:18
  • Now I need to move files in connector/C to where? Or what to do? I check my local mysql.In the lib, it do have `libmysqlclient` – Rocky Zh Dec 30 '15 at 01:23
  • Compile and install Connector/C. – Piotr Król Dec 30 '15 at 01:25
  • It seems like that did't work. I think I did something wrong in installing. `Installing Connector/C from a Binary Distribution` this step ,I just unpack the `tar` . `Installing Connector/C from Source on Unix and Unix-Like Systems` : first step`Change location to the top-level directory of the source distribution.` I did this : `cd path/to/librarySys' (my project) – Rocky Zh Dec 30 '15 at 02:35
  • Why you didn't use DMG package ? It should install just by double click on your system. – Piotr Król Dec 30 '15 at 10:21
  • I have installed Connector/C. The Error Message is same. – Rocky Zh Dec 31 '15 at 00:11
  • Did you searched for `libmysqlclient` ? Is it available ? – Piotr Król Dec 31 '15 at 00:36

1 Answers1

0

Struggled with the same error and I got a small example to work with this CMakeLists.txt content. Maybe it's helpful for you even if its not same versions.

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)
Mathieu
  • 8,840
  • 7
  • 32
  • 45