我一直在使用C ++为特定函数实现一个简单的ODE求解器。一切都很好,编译得很好。然后我添加了一些调整,并保存了它。突然,旧版本和新版本都不再编译了!我一直在使用emacs.I担心我可能会意外删除一些图书馆,但我不知道这是怎么发生的! 这是我得到的错误:
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 :(得分:2)
您的代码在Ubuntu上使用g++ 4.9.2
为我编译和链接。您不太可能删除任何通常需要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)