2015-10-28 79 views
0

我一直在使用C++實現一個簡單的ODE求解器來實現特定的功能。一切工作正常,編譯得很好。然後,我添加了一些調整,並保存它。突然之間,舊版和新版本都不再編譯了!我一直在使用emacs.I擔心我可能會意外刪除一些庫文件,但我不知道這是怎麼發生的! 這是我的錯誤:C++代碼突然不再編譯

ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

這是正在完美的罰款代碼:

#include <iostream> 
#include <cmath> 
#include <fstream> 

using namespace std; 

/* Differential equation */ 
double fun(double y){ 
    return (sqrt(y)); 
} 

/*euler update formula */ 
double EulerUpdate(double y_n, double t_step){ 

    return y_n + t_step * fun(y_n); 

} 

/* main function asking the user for input values, data stored in .dat file */ 
int main(void){ 
    double T; 
    double y0; 
    double t_step; 
    double t = 0; 
    cout << " enter the maximum t value for which to compute the solution: "; 
    cin >> T ; 
    cout << "enter the initial value y0: "; 
    cin >> y0 ; 
    cout << "enter the time step: "; 
    cin >> t_step; 
    double y_n = y0; 
     ofstream outFile("ODE.dat"); 
    for (int n = 0; n < T; n++){ 
     outFile << t << " " << y_n << endl; 
     y_n = EulerUpdate(y_n, t_step); 
     t = t + t_step; 


    } 

} 
+0

程序正在編譯好,它是失敗的鏈接器。你的調整是否涉及添加cmath? – mooiamaduck

+0

沒有,這是未被刪除的版本。在另一個我開始使用數組。 – SandraK

+0

你是如何調用編譯器的? – mooiamaduck

回答

2

你的代碼編譯和鏈接我的罰款與g++ 4.9.2在Ubuntu。您不可能刪除任何重要的庫,這通常需要root訪問權限。

錯誤clang: error: linker command failed with exit code 1 (use -v to see invocation)建議您可能實際上使用了clang而不是g++?在這種情況下,您的clang安裝可能有問題。如果我嘗試用普通clang我得到的錯誤編譯,因爲clang是一個C編譯器而不是C++編譯器:

/tmp/test-dc41af.o: In function `main': 
test.cpp:(.text+0xa): undefined reference to `std::cout' 
test.cpp:(.text+0x24): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' 
test.cpp:(.text+0x2a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' 
test.cpp:(.text+0x36): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))' 
/tmp/test-dc41af.o: In function `__cxx_global_var_init': 
test.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::Init()' 
test.cpp:(.text.startup+0x19): undefined reference to `std::ios_base::Init::~Init()' 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

顯而易見的解決辦法是使用clang++,再次工作正常,我有Ubuntu clang version 3.5.0-4ubuntu2 (tags/RELEASE_350/final) (based on LLVM 3.5.0)

+0

但是,我使用g ++作爲編譯。關閉所有程序並重新啓動我的電腦,這已經解決了一切。兩個版本都正確編譯。我不知道發生了什麼 – SandraK