1

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)

8
  • Welcome to Ask Ubuntu. We may be able to help more if you provide the command & error message exactly (ie. copy & paste it). My guess is you didn't provide the full name (eg. my-program.c where the full name is required including the .c ending) - but without your command & actual error we can only guess like I just did. Your release of Ubuntu isn't essential, but is still helpful as later releases run later versions of gcc which often have different defaults
    – guiverc
    Feb 21 '19 at 0:08
  • I've posted the error message. As I have mentioned in the edit the error seems to be in some way tied to code I create through editors, somehow gedit was not affected
    – V.Ch.
    Feb 21 '19 at 0:16
  • Most likely, the editor you are using to create the .c files is saving them to a directory that's different from the one in which you are executing the gcc command: check what files are in the current directory using ls Feb 21 '19 at 0:16
  • I am sure that is not the case. While testing for this error I kept saving the .c files to the home directory, which is the default directory for the terminal, thus ensuring I don't move somewhere by accident with cd. I have done exactly as you've said, ran ls each time to make sure that the file is present before attempting to compile. The result is still the same
    – V.Ch.
    Feb 21 '19 at 0:18
  • 1
    Your ls output appears to show a file called test - but no file called test.c Feb 21 '19 at 0:24
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.

1
  • Thank you for your exhaustive answer. With regard to editing the PATH variable, a quick google search suggests that this is not the best thing to do, as security and naming issues may arise from this. Should i disregard these? Also, how exactly do i edit it?
    – V.Ch.
    Feb 21 '19 at 12:15
1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.