Tuesday, October 25, 2011

A stronger C/C++ Preprocessor

Ever felt you needed some preprocessing to generate some C/C++ code for you, but the C preprocesssor is too lacking? Say for example you wanted to include part of another file, or include output from an application. Perhaps the compile should be based on some data found on a remote server? Well, there are stronger preprocessors which work for C/C++, such as PHP.

Here's an example:
/tmp> php test.cpp.php | g++ -x c++ -Wall -o test -
/tmp> ./test
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
/tmp> cat test.cpp.php
#include <iostream>
int main()
  <?php for ($i = 0; $i < 5; ++$i) { echo 'std::cout << "Hello ', $i, '" << std::endl;'; } ?>
Here's a more interesting example:
/tmp> php weather_example.c.php | gcc -x c -Wall -o weather_example -
/tmp> ./weather_example
Hi, when I was compiled, the weather here in New York City was 57F
/tmp> cat weather_example.c.php
$w = file_get_contents('http://www.google.com/ig/api?weather=New+York+City');
$f = '<temp_f data="';
echo '#define __WEATHER__ ', (int)substr($w, strpos($w, $f)+strlen($f)), 'U', "\n";
#include <stdio.h>
int main()
  printf("Hi, when I was compiled, the weather here in New York City was %uF\n", __WEATHER__);


Sohail said...

I find it really helpful and this is just not a blog it's a goldmine for song lyrics and as we know just visit this site hanuman ji ki chalisa ki lyrics it is a fabulous site for all types of lyrics bye the way you are really helping us.

tanuhealthdoctor said...

I am happy to find this post very useful for me, as it contains lot of information.
this blog

tanuhealthdoctor said...

Thank you for sharing with us! Good luck!kendra karter

Easy Loan Mart said...

Preprocessor programs provide preprocessors directives which tell the compiler to preprocess the source code before compiling.
You are also read more Best Business Loans

Josh Freeman said...

When I need to use a scripting language, I choose the most appropriate tool for the job. For the past, I've worked as a programmer.
Python is good for system administration, but PHP is great for web design development.
PHP is a powerful programming language for web developers. If you're a Sys admin, Python and Bash scripting will suffice.

JacobHarman said...

AWS is a far reaching secure cloud stage which offers more than 175 completely included administrations. It was intended to help all organizations, from little new businesses to enormous endeavors, to turn out to be more adaptable and to enhance all the more productively at low expenses. With AWS organizations can moderately send off servers practically, have static sites and email servers or store information, move their current data sets to the cloud, make s1erverless capabilities to screen assets and that's only the tip of the iceberg>> cloud infrastructure architect aws

John Fei said...

You know that edubridie serves as a useful aid in navigating the complexities of academic writing. It provides students with accessible support, enhancing their understanding of subject matter and essay structure.