与 gcc 链接时出错

发布于 2024-08-15 16:21:35 字数 3802 浏览 6 评论 0原文

我尝试编译此代码:

#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: 在函数中 _init': (.init+0x0): 多个_init' 的定义

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.init+0x0): 首先在这里定义

/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
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-08-22 16:21:35

您使用的是 gcc 而不是 g++ 吗?

如果 gcc 与 C++ 代码一起使用,则会出现奇怪的链接错误。 C++ 代码必须使用 g++ 编译。


编辑:根据您提供的新信息,我发现您正在运行g++ -o main main.o main.o

您应该运行:g++ -o main main.cpp

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

云柯 2024-08-22 16:21:35

您写“...它有效”,但随后您写“...链接问题”。

我对这个问题有点困惑,因为:

  • 如果链接存在问题,那么它不起作用 ...
  • 但是如果它有效,那么你就没有 > 链接问题...

所以我猜你的意思是:“它可以编译,但存在链接错误”?

如果是这种情况,那么您可以尝试

g++ -g main.cpp -o main

代替

gcc -g main.cpp -o main

编辑:...并且在命令行上提及ma​​in.o=;)

编辑:如果这一切都没有没有帮助 - 也许您的 g++/gcc 安装有问题?

在 ubuntu 上请尝试

sudo aptitude install build-essential

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文