当从PHP执行c++时,包含的c++库不工作

Included C++ libraries are not working when executing C++ from PHP

本文关键字:c++ 工作 包含 PHP 执行 当从      更新时间:2023-10-16

我正在尝试使用PHP中的exec()函数在XAMPP服务器上运行c++。我正在导入一个c++ json库。当我从终端运行c++代码时,它工作得很好,但是当我在PHP中运行它时,我没有得到任何输出。只要我从json库中添加任何代码,我就不再得到任何输出。

PHP:

<?php
  exec("./cPlusPlus", $output);
  echo $output;
?>

用c++编译的c++ -std=c++11:

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <json>
using namespace std;
using json = nlohmann::json;
int main(int argc, char *argsv[])
{
  // If I remove this line, then I get the correct output
  // If I keep this line, I get no output
  json data = {"json stuff"};
  cout << "This is returned to PHP.";
  return 0;
}

似乎这个问题与我正在使用库的事实有关。我是否需要做一些特别的事情,以便与PHP exec()函数运行库?

我在@ChristianHackl的帮助下修复了我的问题。为了看到抛出的错误,我将exec("./cPlusPlus", $output);更改为exec("./cPlusPlus 2>&1", $output);。然后我可以看到错误:

libstdc + + . so。6:版本' GLIBCXX_3.4.14'未找到

在这里找到了解决方案