.cpp文件在编译后消失



我无法解释如果我无法恢复此文件,这将是多么令人沮丧。老实说,我觉得没有,但我需要了解到底发生了什么,这样我才能在未来采取预防措施。从本质上讲,我正在做一个简单的OOP项目,涉及EMACS中的假银行账户,在编译过程中,包含我所有类代码的.cpp文件会出现并消失。

以下是消失文件前后的终端命令:

    lin114-11:25% ls
    BankAccount.cpp  BankAccount.h  BankAccount.h~  main.cpp
    lin114-11:26% emacs &
    [1] 23359
    lin114-11:27% g++ -o BankAccount.cpp main.cpp
    /tmp/ccEXMM25.o: In function `main':
    main.cpp:(.text+0x67): undefined reference to `CheckingAccount::driver()'
    main.cpp:(.text+0x78): undefined reference to `CheckingAccount::~CheckingAccount()'
    main.cpp:(.text+0xa1): undefined reference to `CheckingAccount::~CheckingAccount()'
    main.cpp:(.text+0xcc): undefined reference to `CheckingAccount::~CheckingAccount()'
    /tmp/ccEXMM25.o: In function `CheckingAccount::CheckingAccount(double, int,                                                                                        
    std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
    main.cpp:(.text._ZN15CheckingAccountC2EdiSs[_ZN15CheckingAccountC5EdiSs]+0x59):
    undefined reference to `vtable for CheckingAccount'
    collect2: ld returned 1 exit status
    lin114-11:28% ls
    BankAccount.h  BankAccount.h~  main.cpp

正如您所看到的,BankAccount.cpp在编译后刚刚出现并消失。发生了什么?是否有恢复文件的方法?

执行此操作时:

g++ -o BankAccount.cpp main.cpp

您告诉g++编译main.cpp,并将输出写入一个名为BankAccount.cpp的文件。因此,您使用可执行文件重写源文件。

juancapanza解释了发生的事情。

根据文件系统的操作方式,仍然有(微弱,微小)检索部分(或全部)的机会…:

df .  #to see which /dev/something partition is holding that current directory
dd if='/dev/something' of=- | cat -v | grep -n 'a_specific_string'  2>/dev/null | less

要有耐心
a_specific_string是您知道在该文件中的字符串(如果可能的话,可能在其他地方,以缩小搜索范围)。

然后,一旦你得到了一些行号,你就可以四处搜索(然而,请注意,如果你的文件超过了inode的长度,那么文件如果仍然在某个地方,完全有可能被分割在磁盘分区的几个不同区域中)

然后你可以使用行号来检索整个文件

例如,如果你只在nnnn:行找到它

 dd if='/dev/something' of='-' | cat -v | awk ' ( (NR > (nnnn - 100) ) && (NR < (nnnn+100) ) { print ; }'  

(匹配行之前打印99行,匹配行之后最多打印100行)
(如果可以的话,根据您要查找的行及其在文件中的位置进行更紧密的调整)