Entries filed under C/C++

MySQL Verbindungen in C++ mit mysqlpp

Posted on 6. November 2013 Comments

MySQL Server und Header installieren:

sudo apt-get install mysql-server  libmysqlclient-dev libmysql++-dev

In Eclipse C++-Projekt anlegen, Rechtsklick auf das Projekt:

Properties>C/C++-Build>Settings>Tool Settings>GCC C++ Compiler>Includes
Dort die Pfade /usr/include/mysql und /usr/include/mysql++ als Include paths eintragen.

cpp_mysql1

 

Weiter unten unter GCC C++ Linker>Libraries>Libraries „mysqlpp“ eintragen und unter Library search paths ebenfalls /usr/include/mysql und /usr/include/mysql++.

cpp_mysql2

 

Unter Miscellaneous dann noch bei Other objects die Datei /usr/lib/x86_64-linux-gnu/libmysqlclient.so angeben:

cpp_mysql3

 

Beispieldatei: mysqlpptest.cpp

 

Credits: C++ with a shot of MySQL on Ubuntu 10.10

ActiveMQ mit C++ und Eclipse

Posted on 6. November 2013 Comments

Vorraussetzungen

Vorbereitung

sudo apt-get update && apt-get upgrade && apt-get install build-essential autoconf libaprutil1-dev libapr1-dev

cd ~/Development

wget http://mirror.softaculous.com/apache/activemq/activemq-cpp/source/activemq-cpp-library-3.4.5-src.tar.gz

tar -xvzf activemq-cpp-library-3.4.5-src.tar.gz

cd activemq-cpp-library-3.4.5-src

./configure

make

ActiveMQ-CPP

Dann mit sudo make install auf dem lokalen Rechner installieren. Damit befinden sich dann die .so-Dateien in /usr/local/lib.

In Eclipse

  1. Neu -> C++ Project
  2. Rechtsklick Projekt -> Properties
  3. C/C++ -> Build -> Settings -> GCC C++ Compiler -> Includes
  4. Unter include paths Pfad von ActiveMQ-CPP: /home/user/workspace/activemq-cpp-3.4.5/activemq-cpp/src/main hinzufügen oder anpassen
  5. Dort auch Pfad von APR hinzufügen: /usr/include/apr-1.0
  6. Die beiden auch beim Linker als library search path hinzufügen oder anpassen
  7. Als shared Object beim Linker /usr/local/lib/libactivemq-cpp.so hinzufügen

Beispiele

Die Projekte unter activemq-cpp-3.4.5/activemy-cpp/src/examples lassen sich jetzt kompilieren, allerdings kommt beim starten folgende Fehlermeldung:

./activeMQProducer: error while loading shared libraries: libactivemq-cpp.so.14: 
cannot open shared object file: No such file or directory

Der Fehler lässt sich mit folgendem Befehl beheben(credit):

export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}

Diesen Artikel hatte ich bereits vor einem Jahr(Ende 2012) geschrieben. Vielleicht gibt es neue Versionen der benutzen Software.

Dining Philosophers with monitor using pthread mutex and condition variables in C

Posted on 24. Mai 2012 Comments

I had to do this for uni a while ago so I thought I might as well publish it. The task was to implement the Dining philosophers problem in C. We had to use the monitor concept which is implemented with mutual exclusion (mutex) from the pthread-library because C lacks a built-in monitor function in contrary to e.g. Java, Python or Delphi. Bascially this just means, the critical things have to go between pthread_mutex_lock(&mutex) and pthread_mutex_unlock(&mutex) The communication from one philosopher to another was realized with condition variables(cond-vars). The realisation of thinking and eating should be done by to empty loops that go to a huge number(so it takes a while even on todays computers). A debug printout should be implemented as well to control everything is working as expected. Nearer details can be found in the doxygen documentation or directly in the sourcecode.

Download

Fibonacci Folge in C

Posted on 19. April 2010 Comments

Um mal wieder wegen dem Studium ein bisschen reinzukommen (C diese Semester), hier die Fibonacci Folge in C. Das Ganze wurde eben grade mal schnell zusammengehackt, ist jetzt nicht das beste Programm, was ich je geschrieben habe. Quelltext zum Download.

/* quickly coded by repat, 19/04/2010, way too late in the evening;) */

#include <stdio.h>

int main(void)
{
	//zaehlvariable und endvariable wird deklariert (max. 4.294.967.295)
	unsigned int i,ende;

	//endvariable wird eingelesen, ob der maximale wert dann tatsächlich berechnet in unsigend long passt, ist eine andere sache.
	printf("Bitte Anzahl der zu berechnenden Zahlen eingeben: ");
	scanf("%u",&ende);
	
	//dynamisches array wird anhand der eingabe erschaffen
	unsigned long folge[ende];	
	
	//werte werden laut definition vorgegeben a.k.a. initialisieren
	folge[0]=0;
	folge[1]=1;	

	//wenn nur 0 oder 1 eingeben wurde, eine dementsprechende Meldung ausgeben, sonst anfang schonmal ausgeben.
	if (ende kleiner 2)
	{
		printf("Die vorgegebenen Werte sind %d und %d\n",0,1);
	}
	else
	{
		printf("%d, %d",0,1);
	}

	//array mit der fibonacci folge fuellen und ausgeben, letzte zahl mit einem .
	for ( i=2; i kleiner= ende ;i++)
	{
		folge[i]=folge[i-1]+folge[i-2];		

		if (i==ende)
		{
			printf(", %lu.\n",folge[i]);
		}
			
		else
		{
			printf(", %lu",folge[i]);
		}
	}
	return 0;
}