title: Setting Up Code::Blocks with MySql Connector on Windows 7 64bit
body: <p>Variations of this question has been asked many times before, but I've searched through everything I can find on the web and still don't have a valid answer, or at least one that works.</p>
  
  <p>I am trying to setup Code::Blocks to use MySql Connector in order to get the tutorial posted <a href="http://dev.mysql.com/tech-resources/articles/mysql-connector-cpp.html">here</a> to run. I have done everything to the best of my knowledge but still get an 'undefined reference' error on compile. I'm fairly certain it is a linking error.</p>
  
  <h2>Here is the error I get on compile:</h2>
  
  <pre><code>obj\Release\main.o:main.cpp:(.text.startup+0x15e)
  undefined reference to _imp__get_driver_instance' error: ld returned 1 exit status
  </code></pre>
  
  <h2>Here is my setup:</h2>
  
  <p>OS: Windows 7 64bit<br>
  IDE: Code::Blocks 16.01</p>
  
  <p><strong>Settings->Compiler->Search Directories->Compiler</strong>:  </p>
  
  <pre><code>C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\include  
  C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\include\cppconn  
  C:\local\boost_1_61_0  
  </code></pre>
  
  <p><strong>Settings->Compiler->Search Directories->Linker</strong>:  </p>
  
  <pre><code>C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\lib\opt  
  </code></pre>
  
  <p><strong>Settings->Compiler->Linker Settings->Link Libraries</strong>:  </p>
  
  <pre><code>C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\lib\opt\mysqlcppconn.lib  
  C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\lib\opt\mysqlcppconn-static.lib 
  </code></pre>
  
  <p>Like I said, there are lots of questions around the web about this particular error, but I haven't found anything with a solution for my setup. I admit I'm a little rusty on linking libraries but I swear I've tried every possible variation I could think up without success.</p>
  
  <h2>Here is the code:</h2>
  
  <pre><code>/* Standard C++ headers */
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;memory&gt;
  #include &lt;string&gt;
  #include &lt;stdexcept&gt;
  #include &lt;stdlib.h&gt;
  
  /* MySQL Connector/C++ specific headers */
  #include &lt;driver.h&gt;
  #include &lt;connection.h&gt;
  #include &lt;statement.h&gt;
  #include &lt;prepared_statement.h&gt;
  #include &lt;resultset.h&gt;
  #include &lt;metadata.h&gt;
  #include &lt;resultset_metadata.h&gt;
  #include &lt;exception.h&gt;
  #include &lt;warning.h&gt;
  
  #define DBHOST "removed"
  #define USER "removed"
  #define PASSWORD "removed"
  #define DATABASE "removed"
  
  #define NUMOFFSET 1
  #define COLNAME 1
  
  using namespace std;
  using namespace sql;
  
  static void retrieve_data_and_print (ResultSet *rs, int type, int colidx, string colname) {
  
      /* retrieve the row count in the result set */
      cout &lt;&lt; "\nRetrieved " &lt;&lt; rs -&gt; rowsCount() &lt;&lt; " row(s)." &lt;&lt; endl;
  
      cout &lt;&lt; "\nTestColumnName" &lt;&lt; endl;
      cout &lt;&lt; "--------" &lt;&lt; endl;
  
      /* fetch the data : retrieve all the rows in the result set */
      while (rs-&gt;next()) {
          if (type == NUMOFFSET) {
                         cout &lt;&lt; rs -&gt; getString(colidx) &lt;&lt; endl;
          } else if (type == COLNAME) {
                         cout &lt;&lt; rs -&gt; getString(colname) &lt;&lt; endl;
          } // if-else
      } // while
  
      cout &lt;&lt; endl;
  }
  
  int main(int argc, const char *argv[]) {
      Driver *driver;
      Connection *con;
      Statement *stmt;
      ResultSet *res;
      PreparedStatement *prep_stmt;
      Savepoint *savept;
  
      int updatecount = 0;
  
      /* initiate url, user, password and database variables */
      string url(argc &gt;= 2 ? argv[1] : DBHOST);
      const string user(argc &gt;= 3 ? argv[2] : USER);
      const string password(argc &gt;= 4 ? argv[3] : PASSWORD);
      const string database(argc &gt;= 5 ? argv[4] : DATABASE);
  
      try {
          driver = get_driver_instance();
  
          /* create a database connection using the Driver */
          con = driver -&gt; connect(url, user, password);
  
          /* turn off autocommit */
          con -&gt; setAutoCommit(0);
  
          cout &lt;&lt; "Database connection\'s autocommit mode = " &lt;&lt; con -&gt; getAutoCommit() &lt;&lt; endl;
  
          // select database schema
          con -&gt; setSchema(database);
  
          // create a statement object
          stmt = con -&gt; createStatement();
  
          cout &lt;&lt; "Executing Query: \"SELECT * FROM organizations\" ... " &lt;&lt; endl;
  
          /* run query */
          res = stmt -&gt; executeQuery ("SELECT * FROM organizations");
  
          cout &lt;&lt; "Retrieving the result set ..." &lt;&lt; endl;
          retrieve_data_and_print (res, NUMOFFSET, 1, string("TestColumnName"));
  
      } catch (SQLException &amp;e) {
          cout &lt;&lt; "ERROR: SQLException in " &lt;&lt; __FILE__;
          cout &lt;&lt; " (" &lt;&lt; __func__&lt;&lt; ") on line " &lt;&lt; __LINE__ &lt;&lt; endl;
          cout &lt;&lt; "ERROR: " &lt;&lt; e.what();
          cout &lt;&lt; " (MySQL error code: " &lt;&lt; e.getErrorCode();
          cout &lt;&lt; ", SQLState: " &lt;&lt; e.getSQLState() &lt;&lt; ")" &lt;&lt; endl;
  
          if (e.getErrorCode() == 1047) {
              /*
              Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)
              Message: Unknown command
              */
              cout &lt;&lt; "\nYour server does not seem to support Prepared Statements at all. ";
              cout &lt;&lt; "Perhaps MYSQL &lt; 4.1?" &lt;&lt; endl;
          }
  
          return EXIT_FAILURE;
      } catch (std::runtime_error &amp;e) {
  
          cout &lt;&lt; "ERROR: runtime_error in " &lt;&lt; __FILE__;
          cout &lt;&lt; " (" &lt;&lt; __func__ &lt;&lt; ") on line " &lt;&lt; __LINE__ &lt;&lt; endl;
          cout &lt;&lt; "ERROR: " &lt;&lt; e.what() &lt;&lt; endl;
  
          return EXIT_FAILURE;
      }
  
      return EXIT_SUCCESS;
  }
  </code></pre>