1

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.

2
  • 5
    Please edit your code to remove the unecessary vertical white space. And "a lot of problems" doesn't give us anything to go on - post the specific error messages you are getting. Commented Dec 20, 2009 at 14:23
  • Sorry, I'll do that the next time Commented Dec 20, 2009 at 14:43

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

6 Comments

It do the same with gcc a g++.
What is the "same"? You never explained exactly what the error is
In that screenshot you ran main.o again instead of main.cpp
please do not use: g++ -g main.cpp main.o. That's wrong. use: g++ -g main.cpp -o main
|
2

You write "... it works", but then you write "... problems with linking".

I am little bit confused with this question, because:

  • If there are problems with linking then it doesn't work ...
  • But if it works, then you don't have problems with linking...

So I guess that you mean: "it compiles, but there are linking errors" ?

If that's the case, then you could try

g++ -g main.cpp -o main 

instead of

gcc -g main.cpp -o main 

EDIT: ... and do not mention main.o on the command line =;)

EDIT: if that all doesn't help - maybe there is something wrong with your g++/gcc installation?

on ubuntu please try

sudo aptitude install build-essential 

6 Comments

It do the same at linking, I think, I haven't got the debugging library or something like that.
what kind of development environment is this? linux? cygwin? mingw?
which version of Ubuntu? karmic? Is it a 32bit or a 64bit Ubuntu?
... and how did you install gcc/g++ ? I would recommend that you use sudo aptitude install build-essential
Karmic Koala 9.10, 32 Bits. I did that when I started programming in C/C++
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.