views:

102

answers:

5

Is it possible to have a c++ program like this...

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

And have it's output on a webpage like this...

<html>
  <head>
    <title>C++</title>
  </head>
  <body>
    <div src = "cpp.exe"></div>
  </body>
</html>
+3  A: 

Not in this way, but you can use C++ as a server side language (pretty much like many others around). This library seems to be interesting: Tntnet. Take a look a this example.

ltcmelo
+6  A: 

Not in the HTML per se, no. But if your server supports it (e.g., Apache), you can use a server-side include to execute a program and include the output on the web page.

Your HTML page would look like this:

<html>
  <head>
    <title>C++</title>
  </head>
  <body>
    <div><!--#exec cmd="cpp.exe" --></div>
  </body>
</html>

It isn't a very good approach because there are better ways to produce dynamic content. But if you really, really need to do it this way, that's how...

Jeff
+2  A: 

With little modifications to the html, yes, using SSI.

mmonem
+2  A: 

You basically have two possibilities. SSI is built into a number of web servers such as Apache. Alternatively, especially for some older web servers that have less capability built in, or if you want some extra features (e.g., running the program on a separate machine from the web server) you might consider using something like FastCGI.

Jerry Coffin
+3  A: 

Jeff is right.

You can also use a library like CPPCMS that allow you to do almost the same thing that what you need :

void my_hello_world::main()  
{  
    cout<<"<html>\n"  
          "<body>\n"  
          "  <h1>Hello World</h1>\n"  
          "</body>\n"  
          "</html>\n";  
}  

Read this tutorial for details : http://art-blog.no-ip.info/wikipp/en/page/tut_hello_world_code

However just make sure that C++ is really what you want to use. As explained in the rationale, the only reason you'd like to use it is in cases you need high performance for your web app. See : http://art-blog.no-ip.info/wikipp/en/page/rationale

Klaim
I'm not going to down vote you but, I really don't think many people would want to do it this way.
thyrgle
I'm not sure what you mean? If you mean that using C++ for building a web application is not a way of doing it for everybody, you're right. this library is made for high load web applications, not everybody's website. See http://art-blog.no-ip.info/wikipp/en/page/rationale . If someone asks for C++ to generate html code, I guess it's for for classic usage.
Klaim
I added this point in the answer.
Klaim