Error linking with gcc

Facon picture Facon · Dec 20, 2009 · Viewed 11.2k times · Source

I've try to compile this code:

#include <iostream>
#include <cstdlib>

using namespace std;

#define ARRAY_TAM 2

typedef int (*operacion)(int, int);
typedef const char* (*Pfchar)();

int suma(int, int);
int resta(int, int);

const char* descrSuma();
const char* descrResta();
const char* simbSuma();
const char* simbResta();

class OP
{
    private:

    public:
        operacion op;
        Pfchar descr;
        Pfchar simb;

};

int main (int argv, char *argc[])
{
    OP ArrayOP[ARRAY_TAM];

    ArrayOP[0].op = suma;
    ArrayOP[0].descr = descrSuma;
    ArrayOP[1].op = resta;
    ArrayOP[1].descr = descrResta;

    int op1, op2;
    unsigned int i;
    char opcion;
    bool fin = false;

    while (fin != true)
    {
        cout << "CALCULADORA" << "\n";
        cout << "===========" << "\n";

        for (i = 0; (i < ARRAY_TAM); i++)
        {
            cout << i+1;
            cout << ".- ";
            cout << ArrayOP[i].descr() << "\n";
        }

        cout << i+1 << ".- " << "Salir" << endl;

        cout << "Opcion: ";

        cin >> opcion;
        opcion = atoi(&opcion);
        opcion--;
        cout << (int)opcion << endl;

        if ((opcion >= 0) && (opcion < ARRAY_TAM))
        {
            cout << "Operando 1: ";
            cin >> op1;
            cout << "Operando 2: ";
            cin >> op2;
            cout << "Resultado: op1 " << ArrayOP[opcion].simb()
                         << " op2 = " << ArrayOP[opcion].op(op1, op2);
        }   
        else if (opcion == ARRAY_TAM)
        {
            fin = true;
        }

    }

    return 0;

}


int suma (int op1, int op2)
{return op1 + op2;}

int resta (int op1, int op2)
{return op1 - op2;}

const char* descrSuma()    
{return "Suma";}

const char* descrResta() 
{return "Resta";}

const char* simbSuma()
{return "+";}

const char* simbResta()
{return "-";}

An it works, but I have a lot of problems linking with gcc with debbugging symbols and it doesn't link :-(

Need Help!

Large linker error:

facon@facon-laptop:~/Windows - Mis documentos/Prog/C/Ejercicios/pedirentero$ g++ -o main main.o main.o: In function `_start':

/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:65: multiple definition of `_start'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:65:

first defined here main.o:(.rodata+0x0): multiple definition of `_fp_hw'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata+0x0): first defined here main.o: In function _fini': (.fini+0x0): multiple definition of_fini'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.fini+0x0): first defined here main.o:(.rodata+0x4): multiple definition of `_IO_stdin_used'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here main.o: In function __data_start': (.data+0x0): multiple definition of__data_start'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.data+0x0): first defined here main.o: In function __data_start': (.data+0x4): multiple definition of__dso_handle'

/usr/lib/gcc/i486-linux-gnu/4.4.1/crtbegin.o:(.data+0x0): first defined here main.o: In function _init': (.init+0x0): multiple definition of_init'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.init+0x0): first defined here

/usr/lib/gcc/i486-linux-gnu/4.4.1/crtend.o:(.dtors+0x0): multiple definition of `DTOR_END' main.o:(.dtors+0x4): first defined here

/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored. /usr/bin/ld: error in main.o(.eh_frame); no .eh_frame_hdr table will be created.

collect2: ld returned 1 exit status

PD: Edited.

Answer

Thomas Bonini picture Thomas Bonini · Dec 20, 2009

Did you use gcc instead of g++?

If gcc is used with C++ code it will give weird linking errors. C++ code must be compiled with g++.


EDIT: Based on the new information you provided I see that you are running g++ -o main main.o main.o.

You should instead run: g++ -o main main.cpp