1

I'm trying to run a C program through PHP exec() function. All is running well in my localhost machine, but when running it in hosting server, C program with pow() function that have variables as argument just won't execute.

I asked the customer support of that hosting, they said that executing C programming language is not supported on their server.

But what is strange for me is, why is my another C program is running well on that server, it just that one code that has pow() with variable as parameter in it that won't run.

Here is my code:

PHP

public function index() {
  //runs well
  $varExec = exec(app_path() . "/Exec/checkfuncpow.exe");
  var_dump($varExec);
  echo "<br/>";
  //this the one that is not running
  $varExec = exec(app_path() . "/Exec/checkfuncpowwithvar.exe");
  var_dump($varExec);
}

Results:

string(8) "9.000000"

string(0) ""

checkfuncpow.c

#include <stdio.h>
#include <math.h>

int main() {
  printf("%f", pow((double)3, (double)2));
  return 0;
}

checkfuncpowwithvar.c

#include <stdio.h>
#include <math.h>

int main() {
  double param = 3;
  printf("%f", pow(param, (double)2));
  return 0;
}

Both code was compiled with the mandatory -lm flag to link to math library

gcc checkfuncpow.c -o ./Exec/checkfuncpow.exe -lm
gcc checkfuncpowwithvar.c -o ./Exec/checkfuncpowwithvar.exe -lm

So the ultimate question is is that really because the server does not support it? Or it should be able to run but the customer support didn't have a clue about the problem , so they just saying what is easy for them?

Frumentius
  • 349
  • 2
  • 16
  • What OS and version are you running? What OS and version is your hosting server running? – Andrew Henle Aug 13 '21 at 11:30
  • By "not supported" they don't mean it will not work, they mean they will not help you to make it work and it may break by any change they make and they don't invest time to fix it. It can be that it just happens to work for some reason but you shouldn't rely on that. – 12431234123412341234123 Aug 13 '21 at 11:42
  • My guess is that there is no C math library. Maybe you could use dlopen(), dlsym(), (or LoadLibraryA() if you are on Windows) ... when it is available, to check for the c math library? You could detect if this is actually the problem. Otherwise you could statically link the library. – 12431234123412341234123 Aug 13 '21 at 11:48
  • First of all i dont know this shared hosting operating system, could be linux. My operating system (localhost) is linux ubuntu 20.04. In the shared hosting where should i run these dlopen(), dlsym() functions to check math library? – Frumentius Aug 14 '21 at 09:13

1 Answers1

1

The canonical answer: if your hosting service doesn't support executing C programs, then you should not do it; first of all, because programs may have bugs or strange behavior (like in this case), and second, because the hosting service would not give you any support in case of problems (like in this case).

The interesting answer: the mystery that is going through here is probably related to two things: 1) compiler optimization; 2) missing math library.

About compiler optimization, your two programs happen to behave differently because in the first case (no param variable), the compiler replaces pow((double)3, (double)2) with (double) 9.0; therefore there is no call to pow(), and a math library is not necessary. In the second case, the compiler doesn't optimize the pow() call away, so a math library is still necessary. If my guess is correct, and you compile both your programs with gcc option -O3, both programs should now run without problems.

About missing math library: the math library is linked to your program dynamically (with -lm option); that means that the target platform where the program will run needs to have the library installed. On the other hand, statically-linked libraries get embedded in your program so that the target platform is not required to have them installed.

One thing you could try is to statically link the math library (libm). Search about the topic if you don't know how to do it.

EDIT: in order to solve the problem statically-linking libraries into your executable, you can have a look at this StackOverflow question. TL;DR: GCC option -static should tell the compiler to use static libraries instead of dynamic ones, as long as static version of the library is available. In the case of libm, on my Ubuntu 20.04 box, static version of libm is available, so if I compile a program with command gcc -o program main.c -lm -static, GCC successfully use static version of libm.

Luca Polito
  • 2,387
  • 14
  • 20
  • It's strange because libm is usually incorporated into libc these days (at least on Linux), even though the compiler and linker pretend it isn't. – Ian Abbott Aug 13 '21 at 15:21
  • It's also strange that the program runs at all if it has a dynamic dependency on `libm.so` and there's no `libm.so`. And if it is there, why the strange output? – Andrew Henle Aug 13 '21 at 15:23
  • I would not make any guess since the target platform appears to be Windows (in the question, executables have extension `.exe`). The GCC version being used here is probably taken from [winlibs.com](https://winlibs.com), which uses MinGW-w64 C library. I have not enough experience with GCC on Windows, so I don't know the cause of this strange behavior. – Luca Polito Aug 13 '21 at 17:02
  • thank you for the reply i will definetly try -03 gcc option. and about libm im still confuse, could anyone point me to right direction. Im not using windows, i compile using linux ubuntu. .exe extension its just my habit of compiling C in .exe. – Frumentius Aug 14 '21 at 09:10
  • 1
    I edited my answer showing you how to solve your problem statically-linking `libm` library. TL;DR: use command `gcc -o program main.c -lm -static` in order to compile your executable. – Luca Polito Aug 14 '21 at 12:32
  • @LucaPolito Thank you so much, that really solve the problem, now it runs perfectly in the hosting server. – Frumentius Aug 16 '21 at 10:31