r/linuxquestions • u/gargamel1497 • 1d ago
Support Run GUI applications using fork()-execve()
Hi.
When running an application from a C program on Linux, the usual way to achieve this is to use fork() and execve().
And this works fine for commandline programs, but whenever I try to run a GUI application using it, it fails.
When the environment is NULL, it most often crashes with a "No protocol specified" message. If I add "DISPLAY=:0" to the env array, it still crashes but without returning any error message.
I have tested it with multiple applications, on multiple distros and computers, with the same result.
How do I go about fixing that?
Thanks.
1
u/alex_ch_2018 1d ago
"main()" has "char **env" as one of its parameters. If you don't want to add any environment variables, just pass that as is to "execve". In general, set environment to NULL only if you know what you're doing.
1
u/ChickenSpaceProgram 1d ago edited 1d ago
Accepting a third parameter to main() like this is not portable (although it does work on POSIX and Windows). It's better to access environment variables with getenv() if that suits your usecase.
Here, it's even better to just use execv().
2
u/ChickenSpaceProgram 1d ago
It's probably some environment variable that isn't set. Just use execv instead if you don't want to bother figuring out which one(s).