0

I am working on a school project. Unfortunately, I just ran a compile like so: gcc -o file.c file.o

I forgot to put the output executable name, and now my previous C file is gone. Is there a way to get this file back? I wrote a lot of code that is gone.

Program was written on a Unix server in C.

Thank you!

user3842643
  • 67
  • 1
  • 2
  • 6

3 Answers3

4

Is there a way to get this file back?

There is if you were using a version control system or made a backup. If not, you're likely out of luck.

Caleb
  • 124,013
  • 19
  • 183
  • 272
1

If your editor saved temporary files, they may still be there. Under unix, the file itself is gone. Like this question soon will be, because it is out of scope for StackOverflow.

I think (as Caleb just observerd as I type) you learned one of the many good reason to use source control and check in regularly.


Here is what happens when you compile bad code into the wrong file:

~ mgregory$ cat > foo.h
bad code
~ mgregory$ cat > foo.c
test text
~ mgregory$ gcc -o foo.c foo.h
foo.h:1:1: error: unknown type name 'bad'
bad code
^
foo.h:1:9: error: expected ';' after top level declarator
bad code
        ^
        ;
2 errors generated.
~ mgregory$ ls -l foo.c
ls: foo.c: No such file or directory
~ mgregory$ 
GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
  • I guess my real question is this: what happens when you gcc -o file.c file.h? Did it create an executable with a blank name? I am confused because file.c disapearred entirely – user3842643 May 19 '15 at 04:52
  • I just experimented to confirm: if your compilation fails due to an error (quite likely if you tried to compile a header without a .c file) then the target file is removed. This makes sense: it prevents you running an "old" target file that was generated from old version of of the code, overlooking the fact that your current version of the code doesn't compile – GreenAsJade May 19 '15 at 04:59
  • 1
    Interesting. Good to know. Tough lesson learned, but this was a positive experience for 2 reasons. 1. I learned to ALWAYS use version control. 2. I assured I understood my assignment, because I was able to rewrite the program fairly quickly. – user3842643 May 19 '15 at 06:35
  • This is actually most often true: it's amanzing how much quicker it is to write something the second time, after you loose it the first. Anyone using Microsoft tools has probably experienced this at some time or other.... (or at least, that used to be the case). – GreenAsJade May 19 '15 at 07:01
1

Generally it is not possible to get back a file (but details are painfully operating system and file system specific; if on Ext4 file system on your Linux laptop you might try debugfs from e2fsprogs). You obviously have backups, so use them (if you don't, you have painfully learned something important, and most of us did the same mistake at some early point in our life, so don't be too much ashamed).

It is time for you to understand what version control systems are. So read about git and learn to use it.

If your software is with a free software license, you could even use http://github.com/ (or something else like http://gitlab.com/) as a repository.

You might run file file.c and ls -l file.c and  stat file.c to get an idea of what is inside file.c. You might list the recently modified files with ls -alst

BTW, you should compile your program with

gcc -Wall -Wextra -g file.c -o prog

This asks for all warnings with -Wall, even more of them with -Wextra, debugging info with -g. Then you can use the gdb debugger on your program as gdb prog

BTW, you might want to have a Makefile for GNU make and to just type make to compile your program. Here is a simple example of Makefile.

Notice that recent GCC are able to handle pre-compiled headers (but it is not a feature very useful for a small school project). If you typed gcc -o file.c file.h it is likely that your header file file.h has been precompiled into file.c

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547