How to compile MySQL's Connector/c++ libraries to work in oF with Xcode

MySQL provides the Connector/C++ library for connecting to MySQL databases from a C++ application. On the downloads page, there is a link to a package with precompiled libraries for OS X. The problem is that they were compiled without a flag that is needed for an Xcode project to build properly.

To get it working, you need to do the following (Note that the version numbers of the library may change):

  1. Download and untar the source from here (select Source Code): http://dev.mysql.com/downloads/connector/cpp/

  2. Follow these instructions, but make sure that you read this StackOverflow answer.

  3. Use these terminal commands to build and install the library on your system:

    cd /path/to/mysql-connector-c+±1.1.6
    cmake -DMYSQL_CXXFLAGS=-stdlib=libc++ .
    make clean
    make VERBOSE=1
    make install

After that:

  1. In Xcode, click on the target of interest.
  2. Under “Linked Frameworks and Libraries” add libmysqlcppconn.7.1.1.6.dylib, which should be in /usr/local/lib.
  3. Under Target -> Build Settings -> Search Paths -> Header Search Paths, add /usr/local/include
  4. Under Target -> Build Settings -> Search Paths -> Library Search Paths, add /usr/local/lib

There are two MySQL complete examples that you can follow:

For my part, with openFrameworks, this is how I was able to test successfully.

ofApp.h

#pragma once

#include "ofMain.h"
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();	
};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    
    try {
    
    sql::Driver *driver;
    sql::Connection *con;
    sql::Statement *stmt;
    sql::ResultSet *res;
    sql::PreparedStatement *pstmt;
    
    driver = get_driver_instance();
    con = driver->connect("tcp://YOUR.SERVER.ADDRESS:3306", "YOUR USERNAME", "YOUR PASSWORD");
    con->setSchema("YOUR DATABASE NAME");
    stmt = con->createStatement();
    
    res = stmt->executeQuery("SELECT * FROM YOURTABLENAME ORDER BY ID LIMIT 5");
    
    while (res->next()) {
        cout << res->getInt64("YOUR COLUMN NAME") << endl; // type may need to be different
    }
    
    delete res;
    delete stmt;
    delete con;
        
    } catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }
}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){

}


2 Likes