LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Compiling using cc generates cc: no input files (https://www.linuxquestions.org/questions/linux-newbie-8/compiling-using-cc-generates-cc-no-input-files-898449/)

electriceddy 08-20-2011 06:44 AM

Compiling using cc generates cc: no input files
 
Hi All,

I was following a very basic tutorial on how to compile programs for linux using gcc and cc

When I compile and run the test program below, using the command gcc program.c -o program everything works fine.

#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}

If I try compiling using the following commands it generates an error relating to having no input file:

cc program.c

cc -o program.c
Error:
cc: no input files

I must be missing something very basic here?

Regards

Patrick

Wim Sturkenboom 08-20-2011 07:04 AM

Does the first one give the error as well? If so, are you compiling in the directory where program.c is stored?

For the second one, -o specifies the output file. So you actually did not specify any source files to be compiled.

Code:

fortyfourgalena@desktop-01:~$ ls *.c
ls: cannot access *.c: No such file or directory
fortyfourgalena@desktop-01:~$ gcc hallo.c
gcc: hallo.c: No such file or directory
gcc: no input files
fortyfourgalena@desktop-01:~$ gcc -o hallo.c
gcc: no input files
fortyfourgalena@desktop-01:~$


electriceddy 08-20-2011 07:16 AM

The first one compiles and runs okay and I'm compiling within the tmp directory where the source file is located.

The second approach generated the file 'program.o' after executing the command 'cc program.c'

The tutorial stated that after running the above file to execute the command cc -o program.c

I take it that this is not the correct syntax?

What would be the correct syntax of the commands if I wanted to compile a program.c in the tmp folder and link it afterwards?

Patrick

trist007 08-20-2011 07:31 AM

I know for gcc it's the following
Code:

gcc -o program program.c
My cc is linked to gcc. They should be using the same syntax. The -o designates what you what you want the finished product binary to be called.

johnsfine 08-20-2011 07:47 AM

Quote:

Originally Posted by electriceddy (Post 4448499)
I was following a very basic tutorial on how to compile programs for linux using gcc and cc

Please provide the URL of that tutorial.

You quoted incorrect instructions from the tutorial. I think it would help if we could look at the tutorial and tell you whether you misunderstood what it said vs. the information there is incorrect.

electriceddy 08-20-2011 09:32 AM

Second post on this site archive.

http://ubuntuforums.org/showthread.p...ighlight=world

Patrick.

Wim Sturkenboom 08-20-2011 10:46 AM

They are wrong; read man cc. Look for the -o option; the argument following it is the name of the output file.

Code:

    -o        filename
          Names        the output file        filename, instead of the  default
          a.out.  filename cannot be the same as sourcefile since
          cc does not overwrite        the source file.  This option and
          its argument are passed to ld.


electriceddy 08-20-2011 12:00 PM

Thank's for pointing me in the right direction, I was assuming that cc and gcc were two different compilers but on my system cc seems to be just a pointer to gcc and takes the same command line options.

Patrick.

johnsfine 08-20-2011 12:22 PM

Quote:

Originally Posted by electriceddy (Post 4448591)

That post says:
Quote:

cc -c first.c

This will create an object file, to create an executable file type in:

cc -o first.c
I'm sure that second command was meant to say
cc -o first first.c
that was just mistyped.

Even without that error, the post is misleading, since it doesn't explain why a beginner might ever want the first command (which creates only an object file) vs. the second command that creates an executable.

So it is easy to see how electriceddy misunderstood and thought the second command was for use after the first command rather than instead.

Quote:

Originally Posted by electriceddy (Post 4448513)
What would be the correct syntax of the commands if I wanted to compile a program.c in the tmp folder and link it afterwards?

If you want compile and link to be two separate steps:
gcc -c program.c
gcc -o program program.o

The first step creates program.o. The second step uses program.o to create program. It leaves program.o so you have both program and program.o when you're done.

The single step version is
gcc -o program program.c
That compiles (creating the .o file) and links (creating the executable from the .o) then deletes the .o.


All times are GMT -5. The time now is 11:19 AM.