与 gcc 链接时出错
我尝试编译此代码:
#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 "-";}
它可以工作,但我在使用调试符号与 gcc 链接时遇到很多问题,并且它不链接:-(
需要帮助!
大链接器错误:
facon@facon-laptop:~/Windows - Mis documentos/Prog/C/Ejercicios/pedirentero$ g++ -o main main.o main.o:在函数中 `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:65: `_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:
首先在这里定义 main.o:(.rodata+0x0): 多个 `_fp_hw' 的定义
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata+0x0): 首先在这里定义 main.o: 在函数中_fini': (.fini+0x0): 多个 定义
_fini'/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.fini+0x0): 首先在这里定义 main.o:(.rodata+0x4): 多个 “_IO_stdin_used”的定义/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata.cst4+0x0): 首先在这里定义 main.o: 在函数中
__data_start': (.data+0x0): 多个 __data_start' 的定义
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.data+0x0): 首先在这里定义 main.o: 在函数中
__data_start': (.data+0x4): 多个 __dso_handle' 的定义
/usr/lib/gcc/i486-linux-gnu/4.4.1/crtbegin.o:(.data+0x0): 首先在这里定义 main.o: 在函数中
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.init+0x0): 首先在这里定义_init': (.init+0x0): 多个
_init' 的定义/usr/lib/gcc/i486-linux-gnu/4.4.1/crtend.o:(.dtors+0x0): “DTOR_END”的多个定义 main.o:(.dtors+0x4): 首先在这里定义
/usr/bin/ld:警告:不能 创建 .eh_frame_hdr 部分, --eh-frame-hdr 被忽略。 /usr/bin/ld: main.o(.eh_frame) 中出现错误;不 .eh_frame_hdr 表将被创建。
collect2: ld 返回 1 退出状态
PD:已编辑。
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
_fini'
definition of/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
__data_start'
definition of/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
__dso_handle'
definition of/usr/lib/gcc/i486-linux-gnu/4.4.1/crtbegin.o:(.data+0x0):
first defined here main.o: In function_init': (.init+0x0): multiple
_init'
definition of/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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是
gcc
而不是g++
吗?如果
gcc
与 C++ 代码一起使用,则会出现奇怪的链接错误。 C++ 代码必须使用g++
编译。编辑:根据您提供的新信息,我发现您正在运行
g++ -o main main.o main.o
。您应该运行:
g++ -o main main.cpp
Did you use
gcc
instead ofg++
?If
gcc
is used with C++ code it will give weird linking errors. C++ code must be compiled withg++
.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
您写“...它有效”,但随后您写“...链接问题”。
我对这个问题有点困惑,因为:
所以我猜你的意思是:“它可以编译,但存在链接错误”?
如果是这种情况,那么您可以尝试
代替
编辑:...并且在命令行上不提及main.o=;)
编辑:如果这一切都没有没有帮助 - 也许您的 g++/gcc 安装有问题?
在 ubuntu 上请尝试
You write "... it works", but then you write "... problems with linking".
I am little bit confused with this question, because:
So I guess that you mean: "it compiles, but there are linking errors" ?
If that's the case, then you could try
instead of
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