Search code examples
c++emacs

C++ code suddenly doesn't compile anymore


I have been implementing a simple ODE solver for a specific function using C++. All was working fine and it was compiling well. Then I added some tweaks, and saved it. Suddenly neither the old nor the new version is compiling anymore! I have been using emacs.I am worried I might have accidently deleted some libary but I have no Idea how this happend! This is the error I get:

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

And this is the code that was working perfectly fine:

/* 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, y0, t_step, 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;
    }
}

Solution

  • Your code compiles and links fine for me with g++ 4.9.2 on Ubuntu. It's unlikely you could have deleted any important libraries, that usually requires root access.

    The error clang: error: linker command failed with exit code 1 (use -v to see invocation) suggests you might actually be using clang instead of g++? In which case maybe something is wrong with your clang installation. If I try to compile with plain clang I get errors because clang is a C compiler not a C++ compiler:

    /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)
    

    The obvious fix is to use clang++, which again works fine for me with Ubuntu clang version 3.5.0-4ubuntu2 (tags/RELEASE_350/final) (based on LLVM 3.5.0)