Click to See Complete Forum and Search --> : Programming questions


paulb
10-18-2001, 03:01 PM
How do I draw wiodows in Linux programming? Is that what GTK is? Can someone point me to a tutorial?

Also, how do you call commands in a C++ program? Like how can I access a system command?

X_console
10-18-2001, 03:05 PM
GTK+ and Qt libraries. Make a search on google for GTK and Qt and they come with tutorials.

paulb
10-18-2001, 03:06 PM
Thanx

paulb
10-18-2001, 03:12 PM
What if I wanted my program to work in KDE and GNOME? Would I use GTK or QT or either or both or what?

bwkaz
10-18-2001, 03:14 PM
If by your second question you mean "how can I do an ls from inside C?", look at the system(3) call (man 3 system).

Actually, I just read the system(3) man page, and you are better off using one of the exec... family of functions -- "man 3 exec" or look at the example given for system(3).

paulb
10-18-2001, 03:20 PM
What I mean is how can I have a c++ program call a system command? For example:

g = shell("dir", "/de/fle/");

or

shell("mpg123 filename.mp3", "filenames/path");

bwkaz
10-18-2001, 10:28 PM
g = shell("dir", "/de/fle/");

Use something like this:

char *argv[5];

argv[0] = "sh";
argv[1] = "-c";
argv[2] = "ls";
argv[3] = "/de/fle/";
argv[4] = NULL;

execv("/bin/sh", argv);

// only returns on error
printf("ERROR can't exec..()\n");
exit(1);

shell("mpg123 filename.mp3", "filenames/path");

Try this:

char *argv[4];

argv[0] = "mpg123";
argv[1] = "filename.mp3";
argv[2] = "filenames/path";
argv[3] = NULL;

execv("/path/to/mpg123", argv);

// only returns on error
printf("ERROR can't exec..()\n");
exit(1);

If you don't want to set up the argv[] array, then try execl():

execl("/path/to/mpg123", "filename.mp3", "filenames/path", NULL);

// only returns on error

printf("ERROR can't exec..()\n");
exit(1);

Note that none of these exec* calls will ever return. If you have to continue execution after the exec() call, you have to use fork():

pid_t p = fork();

if(p == -1) {
// error, can't fork(), print a message and quit or fail the function
printf("ERROR can't fork()\n");
exit(1);
}
else if(p == 0) {
// this is the child process, so do the exec..() call
execl("/path/to/mpg123", "file.mp3", "path/filenames", NULL); // never returns except on error
printf("ERROR can't exec..()\n");
exit(1);
}
else {
// continue execution here, we're still in the parent process
}

WARNING: THIS IS ALL UNTESTED CODE, IT MAY CONTAIN SECURITY HOLES!!! USE AT YOUR OWN RISK, I ACCEPT NO RESPONSIBILITY WHATSOEVER IF IT FSCKS YOUR SYSTEM!!!


edit: CRAP!!! I meant to hit edit before, but hit quote instead!!!! Look below for a new version!

[ 18 October 2001: Message edited by: bwkaz ]

X_console
10-18-2001, 10:34 PM
Originally posted by paulb:
<STRONG>What if I wanted my program to work in KDE and GNOME? Would I use GTK or QT or either or both or what?</STRONG>

If you want it to work in KDE, use Qt. In GNOME, use GTK+. Doesn't really matter. Your program will most likely even work without KDE or GNOME. All the user needs is the libraries for Qt, KDE, GNOME, and GTK+ and they can start running your software. For instance, I don't use KDE, but I can use some KDE apps because I have Qt and KDE libraries installed.

bwkaz
10-18-2001, 10:42 PM
Originally posted by bwkaz:
<STRONG>g = shell("dir", "/de/fle/");

Use something like this:

char *argv[5];

argv[0] = "sh";
argv[1] = "-c";
argv[2] = "ls";
argv[3] = "/de/fle/";
argv[4] = NULL;

execv("/bin/sh", argv);

// only returns on error
printf("ERROR can't exec..()\n");
exit(1);

shell("mpg123 filename.mp3", "filenames/path");

Try this:

char *argv[4];

argv[0] = "mpg123";
argv[1] = "filename.mp3";
argv[2] = "filenames/path";
argv[3] = NULL;

execv("/path/to/mpg123", argv);

// only returns on error
printf("ERROR can't exec..()\n");
exit(1);

If you don't want to set up the argv[] array, then try execl():

execl("/path/to/mpg123", "filename.mp3", "filenames/path", NULL);

// only returns on error

printf("ERROR can't exec..()\n");
exit(1);

Note that none of these exec* calls will ever return. If you have to continue execution after the exec() call, you have to use fork():

pid_t p = fork();

if(p == -1) {
// error, can't fork(), print a message and quit or fail the function
printf("ERROR can't fork()\n");
exit(1);
}
else if(p == 0) {
// this is the child process, so do the exec..() call
execl("/path/to/mpg123", "file.mp3", "filenames/path", NULL); // never returns except on error
printf("ERROR can't exec..()\n");
exit(1);
}
else {
// continue execution here, we're still in the parent process

// An interesting problem is how to tell whether the child could in fact exec().
// Most likely it could, but no guarantees. This is recommended by the man page:
int status = 0; // if your C compiler will let you -- if not, put this line
// at the start of the function

if(waitpid(p, &status, 0) == -1) {
if(errno != EINTR) // fatal error, do whatever
return -1;
}
else {
// now status contains the result of the process that called exec..()
if((WIFEXITED(status)) && (WEXITSTATUS(status) == 1)) {
// exec..() failed
printf("ERROR exec..() failed\n");
exit(1);
}
}
}

WARNING: THIS IS ALL UNTESTED CODE, IT MAY CONTAIN SECURITY HOLES!!! USE AT YOUR OWN RISK, I ACCEPT NO RESPONSIBILITY WHATSOEVER IF IT FSCKS YOUR SYSTEM!!!</STRONG>

edit: added code to check whether exec..() succeeded in the last example.

paulb
10-19-2001, 11:38 AM
I think Ill use GTK. 1 question, though:

Can I run a GTK prog in KDE if I have the GTK libs?

Thanks. Just making sure.

mmccue
10-26-2001, 01:07 PM
Can I run a GTK prog in KDE if I have the GTK libs?

Thanks. Just making sure.



I believe the answer is yes, as long as you have the libraries you shouldn't have any problems running the apps.

Mike