LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-16-2009, 02:04 PM   #1
judoka
Member
 
Registered: Jul 2009
Posts: 194

Rep: Reputation: 15
Cool running a written program


If I wrote a program in C or C++ (lets call it program ABC) and have ubuntu 9.04 how do I run it using a terminal command? And if I have a program that executes on another program how do I call it to do that? Is the command ~$ run ABC XYZ.c?
 
Old 07-16-2009, 02:11 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
you compile it before you run it.

man gcc



Cheers,
Tink
 
Old 07-16-2009, 02:11 PM   #3
chigurh8
Member
 
Registered: Jul 2009
Distribution: Ubuntu, Gentoo
Posts: 102

Rep: Reputation: 18
Already compiled? ./program
To run it "on another program" ... I'm not exactly sure what you mean, but what you're showing is actually part of the code of the program you're writing. You take arguments to the program from the command line, and use them in the program. The arguments are int argc and char *argv[], like:
int main(int argc, char *argv[])

argc - the number of command-line arguments
*argv[] - the arguments

Last edited by chigurh8; 07-16-2009 at 02:13 PM.
 
Old 07-16-2009, 02:12 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
If it's really .c, then that is source code, and you need to compile it first.

Running any executable file in Linux:
Set the executable bit using (e.g.) chmod +x
If the file location is in PATH, then just type the name
If not, type the full path, or CD to the directory and type "./filename"

Quote:
if I have a program that executes on another program
do you mean a program called by another program? All the same rules apply......
 
Old 07-16-2009, 02:35 PM   #5
judoka
Member
 
Registered: Jul 2009
Posts: 194

Original Poster
Rep: Reputation: 15
I already compiled and the ran the program with ./first but if the program is ABC.c and I have another program XYZ and I was run ABC.c and execute on XYZ.c and in my program's code i wrote that in order for it to do that i should give it the command ABC XYZ.c what do i write in the terminal to do that?
 
Old 07-16-2009, 02:54 PM   #6
chigurh8
Member
 
Registered: Jul 2009
Distribution: Ubuntu, Gentoo
Posts: 102

Rep: Reputation: 18
I'm a little confused. If you're performing some input/output action on XYZ, that's just part of the code. If you're passing XYZ as an argument to the ABC program, it would just be ./ABC XYZ
 
Old 07-16-2009, 03:05 PM   #7
judoka
Member
 
Registered: Jul 2009
Posts: 194

Original Poster
Rep: Reputation: 15
When I try like you said :~$ ./ABC XYZ it says no such file or directroy and my c programs ABC.c and XYZ.c are in /home/yuri/c folder. How do I execute them from the terminal so my ABC.c program performs the programmed action on XYZ.c program? ANd if I wrote a program DEF.c and it's in my /home/yuri/c folder and my terminal says desctop:~& what is the sequence of commands for me to run it from the terminal after I compiled it, linked it , and tested it with ./first?
 
Old 07-16-2009, 04:55 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I am also confused.

Does your program call another program based on the name which is passed during invocation?

If not, then what is done with the argument passed?

For example, if your program is named "ABC" and you run it by typing "ABC XYZ", then "XYZ" is passed as an argument to the program.

Also, how about my earlier question about the PATH? If your directory is not in PATH, then you can run ABC by entering "./ABC" But if the program then tries to run XYZ, it will not work.
 
Old 07-16-2009, 06:28 PM   #9
judoka
Member
 
Registered: Jul 2009
Posts: 194

Original Poster
Rep: Reputation: 15
I don't know how to explain my question, but let my try to simplify it to make it clear. I wrote a program in C, compiled it with ~$ cc -c ABC.c then ~$ cc -o first ABC.c then tested it and ran it with ~$ first ABC.c
it worked and i moved it to /home/yuri/c folder. Now i want to run it by giing it a command from my terminal. What is the sequence of commands to do that if my terminal reads yuri@yuri-desktop:~$ What do I type after ~$ to run the program?
 
Old 07-16-2009, 06:29 PM   #10
judoka
Member
 
Registered: Jul 2009
Posts: 194

Original Poster
Rep: Reputation: 15
sorry tested it and ran it with ./first not first ABC.c in the above post
 
Old 07-16-2009, 07:25 PM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
This is getting stranger and stranger.

To compile a program, you do this:

Code:
gcc infile.c -o outfile
And then you run it as

Code:
./outfile
Nothing else. I have no idea what are you trying to do.

You say you have abc.c and xyz.c. But you are not telling us if they are two indendent programs, of if xyz.c is a library of functions to use on a main() function contained within abc.c, or whatever else.

If abc.c and xyz.c are two independent programs, then I have no idea what are you trying to do, you would compile them independently, and run them independently as I explained above. If they are two pieces of the same project, you would usually compile the object code and then link them into a final executable. It all depends on what exactly you are trying to do, which is impossible to tell by what you have already exposed on the previous posts.

To compile only, without linking, you would use -c

Code:
gcc -c abc.c -o abc.o
gcc -c xyz.c -o xyz.o
tcc abc.o xyz.o -o myprog
./myprog
But before anything else, we have to know what those two obscure pieces of code do, and how they interrelate if at all. Otherwise, all we can do is to give shots into the dark.
 
Old 07-16-2009, 08:29 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by judoka View Post
I don't know how to explain my question, but let my try to simplify it to make it clear. I wrote a program in C, compiled it with ~$ cc -c ABC.c then ~$ cc -o first ABC.c then tested it and ran it with ~$ ./first ABC.c
it worked and i moved it to /home/yuri/c folder. Now i want to run it by giing it a command from my terminal. What is the sequence of commands to do that if my terminal reads yuri@yuri-desktop:~$ What do I type after ~$ to run the program?
Code:
c/first ABC.c
 
Old 07-17-2009, 12:40 AM   #13
judoka
Member
 
Registered: Jul 2009
Posts: 194

Original Poster
Rep: Reputation: 15
What I'm trying to say is that ABC.c and XYZ.c are two separate programs that I wrote and already compiled and tested; but XYZ.c was written to do a function on ABC.c after i wrote the programs i placed them in my /home/yuri/c folder. Now i want to give a command for my XYZ.c program to do a function on ABC.c program. I want to give that command from my console. Do I type: open XYZ.c ABC.c? And if I'm still not making myself clear I'd to find out how to run any program that I wrote and placed in the /home/yuri/c folder from my console. For example, if i wrote compiled and tested ABC.c program, then saved ABC.c in in /home/yuri/c folder, closed logged out, then logged back in, opened the terminal and now want to give a command for my ABC.c program to execute, what do I type in order to do that?
 
Old 07-17-2009, 12:54 AM   #14
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Quote:
Originally Posted by judoka View Post
What I'm trying to say is that ABC.c and XYZ.c are two separate programs that I wrote and already compiled and tested; but XYZ.c was written to do a function on ABC.c
It's just that I don't understand the terminology you are using. Do you mean that xyz parses the abc.c source code file, and inserts/changes some text into it?

Quote:
after i wrote the programs i placed them in my /home/yuri/c folder. Now i want to give a command for my XYZ.c program to do a function on ABC.c program. I want to give that command from my console. Do I type: open XYZ.c ABC.c?
What's "open"?

Please, try to be *Very* specific, speak about abc.c and xyz.c when you mean "the source", the plain text files only. And let us know how are you naming the resulting binary files and what tools (what's "open"?) are you using.

Quote:
And if I'm still not making myself clear I'd to find out how to run any program that I wrote and placed in the /home/yuri/c folder from my console. For example, if i wrote compiled and tested ABC.c program, then saved ABC.c in in /home/yuri/c folder, closed logged out, then logged back in, opened the terminal and now want to give a command for my ABC.c program to execute, what do I type in order to do that?
The process to run a command, or whatever program, is the following:
  • if it lives in a directory which is contained withing your $PATH (see echo $PATH), then just invoke it using the name of the file which contains the program
  • if it lives in a directory wich is not contained in your $PATH, then you need to invoke it by supplying the path+the program name.

In the second case, you can specify the path in either a relative or an absolute way. For example, these are absolute paths:

Code:
/home/i92guboj/myprog
/usr/bin/vim
/opt/opera/bin/opera
/home/i92guboj/bin/my_foo_program.sh
And these are relative, because they are specified in relation to our current working directory:

Code:
./myprog
../progs/my_bin_file.bin
foo/bar/run.sh
Assuming that your compiled program lives in /home/yuri/c/ and that the binary file is called "abc", you would need to run either:

Code:
/home/yuri/c/abc
$HOME/c/abc
~/c/abc
Or if you are already at /home/yuri, you can do

Code:
c/abc
If you are at /home/yuri/docs, you could do

Code:
../c/abc
You get the idea.
 
Old 07-17-2009, 01:43 AM   #15
judoka
Member
 
Registered: Jul 2009
Posts: 194

Original Poster
Rep: Reputation: 15
OK I now understand, thank you very much for the information, that's exactly what I was looking for
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I have written a program and it is giving me the following warning sandeep0424 Linux - Newbie 7 12-10-2008 08:26 AM
How Do I Run/Compile a Program Written in C? debsan Fedora 2 05-25-2006 02:58 PM
Squid is running but my logs are not being written Dagman Linux - Software 5 07-13-2004 08:12 AM
have you ever written a program for linux? today53 General 33 12-05-2003 06:54 PM
another dumb program written in bash. micxz Programming 0 10-13-2003 07:23 PM


All times are GMT -5. The time now is 11:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration