Question

I am new to Ubuntu. I want to run c program on Ubuntu. I have installed build-essential. Now I tried following commands to run the program..

sudo gedit first.c

then typed the basic hello world program. then...

cc -c first.c -o first.c

But on this command it is showing error as

No input files.

and also tell me how to run this program.

Was it helpful?

Solution 3

The correct usage is:

cc -o outputfile myFile.c

Then you can run the executable:

./outputfile

OTHER TIPS

At first you do not have to edit a file as root. That's a bad idea.

The line cc -c first.c -o first.c overwrites your C-File you do not want that you want

cc first.c -o first

and then you want to runs the program you can to that with

./first

That compilation command line should have worked. However it will overwrite your source file.

The -o options tells the compiler what to name the output file, and if you name it the same as the source file it will be overwritten.

The first flags you pass to the compiler, -c, tells the compiler to generate an object file. These object files can later be used by the linker to link with other object files to create the final executable program. It does not, however, create a program you can run.

For creating an executable program from a single source file, do something like:

$ cc first.c -o first

This will create an executable program (if there are no errors of course) named first.

Then to run this program:

$ ./first

This last command tells the shell to look for first in the current directory (which is named .), and execute it.

You don't want .c on the output file. i.e.

cc -c first.c -o first

Edit: Oops. You also don't want the -c

cc -o first first.c

Remove -c and try:

cc first.c -o first
   ^^^^^^^ ^^^^^^^^
    input   output  

To run:

./first

Make is configured to recognize certain file extensions by default, so you can simply do:

make first

Make will then find first.c and produce first binary executable, and show you what compile command it used. Note that for this to work in a sensible manner, you should have just one first.XXX source file. Also, once you have more than one source file, you need to write a Makefile, but with just one source defaults are enough.

To run the produced executable, do

./first

./ is needed, because normally working directory is not (and should not be) in PATH in unix environment.


Oh yeah, and as mentioned by others: do not use sudo for stuff like this. And if you already did that, you can't modify/delete these files as normal user, because they're owned by root. Do ls -l, it will show you owners of files. Then you can do sudo chown yourusername.yoursername file to change owner of file to be owned by yourusername (both user and group of file).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top