2013-12-12 3 views
0

Я пытаюсь скомпилировать файл C++ в MPI, но это не working.It говорит:Ошибка при компиляции на C++: Неопределенная ссылка на

main.cpp:(.text+0x35d): undefined reference to `Complex::Complex(double, double)' 
main.cpp:(.text+0x384): undefined reference to `Complex::Complex(double, double)' 
main.cpp:(.text+0x3a5): undefined reference to `Complex::multiply(Complex*)' 
main.cpp:(.text+0x3b7): undefined reference to `Complex::add(Complex*)' 
main.cpp:(.text+0x3cf): undefined reference to `Complex::modul()' 
/tmp/ccoIbWIN.o: In function `f2(int)': 
main.cpp:(.text+0x53d): undefined reference to `Complex::Complex(double, double)' 
main.cpp:(.text+0x580): undefined reference to `Complex::Complex(double, double)' 
main.cpp:(.text+0x5a1): undefined reference to `Complex::multiply(Complex*)' 
main.cpp:(.text+0x5b3): undefined reference to `Complex::add(Complex*)' 
main.cpp:(.text+0x5cb): undefined reference to `Complex::modul()' 
collect2: ld returned 1 exit status 

Вот complex.h:

#include <math.h> 
#include <iostream> 

using namespace std; 

class Complex{ 
    public: 
     Complex(); 
     Complex(double real,double imag); 
     ~Complex(); 
     double real,imag; 
     double modul(); 
     void multiply(Complex *t); 
     void add(Complex *t); 
     void toString(); 

}; 

Вот Complex.cpp:

#include "Complex.h" 

Complex::Complex(){ 
    this->real = 0; 
    this->imag = 0; 
} 

Complex::Complex(double real,double imag){ 
    this->real = real; 
    this->imag = imag; 
} 

Complex::~Complex(){} 

double Complex::modul(){ 
    double realM = this->real*this->real; 
    double imagM = this->imag*this->imag; 
    return realM + imagM; 
} 

void Complex::multiply(Complex *t){ 
    this->real = this->real*t->real - this->imag*t->imag; 
    this->imag = this->real*t->imag + this->imag*t->real; 
} 


void Complex::add(Complex *t){ 
    this->real = this->real + t->real; 
    this->imag = this->imag + t->imag; 
} 

А вот главное:

#include <stdio.h> 
#include <vector> 
#include <stdlib.h> 
#include <fstream> 
#include "mpi.h" 
#include "Complex.h" 

using namespace std; 

int tip,MAX_STEPS,width,height,iteratie; 
double x_min,x_max,y_min,y_max,rezolutie,julia1,julia2; 
int thread,*matrice,*buffer; 
int nr_linii,add_master; 
int i = 0, j = 0; 

void f1(int rank){ 
    Complex *z,*c; 
    for(double y = y_min; y < y_max; y += rezolutie){ 
     for(double x = x_min; x < x_max; x += rezolutie){ 
      z = new Complex(0,0); 
      c = new Complex(x,y); 
      iteratie = 0; 
      while(z->modul() < 4 && iteratie < MAX_STEPS){    
       z->multiply(z); 
       z->add(c); 
       iteratie ++; 
      } 
     matrice[i*width + j] = iteratie % 256; 
     j++; 
     } 
    j = 0; 
    i ++;  
    if((i == nr_linii && rank != 0) || (i == height && rank == 0)) 
     break; 
    } 
} 

void f2(int rank){ 
    Complex *z,*c; 
    c = new Complex(julia1,julia2); 
    for(double y = y_min; y < y_max; y += rezolutie){ 
     for(double x = x_min; x < x_max; x += rezolutie){ 
      z = new Complex(x,y); 
      iteratie = 0; 
      while(z->modul() < 4 && iteratie < MAX_STEPS){ 
       z->multiply(z); 
       z->add(c); 
       iteratie ++; 
      } 
     matrice[i*width + j] = iteratie % 256; 
     j++; 
     } 
    j = 0; 
    if ((i == nr_linii && rank != 0) || (i == height && rank == 0)) 
       break; 
     } 
} 




int main(int argc,char *argv[]){ 
    //more code that doesn't depend on f1 or f2 
    ...... 
    // here i want to use the function f1 or f2 
    if(tip == 0){ 
     f1(rank); 

    } else { 
     f2(rank); 
    } 

return 0; 
} 

Почему это говорит «неопределенная ссылка на Комплекс ::». Что это не так? Вы можете мне помочь? Спасибо.

+1

как вы компиляции и компоновки его? –

+0

mpiC++ -o out main.cpp – JomoJo

+0

Вам также нужно скомпилировать и связать Complex.cpp. –

ответ

4

Вы не получаете ошибку компилятора, вы получаете ошибку компоновщика. Убедитесь, что вывод complex.cpp (обычно complex.o) находится в линкере (ld).

Вы, вероятно, просто нужно убедиться, что complex.cpp является частью вашей командной строки компилятора:

mpic++ main.cpp complex.cpp -o main

Смежные вопросы