Johnny
Johnny

Reputation: 27

Can build Mysql Connector C++ on vs 2017?

I tried to build mysql-connector-c++ from source to static library.

Referring the official spec here, I generated the Visual studio solution by the following command:

cmake -DMYSQL_DIR="E:\mysql-5.7.21-winx64" -DWITH_BOOST="E:\libs\boost_1_67_0_BUILDED" -DWITH_JDBC=ON -G "Visual Studio 15 2017 Win64" ../

Then, I opened the vs solution generated by cmake.Run build, got the two libs: mysqlcppconn8-static-mt.lib, mysqlcppconn-static-mt.lib, and copied them to my project.

When I built my project (also vs2017 version), I got the error:

mysqlcppconn-static-mt.lib(net_serv.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1800”不匹配值“1900”(ActionProcessor.obj 中)

I am confused. They're all built in vs2017, and platform toolset are all set to "Visual Studio 2017 (v141)". I can't understand where 1800_MSC_VER has come from.

Upvotes: 2

Views: 2123

Answers (2)

Lefteris Eleftheriades
Lefteris Eleftheriades

Reputation: 331

Here is what I did

Specs

  • Visual Studio 15, (2017)
  • x64
  • Static (.lib)
  • With static runtime (/MT)

Tools

  • CMAKE
  • Bison (In a folder without spaces)
  • Perl (ActiveState or Strawberry)
  • OpenSSL 1.0.2q.tar.gz

Add CMAKE, Bison and pearl to your system path

Check to see if you got all setup correctly

C:\>cmake --version
cmake version 3.9.6

C:\>bison --version
bison (GNU Bison) 2.4.1

C:\>m4 --version
m4 (GNU M4) 1.4.13

C:\>perl -version
This is perl 5, version 24, subversion 3 (v5.24.3)

Step 1: Build OpenSSL 1.0.x

Note: if you want to build OpenSSL 1.1.x steps will be a bit different

Extract to: C:\mysql-8.0.15\openssl-1.0.2q

Run: x64 Native Tools Command prompt for VS 2017

cd C:\mysql-8.0.15\openssl-1.0.2q

Release

perl Configure VC-WIN64A no-shared --openssldir=.\rel64
ms\do_win64a
nmake -f ms\nt.mak
nmake -f ms\nt.mak install

Debug

nmake -f ms\nt.mak clean
perl Configure debug-VC-WIN64A no-shared --openssldir=.\dbg64
ms\do_win64a
nmake -f ms\nt.mak
nmake -f ms\nt.mak install

Step 2: Build MySQL (Yes the actual database)

Extract to: C:\mysql-8.0.15

Run: x64 Native Tools Command prompt for VS 2017

cd C:\mysql-8.0.15\

cmake . -G "Visual Studio 15 2017 Win64" ^
-DWITH_BOOST=C:/mysql-8.0.15/boost ^
-DDOWNLOAD_BOOST=1 ^
-DBUILD_CONFIG=mysql_release ^
-DWITH_SSL=C:\mysql-8.0.15\openssl-1.0.2q\rel64 ^
-DLINK_STATIC_RUNTIME_LIBRARIES=1

Copy m4.exe to C:\mysql-8.0.15\sql\ this avoids any m4.exe errors during built

Open MySQL.sln and build for release x64

Copy binary_log_funcs.h and binary_log_types.h from mysql-8.0.15\libbinlogevents\export to mysql-8.0.15\include

The library should be located at:

Static release library: C:\mysql-8.0.15\archive_output_directory\Release\mysqlclient.lib

Step 3: Build MySQL Connector C++

Copy C:\mysql-8.0.15-dbg\archive_output_directory\Debug\mysqlclient.lib to C:\mysql-8.0.15-dbg\lib\vs14

Run CMAKE GUI Browse to the path of the MySQL Connector Project.

Click Configure, Select Visual studio 15 2017 Win64, native compilers

Tick BUILD_STATIC 
Tick STATIC_MSVCRT
TICK WITH_JDBC
remove WIN_SSL_YASL from CONFIG_VARS
WITH_SSL = C:\mysql-8.0.15\openssl-1.0.2q\rel64
WITH_BOOST= C:\mysql-8.0.15\boost\boost_1_66_0
MYSQL_DIR=C:\mysql-8.0.15

Open MySQL_CONCPP.sln build for Release x64

The library is located at:

C:\mysql-connector-c++-8.0.15\jdbc\install\lib\mysqlcppconn-static-mt.lib
C:\mysql-connector-c++-8.0.15\Release\mysqlcppconn8-static-mt.lib

Step 4: Test it

Note: This test is for the JDBC style API. If you are not maintaining an existing application, use the new xdevapi API.

Create a new project and put the following in main.cpp

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#define STATIC_CONCPP
#define CONCPP_BUILD_STATIC
#define CPPCONN_PUBLIC_FUNC
#include <driver.h>
#include <connection.h>
#include <statement.h>
#include <exception.h>

sql::Driver *mDriver;
sql::Connection *mConnection;

int main() {

    mDriver = get_driver_instance(); //or use: new MySQL_Driver() 
    try {
        mConnection = mDriver->connect("host", "username", "password");
        mConnection->close();
    }catch(sql::SQLException& ex) {
        std::cout << ex.what();
    }
    delete mConnection;
    return 0;
}

Set: Linker > Input > Additional Dependecies: libeay32.lib;mysqlclient.lib;mysqlcppconn8-static-mt.lib;mysqlcppconn-static-mt.lib;ssleay32.lib;%(AdditionalDependencies)

Set: Configuration Properties > VC++ Directories

  • Include Directories.
  • Library Directories.

Upvotes: 2

Mecanik
Mecanik

Reputation: 1051

I don't even know where to start explaining to be honest. Building this was a massive pain.

Before you begin to read this, please ensure you have the following:

  • C:\boost
  • C:\OpenSSL-Win32
  • C:\OpenSSL-Win64

Because I was writing my own "wrapper" and trying to make UNICODE work properly, I was getting weird exceptions, and I was unable to debug anything because I downloaded the latest package which is built without debug information (https://dev.mysql.com/downloads/connector/cpp/)

This is the first time I try to build this, so I went to their Github repository and grabbed the latest "master": https://github.com/mysql/mysql-connector-cpp

BUT! This does not contain the "JDBC" sources, which is by the way the "native" connector as they call it. So I grabbed it from the branch: https://github.com/mysql/mysql-connector-cpp/tree/jdbc and then copied the files into "mysql-connector-cpp-master\jdbc".

OK. I opened "x64 Native Tools Command Prompt for VS 2017" and navigated to "mysql-connector-cpp-master". At this point I still had no idea what I am doing and how to build it, all I could understand from the docs and different articles here is to run:

cmake -DWITH_JDBC=ON --build .

This started to configure things and build OK, but it stopped here:

-- Searching for static libraries with the base name(s) "mysqlclient"
CMake Error at FindMySQL.cmake:524 (message):
  Could not find "mysql.h" from searching "/usr/include/mysql
  /usr/local/include/mysql /opt/mysql/mysql/include
  /opt/mysql/mysql/include/mysql /usr/local/mysql/include
  /usr/local/mysql/include/mysql C:\Program Files/MySQL/*/include
  C:/MySQL/*/include"
Call Stack (most recent call first):
  CMakeLists.txt:332 (INCLUDE)

OK. So what now... where is "mysql.h" ?! After searching... and searching, I found this: Missing mysql.h and trying to find mysql-devel

And I quote:

Grab the MySQL Community Server and install it on your system. It contains a directory called include in which you find the mysql.h.

Ok, great we need more stuff. So I download: https://dev.mysql.com/downloads/mysql/ and run the command again.

Happy days, something happened but no libs yet. I did get however a VS 2017 solution called: "MYSQLCPPCONN.sln".

This contains VS 2017 configuration to build the library for Win32. And of course trying to build with this solution will give you A LOT of unresolved external symbols.

So I run again the cmake but with static option added:

cmake -DWITH_JDBC=ON -DBUILD_STATIC=ON --build .

This time when I open "MYSQLCPPCONN.sln" I can see another project "mysqlcppconn-static" and this builds fine without issues.

However, in my case I need x64. I just created a new configuration for x64 by copying the exiting configuration fron Win32.

So that's it, I have both Win32 and x64 "mysqlcppconn-static.lib".

Overall Conclusion at this time:

  • Quite hard to build, without experience you will spend some time... time.
  • No proper documentation, their existing documentation is poorly
    written and very confusing.
  • No UNICODE support, yes the projects compile as Multi-Byte Character Set.

I will see if I can debug and find out why am I getting exceptions in the library when using this (from their documentation):

sql::ConnectOptionsMap connection_properties;

connection_properties["hostName"] = "";
connection_properties["port"] = "";
connection_properties["userName"] = "";
connection_properties["password"] = "";
connection_properties["schema"] = "";
connection_properties["OPT_CONNECT_TIMEOUT"] = 10;
connection_properties["CLIENT_MULTI_STATEMENTS"] = (true);
connection_properties["OPT_CHARSET_NAME"] = "utf8";
connection_properties["OPT_SET_CHARSET_NAME"] = "utf8";

I hope you can build your library with this information, and hopefully in the future it will become more easier and clear.

Good Luck.

Upvotes: 0

Related Questions