简体   繁体   English

ld linker 错误,未定义对主要 function 的引用

[英]ld linker error , undefined refrence to main function

My problem is not with the program but I put it in case it is necessary.我的问题不在于程序,但我把它放在了必要的情况下。 I always used VS on the Windows version, now I'm using a Linux version.我一直在 Windows 版本上使用 VS,现在我使用的是 Linux 版本。 I'm having a problem so that a simple program recognizes the methods of a class, an error appears: A simpler program, without an.h header, compiles normally.我遇到了一个问题,所以一个简单的程序可以识别 class 的方法,出现错误:一个更简单的程序,没有 an.h header,编译正常。 What do I need to do more to fix this problem?我还需要做些什么来解决这个问题? In the Windows version, the program runs在Windows版本中,程序运行

The error:错误:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: in the `_start 'function:
(.text + 0x20): undefined reference to `main '
collect2: error: ld returned 1 exit status

[main.cpp 2020-05-23 17: 28: 09.707]
,, / tmp / ccJJYd6a.o: in the `main 'function:
main.cpp :(. text + 0x1f): undefined reference to `Complex :: Complex () '
main.cpp :(. text + 0x2b): undefined reference to `Complex :: Complex () '
main.cpp :(. text + 0x37): undefined reference to `Complex :: Complex () '
main.cpp :(. text + 0x43): undefined reference to `Complex :: lerComplexo () '
main.cpp :(. text + 0x4f): undefined reference to `Complex :: print () '
main.cpp :(. text + 0x5b): undefined reference to `Complex :: lerComplexo () '
main.cpp :(. text + 0x67): undefined reference to `Complex :: print () '
collect2: error: ld returned 1 exit status

Main.cpp主文件

    #include stdio.h
    #include"Complexo.h"

     int main()
    {
     Complexo a,b,c;

a.lerComplexo();
a.imprimir();

b.lerComplexo();
b.imprimir();



 }

Complexo.cpp复杂的cpp

#define _CRT_SECURE_NO_WARNINGS
#include "Complexo.h"
#include<stdio.h>
#include<math.h>


 Complexo::Complexo()
 {
   re = 0;
   im = 0;
 }

 Complexo::Complexo(float r, float i)
 {
   re = r;
   im = i;
 }

 void Complexo::atribuir(float r, float i)
 {
   float novo_re = r;
   float novo_im = i;
 }

 void Complexo::lerComplexo()
 {
   printf("Parte real:");
   scanf("%f", &re);
   printf("\n");
   printf("Parte imaginaria:");
   scanf("%f", &im);
   printf("\n");

 }

 void Complexo::imprimir()   // escrever complexo para o monitor  
 {
   printf("%.1f +%.1fi\t", re, im);
 }


 float Complexo::modulo()  // devolve módulo do número 
 {
   float m;
   m = float(sqrt(pow(re, 2) + pow(im, 2)));
   return m;
 }
 float Complexo::argumento() // devolve argumento do número 
 {
   float arg;
   arg = float(atan2(im, re));
   return arg;
 }

 Complexo Complexo::conjugado()  // devolve o conjugado do número  
 {
   Complexo c;
   c.re = re;
   c.im = -im;
   return c;
  }
 Complexo Complexo::simetrico()  // devolve o simétrico do numero
 {
   Complexo s;
   s.re = re * (-1);
   s.im = im * (-1);
   return s;
 }
 Complexo Complexo::potencia(float exp)  // devolve potência do número
 {
   Complexo p; 
   float mod;
   mod = pow(modulo(), exp);
   p.re = mod * cos(exp * argumento());
   p.im = mod * sin(exp * argumento());
   return p;
 }

       // operações entre 2 complexos  
 Complexo Complexo::operator +(const Complexo& c) // adição   
 {
   Complexo res;
   res.re = re + c.re;
   res.im = im + c.im;
   return res;

  }
  Complexo Complexo::operator -(const Complexo& c) // subtração  
  {
    Complexo res;
    res.re = re - c.re;
    res.im = im - c.im;
    return res;
   }
  Complexo Complexo::operator *(const Complexo& c) // multiplicação 
  {
    Complexo res;
    res.re = re * c.re - im * c.im;
    res.im = re * c.im + im * c.re;
    return res;
   }
  Complexo Complexo::operator /(const Complexo& c) // divisão    
 {
    Complexo num, den, res;
    num = Complexo(re, im);
    den = c;
    num = *this * den.conjugado();
    den = den * den.conjugado();
    return Complexo(num.re / den.re, num.im / den.re);



    }

    // operações entre um complexo (operador da esquerda) e um real (operador da  // 
        direita)  

  Complexo Complexo::operator *(float f)
  {
    Complexo res;
    res.re = re * f;
    res.im = im * f;
    return res;

   }
  Complexo Complexo::operator /(float f)
  {
    Complexo res;
    res.re = re / f;
    res.im = im / f;
    return res;

  }

Complexo.h复杂的.h

        #pragma once  
        class Complexo
        {
        private:
          float re, im;
        public:
          Complexo();   // Construtor cria um número nulo  
          Complexo(float r, float i);

    void atribuir(float r, float i);
    void lerComplexo();  // ler complexo do teclado  
    void imprimir();   // escrever complexo para o monitor  

    float real() { return re; } // devolve coeficiente real  
    float imaginario() { return im; } // devolve coeficiente imaginário  
    float modulo();  // devolve módulo do número  
    float argumento(); // devolve argumento do número  
    Complexo conjugado();  // devolve o conjugado do número  
    Complexo simetrico();  // devolve o simétrico do número 
    Complexo potencia(float exp);  // devolve potência do número 

    // operações entre 2 complexos  
    Complexo operator +(const Complexo& c); // adição   
    Complexo operator -(const Complexo& c); // subtração  
    Complexo operator *(const Complexo& c); // multiplicação  
    Complexo operator /(const Complexo& c); // divisão     

    // operações entre um complexo (operador da esquerda) e um real (operador da  // direita)  
    Complexo operator *(float f);
    Complexo operator /(float f);

};

Did you compile the main.cpp at all?编译main.cpp吗? for example, on linux/g++ compiler the compilation command line should looks at least like below: g++ main.cpp complexo.cpp -pedantic -Wall -Wconversion例如,在 linux/g++ 编译器上,编译命令行至少应如下所示: g++ main.cpp complexo.cpp -pedantic -Wall -Wconversion

remarks Including C headers in C++ is a bad thing to do in most cases.备注在大多数情况下,在 C++ 中包含 C 接头是一件坏事。 for example you have included <stdio.h> , and you dont need it, because c++ did all the work for you in <iostream> and you should use its functionality (like cin, cout objects and more)!例如,您已包含<stdio.h> ,但您不需要它,因为 c++ 在<iostream>中为您完成了所有工作,您应该使用它的功能(如cin, cout objects等)!

general tip: always compile with the flags above.一般提示:始终使用上面的标志进行编译。 and let the compiler helps you.让编译器帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM