Author Topic: PHP 5 Processing of C/C++ code  (Read 2742 times)

drsounds

  • Guest
PHP 5 Processing of C/C++ code
« on: December 20, 2008, 08:53:15 pm »
For a time ago, I've installed PHP 5 on my computer to make a website. It didn't work properly, but I've found another "feature" of that, I've just come up with the ultimate though, could I use the php toolkit as a preproccessor for C/C++. This sounds weird, but possible with C::B. Assume you have a project folder containing these files; main.cpp and main.h. Then I add a 's' on the end of the file extensions.
The next step was to create a .cmd file (I'm using WinVista H Pre) where I called the PHP interpreter to look around the code files for PHP code snippets, and then put the output in a new .cpp file. I've made the proccess automatically, by placing a call to that script before the build and only edit the file that ends on .cpps/hs. instead of the regular. The final resoults was that I could connect my code to a php interpreter and use it as an excellent preproccessor. Maybe I can make some benefit of this in the future, when I intend to write code snippts serval times automatically via the PHP 5 interpreter. The only downside was that the compiler took some little more time (about two seconds more, maybe more if you write larger amount of code).

The way to do this is:
First of all, Go get a reliable release of PHP 5 interpreter at php.net. Do it or it won't work.
Add php folder to the system %path% variable.
1. Create a new project, containing main.cpp.
2. Open the project folder (assume it is "C:\myproject")
3. Make a copy of main.cpp and name it main.cpps
4. Open your favorite text editor, write this:
php C:\myproject\main.cpps > C:\myproject\main.cpp and
save it as generate.cmd in the project folder.
5. Open Code::Blocks IDE. Import the main.cpps file in that IDE to the project scout.
6. Open "Build Options" dialog, click on the Pre/post build steps, in the Pre-build steps field, write generate.cmd.
7. Create a new .cmd file, name it to clean.cmd and put this in that file:
del C:\myproject\main.cpp.
8. In the same dialog as before, put clear.cmd in Post-build steps.
9. Try to open the cpps file and do any coding here. If everything is correct, the file will be compiled properly and you'll have the possible to preproccess your code with PHP!
10. A downside is that the syntax highlightning doesn't work when the file ending is modified.