Solved

Running a c file in linux enviroment

Posted on 2011-09-25
9
241 Views
Last Modified: 2012-06-21
Hello Experts..

I am a newbie to linux and need to know how I can compile and run this c program.

I don't have any enviroments setup on my computer.  So I am looking to get a "how to" so I can run this file.

Thanks dad.c
0
Comment
Question by:Maliki Hassani
9 Comments
 
LVL 21

Expert Comment

by:Papertrip
ID: 36595970
You will need to download gcc if it's not installed.
gcc dad.c -o dad
./dad

Open in new window

That will compile dad.c into an executable named dad, then run it.
0
 

Author Comment

by:Maliki Hassani
ID: 36596004
okay, I am doing it now..  Can you talk me through how I would run the file?  
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36596007
If you typed command 1 exactly as I pasted it, then command 2 will execute it.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 21

Expert Comment

by:Papertrip
ID: 36596008
Ah I can see how my wording on the initial reply could have been confusing.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36596973
In Linux, the following should work.  (Assumes your source file is called foo.c)

$ make foo
$ ./foo


make foo will run the compiler and linker.
./foo will run the program.

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36596977
Are you trying to learn C or are you just trying to port the software?

If the program is complicated, or you plan to write complicated programs in the future, I suggest you learn about make.  (If you need pointers, EE is a place to ask).

Also, if you are learning C, I suggest you also try to learn C++.  (This is even more important if you are right brain dominant.)
0
 
LVL 24

Expert Comment

by:fridom
ID: 36597094
No, make will  not work without having  a proper Makefile.
The proper command is by Papertrip.
0
 
LVL 2

Accepted Solution

by:
xeltek earned 250 total points
ID: 36597880
Hi,
I checked your code,  gcc will compile and no other 3rd party library needed.
What linux distro you are using? then we can help to install the compiler.
gcc:
http://gcc.gnu.org/
a brief tutorial:
http://pages.cs.wisc.edu/~beechung/ref/gcc-intro.html

I tested your code in my ubuntu server box:

root@ubuntu:/srv/ftp# gcc dad.c -o dad
root@ubuntu:/srv/ftp# ./dad


Please choose the assignment to run:

2. Assignment 2
3. Assignment 3
4. Assignment 4
0. Quit

Please enter the number of your choice:

Open in new window


Installation of gcc in ubuntu box will be:
#apt-get install gcc
0
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 250 total points
ID: 36598796
fridom, make works for me, out of the box, without having a Makefile.  Of course, I create a Makefile for more complicated compilations.

hmccurdy@dagmar:~/empire/c/hello$ ls -l
total 4
-rw-r--r-- 1 hmccurdy hugh 56 2011-09-25 23:28 hello.c
hmccurdy@dagmar:~/empire/c/hello$ make hello
cc     hello.c   -o hello
hmccurdy@dagmar:~/empire/c/hello$ ./hello
Hello
hmccurdy@dagmar:~/empire/c/hello$ 

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question