MySQL++ with Eclipse CDT on Linux/Ubuntu

After a whole day of figuring out how to get a stupid “select item from stock” running with MySQL++ and Eclipse CDT I decided to write it down for everyone else having the same problem.

  1. Get the MySQL++ development-files. You might load them from here but it might be easier to just get them via
    apt-get libmysql++-dev

    or synaptic (I’m working with Ubuntu). Make also sure to have mysql-client-x.x (just in case…)

  2. Start Eclipse. Create a new C++ project (executable|empty project)  and add a “source folder” named “src”.
  3. Create a new *.c and *.h file with following contents:
    #include 
     
    #include "mysql++.h"
     
    using namespace std;
     
    int main() {
     
    	mysqlpp::Connection conn(false);
    	if (conn.connect("sample", "localhost", "root", "rootpw")) {
    		// Retrieve a subset of the sample stock table set up by resetdb
    		// and display it.
    		mysqlpp::Query query = conn.query("select item from stock ");
    		if (mysqlpp::StoreQueryResult res = query.store()) {
    			cout << "We have:" << endl;
    			for (size_t i = 0; i < res.num_rows(); ++i) {
    				cout << '\t' << res[i][0] << endl;
    			}
    		} else {
    			cerr << "Failed to get item list: " << query.error() << endl;
    			return 1;
    		}
     
    		return 0;
    	} else {
    		cerr << "DB connection failed: " << conn.error() << endl;
    		return 1;
    	}
     
    	return 0;
    }
  4. Now for the tricky bit (I spent a whole day searching for something called “linker” or “includes” but Eclipse decided to just name it “directories”…): Right click your project and choose “properties”. Under “C/C++ Build|Settings|Tool Settings|GCC C++ Compiler|Directories” you will find an empty field. Click “add” and include “/usr/include/mysql++” and “/usr/include/mysql”. Apply.
  5. Hit the “hammer” icon (“build ‘debug’ for …”), choose “run configurations” under the run icon and type into the textfield names “C/C++ Application” “Debug/MyProjectName”. Apply.
  6. Click “Run” – You will now hopefully have a running “Hello World” for C++ with MySQL.

Leave a Reply