简体   繁体   中英

How do I run a C program that involves an external library in a PHP script?

I created a simple C program that takes an integer argument and outputs that integer plus two. I can run it in a terminal with

./simplep 2

and in PHP with

exec('./simplep 2', $output) .

I have a more complicated program called myprogram which involves an external library. I can run it in the terminal with

./myprogram a.mov

but it fails if I run it in PHP with

exec('./myprogram a.mov', $output)

No output is produced, which makes me think the program isn't running at all.

PS. I am working on Mac.

update

From the error ouput I know the problem is the library. It says

dyld: Library not loaded: /opt/local/lib/libjpeg.8.dylib". 

But the library file is there.

It's probably producing an error, but it's going to stderr instead of stdout . One simple way to view stderr here is to change:

exec('./myprogram a.mov', $output)

to

exec('./myprogram a.mov 2>err.out', $output)

and view the file err.out to see what the error message is.

EDIT:

Now that you posted the error, it looks like a dynamic library is not being loaded. The most likely reason is that environment variables are not being passed to exec , and the DYLD_LIBRARY_PATH environment variable is being cleared out. See what DYLD_LIBRARY_PATH is on your terminal (via env ), then try running:

exec('env DYLD_LIBRARY_PATH=XYZ ./myprogram a.mov 2>err.out', $output)

Where XYZ is the value of DYLD_LIBRARY_PATH on your terminal.

Do you have right permissions to run myprogram with exec? Try this

chmod +x myprogram

如果您的程序在命令行上运行,请尝试使用shell_exec而不是exec。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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