gcc "no such file or directory" error when attempting compilation

I just moved to Ubuntu and am currently learning C programming, so naturally I tried to compile some code. I am sure I'm using the command right (it's just a few letters, come on), but the compiler keeps complaining about there being "no such file or directory", regardless of what directory I try to compile from. I have build-essential installed and gcc seems to be functioning normally otherwise, what could be the cause of this? Help is much appreciated.

Edit: Upon further investigation, I've realised that this happens with .c files I create through an editor, such as the default Ubuntu editor or Atom. The result was succesfull when using the gedit command, but unsuccesfull when creating the same file through an editor. I also saved the files to the home directory and ran ls every time to make sure that they're there before attempting to compile. Here's the error message:

me@My-PC:~$ gcc test.c
gcc: error: test.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.

Edit2: I was being an idiot and not saving the files as .c (I assumed setting the format to "C" in the editors would do that for me)


Solution 1:

  1. Case is important, test.c is not test.C.
  2. gcc will actually accept either case .c, if it exists.
  3. gcc by default rejects extensionless files, but steeldriver showed you how to override that (which is certainly not standard practice).
  4. gcc can produce an output for a name you supply, -o test.out using your example. The standard user setup includes a bin directory in the home directory, and this is the usual place to add private scripts because it is already in your PATH. For instance, in ~/bin, add a file named mygcc containing
    gcc -o "$1".out ${1}.c
    to compile your files anywhere with the new command mygcc test Alter to fit your needs -- pass explicit names executables, pass the entire test.c name, etc.
  5. Running from the local directory without the ./ (as ./test.out) is made possible by adding "." to the PATH variable (edit the .profile in your home directory to do this)

The .profile file in your home directory is just a text file owned my you, so any editor will work, but choose one which will not wrap lines for you. The PATH variable is usually set up near the bottom, so you may add the :. to the end of an existing PATH=... line, or make a new one like

PATH=${PATH}:.

Adding the . to the end is "safer" than at the beginning, so it cannot override any system programs of the same name. Running any non-system program entails some risk, (where did it come from, what does it do,...?) but if the program is one you are creating (source and all), those risks are less. It may be worth the convenience to test new compiled programs, your choice.

Solution 2:

I finally found out what was wrong with mine, my file name had a space in it, so gcc read only the first name of the file. Example: hello world.c was read as hello . meaning it couldn't find the file named Hello World.c

Hope this helps!