tags:

views:

652

answers:

3

Hi,

I have the following c program which launches a Gtk Program on ubuntu:

#include <unistd.h>

int main( int argc, const char* argv[] )
{
    char *args[2] = { "testarg", 0 };
    char *envp[1] = { 0 };
    execve("/home/michael/MyGtkApp",args,envp);
}

I get "Gtk-WARNING **: cannot open display: " and my program is not launched.

I have tried setting char *envp[1] = {"DISPLAY:0.0"}; and execute 'xhost +' , I don't see the 'cannot open display' warning, but my program is still not launched.

Does anyone know how to fix my problem?

Thank you.

A: 

I tried setting the envp to this, and it tries to launch my application.

char *envp[2] = { (char*)"DISPLAY=:0.0", 0 };

But I end up with a Segmentation Fault (my program runs fine when I launch it via command prompt:

(gdb) bt
#0  0x007e5f4e in g_main_context_prepare () from /lib/libglib-2.0.so.0
#1  0x007e6351 in ?? () from /lib/libglib-2.0.so.0
#2  0x007e6b9f in g_main_loop_run () from /lib/libglib-2.0.so.0
#3  0x0041b419 in gtk_main () from /usr/lib/libgtk-x11-2.0.so.0
#4  0x08049191 in main (argc=1, argv=0xbffffed4)
    at main.c:471
michael
A: 
ephemient
A: 

If you ended up with a segmentation fault in MyGtkApp, your app is buggy and this has nothing to do with the program you posted.

Some suggestions:

  1. I would never use 0 instead of NULL, it is a pain generator on 64 bit platforms: use at least (void *) 0;
  2. no need to specify the array size if you're initializing it;
  3. the first argument is (by convention) always the program name, so:

    char *args[] = { "/home/michael/MyGtkApp", "testarg", (void *) 0 };
    
fetasail
As long as prototypes are all correct and varargs aren't in use, I don't see why `0` instead of `NULL` would be a problem. IMO the best reason to use `NULL` is because it is more idiomatic in C.
ephemient