简体   繁体   English

从PHP执行C ++时,包含的C ++库不起作用

[英]Included C++ libraries are not working when executing C++ from PHP

I am trying to run C++ on an XAMPP server using the exec() function in PHP. 我正在尝试使用PHP中的exec()函数在XAMPP服务器上运行C ++。 I am importing a C++ json library . 我正在导入C ++ json库 When I run the C++ code from terminal it works perfectly, but when I run it in PHP I get no output. 当我从终端运行C ++代码时,它可以正常工作,但是当我在PHP中运行它时,没有任何输出。 As soon as I add any code from the json library, I no longer get any output. 一旦我从json库添加任何代码,我将不再获得任何输出。

PHP: PHP:

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

C++ compiled with g++ -std=c++11: 使用g ++ -std = c ++ 11编译的C ++:

#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;
}

It seems like the problem has to do with the fact that I am using a library. 看来问题出在我正在使用一个库上。 Do I need to do something special in order to run libraries with the PHP exec() function? 为了使用PHP exec()函数运行库,我是否需要做一些特殊的事情?

I fixed my problem with the help of @ChristianHackl. 我在@ChristianHackl的帮助下解决了我的问题。 I changed exec("./cPlusPlus", $output); 我更改了exec("./cPlusPlus", $output); to exec("./cPlusPlus 2>&1", $output); exec("./cPlusPlus 2>&1", $output); in order to see the thrown errors. 为了看到抛出的错误。 I was then able to see the error: 然后,我能够看到错误:

libstdc++.so.6: version `GLIBCXX_3.4.14' not found libstdc ++。so.6:找不到版本“ GLIBCXX_3.4.14”

and found the solution here . 这里找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM