title: Connecting to MySQL in C++
body: <p>I am trying to learn C++ and I am having a bit of nightmare doing a test where I connect to a MySQL database. </p>
  
  <p>I've had issues with the MySQL connector not linking properly then was getting issues related to <code>relocation truncated to  fitr_x86_64_32 against symbol</code>. </p>
  
  <p>I think I have fixed that by adding a compiler flag and now the app successfully builds and links. </p>
  
  <p>When I run the app, it gets as far as calling get_driver_instance but then it exits. No exception is thrown, no errors nothing just exit code 0. </p>
  
  <p>Below is my DBManager class</p>
  
  <pre><code>#include "DBConnectionManager.h"
  
  using namespace std;
  
  DBConnectionManager::DBConnectionManager() {
      cout &lt;&lt; "Starting DBConnectionManager - Updated" &lt;&lt; endl;
      try {
          cout &lt;&lt; "Getting driver instance" &lt;&lt; endl;
          driver = get_driver_instance();
          cout &lt;&lt; "Got driver instance" &lt;&lt; endl;
          conn = driver-&gt;connect("tcp://127.0.0.1:3306", "root", "password");
          conn-&gt;setSchema("bugs");
          cout &lt;&lt; "Connected to database" &lt;&lt; endl;
      }
      catch (SQLException ex) {
          cout &lt;&lt; "Error connecting to DB: " &lt;&lt; ex.what() &lt;&lt; endl;
      }
      catch (...) {
          cout &lt;&lt; "Something has gone wrong" &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Below is the header file</p>
  
  <pre><code>#ifndef MYSQLTEST_DBCONNECTIONMANAGER_H
  #define MYSQLTEST_DBCONNECTIONMANAGER_H
  #include &lt;driver.h&gt;
  #include &lt;exception.h&gt;
  #include &lt;resultset.h&gt;
  #include &lt;statement.h&gt;
  
  using namespace sql;
  
  class DBConnectionManager
  {
  private:
      sql::Driver *driver;
      sql::Connection *conn;
      sql::Statement *statement;
      sql::ResultSet *res;
  public:
      DBConnectionManager();
      void performSql();
  };
  #endif //MYSQLTEST_DBCONNECTIONMANAGER_H
  </code></pre>
  
  <p>Below is my main method</p>
  
  <pre><code>#include "DBConnectionManager.h"
  
  int main() {
      DBConnectionManager dbConnectionManager;
      dbConnectionManager.performSql();
      return 0;
  }
  </code></pre>
  
  <p>Below is my CMakeLists.txt file</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.6)
  project(MySQLTest)
  
  include_directories("C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\include\\cppconn" "C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\lib\\opt")
  
  SET(GCC_COVERAGE_LINK_FLAGS    "-m64 -Wl,--image-base -Wl,0x10000000 -lpthread -pthread")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -Wl,--image-base -Wl,0x10000000 -lpthread -pthread ")
  
  set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
  add_executable(MySQLTest ${SOURCE_FILES})
  add_library(mysqlcppconn.lib)
  
  set_target_properties(MySQLTest PROPERTIES LINKER_LANGUAGE CXX)
  set_target_properties(mysqlcppconn.lib PROPERTIES LINKER_LANGUAGE CXX)
  
  target_link_libraries(MySQLTest "C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\lib\\opt\\mysqlcppconn.lib")
  </code></pre>
  
  <p>When I create the instance of my <code>DBConnectionManager</code> class it successfully calls the query and prints <code>Starting DBConnectionManager - Updated</code> followed by <code>Getting Driver Instance</code> but then it exits with <code>Process finished with exit code 0</code> with no clues as to what went wrong. </p>
  
  <h2>Update</h2>
  
  <p>I'm finally getting somewhere. I found there are some MySQL client libraries within Cygwin so I have download them and referenced them in the cmake file. </p>
  
  <p>My cmake file now looks like this:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.6)
  project(MySQLTest)
  
  SET(CPPCONN_PUBLIC_FUNC=)
  
  SET(GCC_COVERAGE_LINK_FLAGS    "-g -m64 -DCPPCONN_PUBLIC_FUNC= -Dmysqlcppconn_EXPORTS -lpthread -pthread -Wl,--image-base -Wl,0x10000000  -lz")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCPPCONN_PUBLIC_FUNC= -Dmysqlcppconn_EXPORTS -std=c++11 -g -m64 -Wl,--image-base -Wl,0x10000000  -lpthread -pthread  -lz")
  
  include_directories("C:/mysql_connector/include")
  include_directories("C:/boost_1_61_0")
  
  set(BOOST_INCLUDE_DIR C:/boost_1_61_0)
  set(BOOST_LIBRARY_DIR C:/boost_1_61_0/libs)
  set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
  
  add_executable(MySQLTest ${SOURCE_FILES})
  
  find_package(Boost COMPONENTS REQUIRED)
  
  link_directories(C:/mysql_connector/lib)
  
  target_link_libraries(MySQLTest "C:/mysql_connector/lib/mysqlcppconn.dll"  "C:/Program Files/MySQL/MySQL Server 5.7/lib/libmysql.dll" "C:/mysql_connector/lib/libmysqlclient.dll.a" "C:/mysql_connector/lib/libmysqlclient_r.dll.a" ${Boost_LIBRARY_DIR})
  </code></pre>
  
  <p>Notice how I have linked the libraries libmysqlclient.dll.a and libmysqlclient_r.dll.a which is what I got from Cygwin. </p>
  
  <p>When I run the app now it successfully gets the driver instance and to the console is outputted</p>
  
  <pre><code>Starting DBConnectionManaged - Updated
  Getting driver instance
  Got driver instance
  </code></pre>
  
  <p>But when I try and connect with driver->connect I then get the following error</p>
  
  <pre><code>0 [main] MySQLTest 2976 C:\Users\Chris\.CLion2016.2\system\cmake\generated\MySQLTest-8702ae13\8702ae13\Debug\MySQLTest.exe: *** fatal error - Internal error: TP_NUM_C_BUFS too small: 50
  </code></pre>
  
  <p>When I put it through the debugger, it fails on the driver->connect with</p>
  
  <pre><code>gdb: unknown target exception 0xe06d7363 at 0x7fff11347788
  
  Program received signal ?, Unknown signal.
  0x00007fff11347788 in RaiseException () from /cygdrive/c/WINDOWS/System32/KERNELBASE.dll
  </code></pre>
  
  <h2>Update 2</h2>
  
  <p>Everything I've read points the mysql connector binaries should work fine, so I started again. Below is now the contents of my cmake file </p>
  
  <pre><code>cmake_minimum_required(VERSION 3.6)
  project(MySQLTest)
  
  
  #add_compile_options("-v")
  
  SET(GCC_COVERAGE_LINK_FLAGS )
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  
  set(BOOST_INCLUDE_DIR C:/boost_1_61_0)
  set(BOOST_LIBRARY_DIR C:/boost_1_61_0/libs)
  
  include_directories("C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/include" "C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/include/cppconn" ${BOOST_INCLUDE_DIR})
  
  
  set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
  
  add_executable(MySQLTest ${SOURCE_FILES})
  
  find_package(Boost COMPONENTS REQUIRED)
  
  link_directories(C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/lib/opt)
  
  target_link_libraries(MySQLTest C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/lib/opt/mysqlcppconn.lib ${Boost_LIBRARY_DIR})
  </code></pre>
  
  <p>Now when I compile I get the original error</p>
  
  <pre><code>C:/Program Files/MySQL/MySQL Connector C++ 1.1.7/lib/opt/mysqlcppconn.lib(mysqlcppconn.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_get_driver_instance' defined in .idata$5 section in C:/Program Files/MySQL/MySQL Connector C++ 1.1.7/lib/opt/mysqlcppconn.lib(mysqlcppconn.dll.b)
  </code></pre>
  
  <p>That sounds like to me like my app is compiling as 32 bit instead of 64 bit. As a test I ran the following code:</p>
  
  <pre><code>cout &lt;&lt; "Int size is: " &lt;&lt; sizeof(int) &lt;&lt; endl;
  </code></pre>
  
  <p>The code above prints 4 (shouldn't it be 8 if it was compiled as 64 bit). </p>
  
  <p>If my thinking is correct, why isn't it compiling it as 64 bit, I've tried setting the compiler flag <code>-m64</code> but makes no difference. I've installed the Cygwinx64 as well which CLion is using. </p>