DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 10th August 2008
Libran Libran is offline
New User
 
Join Date: Aug 2008
Posts: 5
Default Executing a c program

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.
Reply With Quote
  #2   (View Single Post)  
Old 10th August 2008
adamk adamk is offline
Spam Deminer
 
Join Date: May 2008
Posts: 250
Default

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'
Reply With Quote
  #3   (View Single Post)  
Old 10th August 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

...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.
Reply With Quote
  #4   (View Single Post)  
Old 10th August 2008
Libran Libran is offline
New User
 
Join Date: Aug 2008
Posts: 5
Default

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.
Reply With Quote
  #5   (View Single Post)  
Old 10th August 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Netherlands
Posts: 2,243
Default

Quote:
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
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #6   (View Single Post)  
Old 10th August 2008
Libran Libran is offline
New User
 
Join Date: Aug 2008
Posts: 5
Default

Quote:
Originally Posted by Carpetsmoker View Post
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.

Last edited by Libran; 10th August 2008 at 04:07 PM. Reason: addition
Reply With Quote
  #7   (View Single Post)  
Old 10th August 2008
18Googol2's Avatar
18Googol2 18Googol2 is offline
Real Name: whoami
Spam Deminer
 
Join Date: Apr 2008
Location: pwd
Posts: 283
Default

Quote:
Originally Posted by Libran View Post
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

Last edited by 18Googol2; 10th August 2008 at 04:05 PM.
Reply With Quote
  #8   (View Single Post)  
Old 10th August 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Netherlands
Posts: 2,243
Default

Quote:
Compiled with:
cc <filename>
Use:
% cc source.c -o program

Then execute program with:

% ./program
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #9   (View Single Post)  
Old 10th August 2008
drhowarddrfine drhowarddrfine is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 377
Default

I'll bet he doesn't know to add the ./ in front or set the path.
Reply With Quote
Old 11th August 2008
Libran Libran is offline
New User
 
Join Date: Aug 2008
Posts: 5
Default

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.
Reply With Quote
Old 11th August 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by Libran View Post
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(3) 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.
Reply With Quote
Old 11th August 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

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.).

Last edited by BSDfan666; 11th August 2008 at 03:46 PM.
Reply With Quote
Old 11th August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

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
Reply With Quote
Old 12th August 2008
Libran Libran is offline
New User
 
Join Date: Aug 2008
Posts: 5
Default

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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't install program guitarscn OpenBSD General 3 31st August 2009 08:40 PM
A PDP-11 program card TerryP Programming 0 10th April 2009 05:54 AM
How to learn to program under BSD? Sunnz Programming 5 24th December 2008 11:45 PM
Learning how to program Solaris_Delta Programming 9 24th December 2008 07:58 PM
executing commands for all users when X starts with KDM caesius FreeBSD General 2 20th October 2008 09:03 AM


All times are GMT. The time now is 03:17 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick