3

在我的 cpp 文件中,我将一些调试消息打印到 std::cout 标准输出流。当我使用此文件并使用 Apache 服务器运行可执行文件时。调试消息将在哪里打印。我没有看到它们打印在 /var/lib/httpd/error_log 中。

提前致谢。

4

1 回答 1

3

您应该使用 Apache Web 服务器来运行 C++ 程序的唯一原因是您制作 CGI 脚本

看看:http ://en.wikipedia.org/wiki/Common_Gateway_Interface


此处的过程是 Web 服务器 Apache 运行您的程序并使用输出(std::cout)作为页面源。

页面源可以是 html 或纯文本。唯一的问题是服务器不知道,所以你在输出开始时给它一点提示。它被称为标题。

如果您输出 html,则必须打印:

内容类型:文本/html

后跟两个换行符。

或者如果您希望 Web 服务器将数据解释为纯文本,则必须首先打印

内容类型:文本/纯文本

后面还有两个换行符。


例如,一个应该可以工作的 C++ 程序看起来像这样:

#include <iostream>

int main()
{
  //output header, then one newline, then another, paired with a flush.
  std::cout << "Content-type: text/plain\n" << std::endl;
  //now your output
  //calculation...
  std::cout << "Hello World" << std::endl;
  return 0;
}

可以使用一些预先设置的环境变量来查询任何 Web 服务器参数。阅读我链接的维基百科文章。


编辑:

我很抱歉,Content-type: text/html并且Content-type: text/plain是正确的,但我之前说过他们需要一个新行。我弄错了,他们需要两条新线

如果这是您第一次看到这篇文章,请不要担心。

于 2013-03-04T22:44:07.643 回答