0

I am trying to run C++ on an XAMPP server using the exec() function in PHP. I am importing a C++ json library. When I run the C++ code from terminal it works perfectly, but when I run it in PHP I get no output. As soon as I add any code from the json library, I no longer get any output.

PHP:

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

C++ compiled with g++ -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;
}

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?

shanek21
  • 67
  • 6
  • 1
    Have you checked whether an exception is thrown from the library code? Surround everything in `main` with `try { ... } catch (std::exception const& exc) { std::cout << exc.what() << "\n"; }` and see if your PHP script receives an error message. – Christian Hackl Aug 07 '15 at 15:21
  • @ChristianHackl Definitely a great idea, thanks! Though, sadly, I still get no output. I put a a plain `cout` in the `catch` to see if it was running at all, but still nothing. So it looks like it is running the `try` with no error even though I am not getting any output... – shanek21 Aug 07 '15 at 15:27
  • 1
    I was suspecting a `std::bad_alloc` being thrown for some reason. However, it depends on your implementation; it may very well just crash when it runs out of memory. What about using a "poor man's debugger" and temporarily sprinkling the library header file with some `cout` output? – Christian Hackl Aug 07 '15 at 15:59
  • 1
    Just to answer your general question: No, it's quite unthinkable that using a C++ header-only library like this has any effect on a PHP `exec` call. You should definitely add more information to your question: How did you compile the C++ program? Show us the complete compiler invocation with all flags, and tell us which compiler and which version you use. – Christian Hackl Aug 07 '15 at 16:03
  • I used g++ -std=c++11 to complile. I updated it in the question as well. – shanek21 Aug 07 '15 at 16:08
  • 1
    btw, you should also apply `2>&1` to the end of your command to capture stderr output: `exec("./cPlusPlus 2>&1", $output);` – Christian Hackl Aug 07 '15 at 16:15
  • Ah. That is exactly what I needed. After looking at the errors I was able to see that I had [this problem](http://stackoverflow.com/questions/19386651/how-to-fix-usr-lib-libstdc-so-6-version-glibcxx-3-4-15-not-found) and fixed it. Thanks for your help! – shanek21 Aug 07 '15 at 16:23
  • Cool! :) It would be nice if you added an answer to your own question, briefly explaining what was the problem and how you solved it, so that future readers may benefit from it. – Christian Hackl Aug 07 '15 at 16:26

1 Answers1

0

I fixed my problem with the help of @ChristianHackl. I changed exec("./cPlusPlus", $output); to 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

and found the solution here.

Community
  • 1
  • 1
shanek21
  • 67
  • 6