View Full Version : Executing a c program
Libran
08-10-2008, 03:37 PM
I've googled and searched, but can't find an answer to my first post here.
Using FreeBSD v7.0, I wrote a simple test program in c that just outputs a few lines of text to the screen. I discovered that the program apparently cannot be run from within the same directory where it resides. So, I moved it to /usr/local/bin, and now can execute it from my home directory.
I'm using the JWM window manager, and put my c program into the JWM menu, but it won't execute from there. Both JWM and the program reside in /usr/local/bin.
I'd like to be able to run the program either from the command line or from the JWM menu. I'm guessing I need to know the correct directory in which to put the program. Any help is appreciated.
adamk
08-10-2008, 03:46 PM
How do you know that the command isn't executing from the JWM menu? All it does, you said, is output a few lines of text to the screen. So all it's doing is sending the text to STDOUT. If you don't have a terminal, STDOUT is either the terminal you started X from or your .xsession-errors file if you're using a display manager.
You probably want to add something like this to your JWM menu: 'xterm -e /usr/local/bin/your-program'
BSDfan666
08-10-2008, 03:52 PM
...did you compile said program? or just copy it over in source form? if so, compile it first. ;)
To reiterate what adamk said, if the program is a simple text based application... running it via "JWM" won't produce any noticeable output, use xterm as demonstrated above.
Libran
08-10-2008, 04:07 PM
Yes, I copied the compiled program to /usr/local/bin, and can run it from the command line. I tried xterm as suggested with no luck, but I'll keep toying with it.
Carpetsmoker
08-10-2008, 04:26 PM
Using FreeBSD v7.0, I wrote a simple test program in c that just outputs a few lines of text to the screen. I discovered that the program apparently cannot be run from within the same directory where it resides.
This should be possible, can you post:
The source of the C program
The command you used to compile this program
The command you used to run this program
Libran
08-10-2008, 04:57 PM
This should be possible, can you post:
The source of the C program
The command you used to compile this program
The command you used to run this program
Code for some help files I want to write:
#include <stdio.h>
main()
{
system("clear");
printf("CD burning procedure\n\n");
printf("Before burning, test with the -t option\n\n");
printf("To burn CD: burncd -f /dev/acd0 -v -s 8 data <fname> fixrate\n\n");
printf("To erase CD : burncd -f /dev/acd0 blank\n\n");
printf("Files in /home/Ron:\n\n");
system("ls");
}
Compiled with:
cc <filename>
Result is a.out - cannot execute this unless i move it to /usr/local/bin. Then can run from command line, but not from JWM menu. If I try to execute it drom that directory, it prints out all files in dir. I renamed it to Junk and then can run from command line, but not from /usr/local/bin
Edit - after reading what I wrote it becomes clear that the JWM menu is executing a.out because the last line of my code is being executed - system("ls"); The files then overwrie what the program has put to screen. Also, it seems apparent that renaming the a.out file to Junk doesn't work.
18Googol2
08-10-2008, 05:02 PM
I've googled and searched, but can't find an answer to my first post here.
Using FreeBSD v7.0, I wrote a simple test program in c that just outputs a few lines of text to the screen. I discovered that the program apparently cannot be run from within the same directory where it resides. So, I moved it to /usr/local/bin, and now can execute it from my home directory.
I'm using the JWM window manager, and put my c program into the JWM menu, but it won't execute from there. Both JWM and the program reside in /usr/local/bin.
I'd like to be able to run the program either from the command line or from the JWM menu. I'm guessing I need to know the correct directory in which to put the program. Any help is appreciated.
If the following command output doesnt contain the dir of your program
%echo $PATH
then you need to add the dot to the beginning of that dir
%./path/of/a.out
Carpetsmoker
08-10-2008, 05:33 PM
Compiled with:
cc <filename>
Use:
cc source.c -o program
Then execute program with:
./program
drhowarddrfine
08-10-2008, 06:01 PM
I'll bet he doesn't know to add the ./ in front or set the path.
Libran
08-11-2008, 05:58 AM
Don't think the problem is with ./ or path. I have no trouble running program. From the dir where it was compiled ./filename runs it. If I move the program to /usr/local/bin, I can run it from there with filename or ./filename. Only problem is that I can't get the JWM menu to execute it.
The jwmrc file is written in XML and is easy to edit. I have no trouble putting Firefox, Opera, etc in there. When i again tried inserting xterm -e /usr/local/bin/filename, as suggested, the output was just another xterm.
Truth be known, I don't even want a desktop, just something to launch programs, and JWM seemed so simple.
My sincere thanks to all of you who have tried to help me.
ocicat
08-11-2008, 06:12 AM
When i again tried inserting xterm -e /usr/local/bin/filename, as suggested, the output was just another xterm.
You may want to experiment with a sleep call at the beginning of your code. I am not convinced yet that your application hasn't finished & returned to the xterm's shell prompt by the time the xterm has been fully rendered.
BSDfan666
08-11-2008, 04:44 PM
ocicat is correct, ask your yourself a question..
What does the program do? it executes a command.. prints some text.. executes another command, and then exits.
Now, if you run this program from inside "JWM", where do you think the output is going? do you think it'll just magically open a window with the output you desire?
On most POSIX systems, each program has a "terminal" tty attached to it(Unless it's a daemon.. syslog etc), and each program has 3 file descriptors, stdin, stdout and stderr.
When you run that command without any associated terminal, the output/input are effectively sent to /dev/null.
You can either delay the termination of the program with a sleep(3) call, or open an xterm and execute it manually. (Copying things over to /usr/local/bin is unnecessary, by default, the PATH environment variable sets $HOME/bin as a valid location for personal executables.).
ephemera
08-11-2008, 05:43 PM
or you can use getchar like this:
#include <stdio.h>
int main()
{
printf("CD burning procedure\n\n");
printf("Before burning, test with the -t option\n\n");
printf("To burn CD: burncd -f /dev/acd0 -v -s 8 data <fname> fixrate\n\n");
printf("To erase CD : burncd -f /dev/acd0 blank\n\n");
printf("Files in /home/Ron:\n\n");
system("ls");
puts("\n[Press any key to quit]");
getchar();
return(0);
}
$ cc -Wall -g -o program prog.c
xterm -e /home/`id -nu`/program
Libran
08-13-2008, 12:42 AM
I think I have it.
excerpt from .jwmrc:
<Program icon="Opera.png" label="Opera">opera</Program>
<Program label="Hello">xterm -e $Home/hello</Program>
And it does need either sleep(x) or getchar()
Thanks again to all who helped.
vBulletin® v3.7.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.