PHP zu C++ mit BinaryPHP und Tiny C Compiler (tcc)



  • Hallo,

    ich habe erfahren, dass man PHP-Skripte zu C++ kompilieren kann. Allerdings würde ich mich als Anfänger über eure Hilfe beim Einrichten sehr freuen. (Keine Angst, weitere Programmierfragen [was ist ein String?] bleiben euch danach erspart. 😉 )

    In der Readme von BinaryPHP steht folgendes zum Konvertieren:
    ./convert.php --if examples/irc.php --of ircbot

    Wie genau oder womit rufe ich jetzt die convert.php auf?
    Normalerweise rufe ich php-Dateien ja einfach per Browser auf. Aber in diesem Falle scheint man noch einen C Compiler zu benötigen. In der Hoffnung, das dieser funktionieren wird, habe ich mir
    TCC heruntergeladen. Mit ihm lassen sich mit der Datei "tcc.exe -c dateiname" ganz einfach Dateien compilen.

    Doch wie verbinde ich das jetzt mit der convert.php?



  • Ich habe zwar weder Ahnung von PHP, noch von dem Tool, aber ich würde mal ganz stark darauf tippen, dass du einfach einen PHP Interpreter installieren musst um PHP-Scripte auszuführen.



  • Vielleicht sollte ich noch auf das Konvertertool und den Compiler verlinken:
    http://sourceforge.net/projects/binaryphp/
    http://bellard.org/tcc/

    Zum Interpreter:
    In die Richtung wirds wohl gehen, nur werden PHP-Dokumente ja normalerweise einfach durch den Apache interpretiert, den ich mir auch installiert habe. Ein Aufruf zeigt:
    #!/usr/bin/php Usage: convert.php --if --of [-v | --verbose] [-h | --help] Too Few Arguments



  • nettes tool, grad mal probiert.

    du nutzt deinen php interpreter über konsole:

    /pfadzuminterpreter/php convert.php --if examples/helloworld.php --of hello
    

    was macht er weiter? in der converter.php ist alles zu erkennen:
    er übersetzt das skript in eine c++ Datei (hello.cpp) und nutzt einen externen compiler zum compilieren.
    je nach betriebssystem sucht er entweder den g++ oder den cl (windows compiler).

    achtung: beim ersten anlauf gab es folgenden fehler
    [/code]Fatal error: Cannot redeclare Generator::$defines in /Users/Downloads/BinaryPHP-POC/tokenflow.php on line 19

    der schnelligkeit halber habe ich die stelle einfach auskommentiert, müsste man mal schaun, was da los ist. 
    übrigens ist das tool älter, noch in php4 geschrieben worden.
    
    raus kommt eine von php nach c++ und dann compilierte, ausführbare Datei.
    
    dieser beispielcode
    [code]<?php
    echo 'Hello World', "\n";
    ?>
    

    wird zu:

    #include <sstream>
    using namespace std;
    #include <iostream>
    #include <string>
    #include <vector>
    #include <stdio.h>
    #define PHP_NULL 0
    #define PHP_STRING 1
    #define PHP_INT 2
    #define PHP_BOOL 3
    #define PHP_ARRAY 4
    #define PHP_RESOURCE 5
    #define PHP_OBJECT 6
    
    char* intstring(long i)
    {
    	char *buf = new char[11];
    	sprintf(buf, "%i", i);
    	return buf;
    }
    char* doublestring(double i)
    {
    	char *buf = new char[16];
    	sprintf(buf, "%d", i);
    	return buf;
    }
    
    class php_var
    {
    	public:
    		php_var() // Constructor
    		{
    			type = PHP_NULL; // Make the var, but make it null.
    			container = "";
    		}
    		php_var(const char* str)
    		{
    			container = str;
    			type = PHP_STRING;
    		}
    		php_var(double i)
    		{
    			type = PHP_INT;
    			container = doublestring(i);
    		}
    		php_var(int i)
    		{
    			type = PHP_INT;
    			container = intstring(i);
    		}
    		php_var(unsigned int i)
    		{
    			container = intstring(i);
    			type = PHP_INT;
    		}
    		php_var(long i)
    		{
    			container = intstring(i);
    			type = PHP_INT;
    		}
    		php_var(const php_var &temp)
    		{
    			type = temp.type;
    			container = temp.container;
    			keys = temp.keys;
    			data = temp.data;
    			res = temp.res;
    		}
    		php_var(char * str)
    		{
    			container = str;
    			type = PHP_STRING;
    		}
    		php_var(string str)
    		{
    			container = str;
    			type = PHP_STRING;
    		}
    		php_var(bool b)
    		{
    			if(b)
    				container = "1";
    			else
    				container = "0";
    			type = PHP_BOOL;
    		}
    		operator const char*()
    		{
    			if(type == PHP_STRING || type == PHP_INT)
    				return container.c_str();
    			else
    				return "Array";
    		}
    		operator string()
    		{
    			if(type == PHP_STRING || type == PHP_INT)
    				return container;
    			else
    				return string("Array");
    		}
    		operator bool()
    		{
    			if(type != PHP_BOOL || (type == PHP_BOOL && container.compare("1") == 0))
    				return true;
    			return false;
    		}
    		operator double()
    		{
    			return atof(container.c_str());
    		}
    		operator float()
    		{
    			return atof(container.c_str());
    		}
    		operator int()
    		{
    			return atoi(container.c_str());
    		}
    		operator unsigned int()
    		{
    			return atoi(container.c_str());
    		}
    		operator long()
    		{
    			return atol(container.c_str());
    		}
    		php_var &operator[](int subscript)
    		{
    			if(type == PHP_STRING)
    			{
    				// return &container[subscript];
    			}
    			else if(type == PHP_ARRAY)
    			{
    				php_var key = subscript;
    				int i = 0;
    				for(i = 0;i < keys.size(); ++i)
    				{
    					if(key.container.compare(keys[i].container) == 0)
    						return data[i];
    				}
    				php_var temp;
    				keys.push_back(key);
    				data.push_back(temp);
    				return data[i];
    			}
    		}
    		php_var &operator[](unsigned int subscript)
    		{
    			if(type == PHP_STRING)
    			{
    				// return &container[subscript];
    			}
    			else if(type == PHP_ARRAY)
    			{
    				php_var key = subscript;
    				int i = 0;
    				for(i = 0;i < keys.size(); ++i)
    				{
    					if(key.container.compare(keys[i].container) == 0)
    						return data[i];
    				}
    				php_var temp;
    				keys.push_back(key);
    				data.push_back(temp);
    				return data[i];
    			}
    		}
    		php_var &operator[](long subscript)
    		{
    			if(type == PHP_STRING)
    			{
    				// return &container[subscript];
    			}
    			else if(type == PHP_ARRAY)
    			{
    				php_var key = subscript;
    				int i = 0;
    				for(i = 0;i < keys.size(); ++i)
    				{
    					if(key.container.compare(keys[i].container) == 0)
    						return data[i];
    				}
    				php_var temp;
    				keys.push_back(key);
    				data.push_back(temp);
    				return data[i];
    			}
    		}
    		php_var &operator[](const char* subscript)
    		{
    			if(type == PHP_STRING)
    			{
    				// return &container[subscript];
    			}
    			else if(type == PHP_ARRAY)
    			{
    				php_var key = subscript;
    				int i = 0;
    				for(i = 0;i < keys.size(); ++i)
    				{
    					if(key.container.compare(keys[i].container) == 0)
    						return data[i];
    				}
    				php_var temp;
    				keys.push_back(key);
    				data.push_back(temp);
    				return data[i];
    			}
    		}
    		php_var &operator[](char* subscript)
    		{
    			if(type == PHP_STRING)
    			{
    				// return &container[subscript];
    			}
    			else if(type == PHP_ARRAY)
    			{
    				php_var key = subscript;
    				int i = 0;
    				for(i = 0;i < keys.size(); ++i)
    				{
    					if(key.container.compare(keys[i].container) == 0)
    						return data[i];
    				}
    				php_var temp;
    				keys.push_back(key);
    				data.push_back(temp);
    				return data[i];
    			}
    		}
    		php_var &operator[](string subscript)
    		{
    			if(type == PHP_STRING)
    			{
    				// return &container[subscript];
    			}
    			else if(type == PHP_ARRAY)
    			{
    				php_var key = subscript;
    				int i = 0;
    				for(i = 0;i < keys.size(); ++i)
    				{
    					if(key.container.compare(keys[i].container) == 0)
    						return data[i];
    				}
    				php_var temp;
    				keys.push_back(key);
    				data.push_back(temp);
    				return data[i];
    			}
    		}
    		php_var &operator[](php_var subscript)
    		{
    			if(type == PHP_STRING)
    			{
    				// return (char *)&container[subscript];
    			}
    			else if(type == PHP_ARRAY)
    			{
    				php_var key = subscript;
    				int i = 0;
    				for(i = 0;i < keys.size(); ++i)
    				{
    					if(key.container.compare(keys[i].container) == 0)
    						return data[i];
    				}
    				php_var temp;
    				keys.push_back(key);
    				data.push_back(temp);
    				return data[i];
    			}
    		}
    		bool operator<(int i)
    		{
    			if(atol(container.c_str()) < i)
    				return true;
    			return false;
    		}
    		bool operator>(int i)
    		{
    			if(atol(container.c_str()) > i)
    				return true;
    			return false;
    		}
    		bool operator<(php_var i)
    		{
    			if(atol(container.c_str()) < atol(i.container.c_str()))
    				return true;
    			return false;
    		}
    		bool operator>(php_var i)
    		{
    			if(atol(container.c_str()) > atol(i.container.c_str()))
    				return true;
    			return false;
    		}
    		bool operator!=(const char* cmp)
    		{
    			if(container.compare(cmp))
    				return true;
    			return false;
    		}
    		bool operator!=(int i)
    		{
    			if(atol(container.c_str()) == i)
    				return false;
    			return true;
    		}
    		bool operator!=(php_var var)
    		{
    			if(!container.compare(var.container))
    				return false;
    			return true;
    		}
    		bool operator==(const char* cmp)
    		{
    			if(container.compare(cmp) == 0)
    				return true;
    			return false;
    		}
    		bool operator==(int i)
    		{
    			if(atol(container.c_str()) == i)
    				return true;
    			return false;
    		}
    		bool operator==(php_var var)
    		{
    			if(container.compare(var.container) == 0)
    				return true;
    			return false;
    		}
    		int operator++(int i)
    		{
    			int ret = atol(container.c_str());
    			container = intstring(ret + i);
    			return ret;
    		}
    		int operator++()
    		{
    			int ret = atol(container.c_str()) + 1;
    			container = intstring(ret);
    			return ret;
    		}
    		int operator--(int i)
    		{
    			int ret = atol(container.c_str());
    			container = intstring(ret);
    			return ret;
    		}
    		int operator--()
    		{
    			int ret = atol(container.c_str()) + 1;
    			container = intstring(ret);
    			return ret;
    		}
    		void operator+=(int inc)
    		{
    			if(type == PHP_INT)
    			{
    				container = intstring(atol(container.c_str()) + inc);
    			}
    		}
    		void operator+=(php_var str)
    		{
    			if(str.type == PHP_INT)
    			{
    				container = intstring(atol(container.c_str()) + atoi(str.container.c_str()));
    				if(type != PHP_INT && type != PHP_STRING)
    					type = PHP_INT;
    			}
    			else
    			{
    				container += str.container;
    				if(type != PHP_STRING)
    					type = PHP_STRING;
    			}
    		}
    		void operator+=(string str)
    		{
    			container += str;
    		}
    		void operator+=(const char* str)
    		{
    			container += str;
    		}
    		void operator+=(char* str)
    		{
    			container += str;
    		}
    		void operator-=(int dec)
    		{
    			if(type == PHP_INT)
    			{
    				container = intstring(atol(container.c_str()) - dec);
    			}
    		}
    		void operator/(php_var i)
    		{
    			if(type == PHP_INT)
    				container = doublestring(atof(container.c_str()) / atof(i.container.c_str()));
    		}
    		void operator-(php_var i)
    		{
    			if(type == PHP_INT)
    				container = doublestring(atof(container.c_str()) - atof(i.container.c_str()));
    		}
    		friend ostream &operator<<( ostream &out, const php_var &var );
    		void to_array()
    		{
    			type = PHP_ARRAY;
    		}
    		string container; // Contains value.
    		vector<php_var> keys;
    		vector<php_var> data;
    		void *res;
    		void *obj_type;
    		int res_type;
    		int type; // Contains current type.
    };
    ostream &operator<<( ostream &out, const php_var &var )
    {
    	if(var.type == PHP_ARRAY)
    		out << "Array";
    	else
    		out << var.container;
    }
    bool operator<(int i,php_var v)
    {
    	return(v > i);
    }
    bool operator>(int i, php_var v)
    {
    	return(v < i);
    }
    php_var operator+(php_var l, php_var r)
    {
    	return (php_var)((int) l + (int) r);
    }
    php_var operator-(php_var l, php_var r)
    {
    	return (php_var)((int) l - (int) r);
    }
    #define is_identical(var, varb) (((var) == (varb) && (var).type == (varb).type) ? (php_var)true : (php_var)false)
    
    int main(int _argc, char **_argv);
    int main(int _argc, char **_argv)
    {
    	cout << (php_var)"Hello World" << endl;
    	return 0;
    }
    


  • ach ja, willst du einen anderen compiler nutzen, musst du die converter.php eben erweitern.



  • BinaryPHP erzeugt C++, tcc ist ein C-Compiler...



  • Nu gehts, hab den Dev-Cpp genutzt.
    Die Fehlermeldungen habe ich wegbekommen. Auch durch wegstreichen. 😕
    Das Ganze wirft auch schon ein schönes "Hello World" in der Eingabeaufforderung aus. 🙂

    Hat jemand einen Tipp, wie man GUIs in PHP erstellen könnte?


  • Administrator

    Tach schrieb:

    Hat jemand einen Tipp, wie man GUIs in PHP erstellen könnte?

    Diese PHP zu C++ Umwandler sind meistens eigentlich dazu gedacht, um PHP zu beschleunigen. Ein bekannter Vertreter davon dürfte HipHop von Facebook sein:
    https://github.com/facebook/hiphop-php/wiki/

    Es ist aber nicht deren Sinn, dass du nun ein GUI für eine Desktopanwendung in PHP entwickelst und diese danach nach C++ umwandeln lässt.

    Grüssli



  • Habe mir das PHP-GTK zum Anzeigen von GUIs und den PHP-GTK Builder zum Zusammenbasteln mal angeschaut. Macht nen guten Eindruck, aber ist auch keine Standalone-Lösung.

    @Dravere: Wahrscheinlich hast du recht, dass sich PHP letztlich nicht für GUIs eignen wird. So ganz hab ich die Hoffnung aber noch nicht aufgegeben. ^^

    So wirbt z.B. der "Roadsend Compiler" mit dem folgenden Satz:
    "Roadsend Compiler can build online web applications with Fast/CGI, offline web applications with an embedded web server (MicroServer), and console applications."
    aus: http://www.roadsend.com/home/index.php

    Ob das dann C++ ist, ist die Frage.



  • hi kannst du nochmal schreiben, wie du das zum laufen gebracht hast bei mir geht es nicht 😞


  • Mod

    Dieser Thread ist viele Jahre alt. Mach einen neuen auf.


Anmelden zum Antworten