I am getting the error: ld returned 1 exit status while trying to use make

I am getting the error: ld returned 1 exit status while trying to use make

我在尝试使用我的 makefile 运行 我的代码时遇到此错误:

find_bits.o: In function `__x86.get_pc_thunk.bx':
(.text+0x30): multiple definition of `__x86.get_pc_thunk.bx'
/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crti.o:(.gnu.linkonce.t.__x86.get_pc_thunk.bx+0x0): first defined here
find_bits.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crti.o:(.fini+0x0): first defined here
find_bits.o: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crt1.o:(.data+0x0): first defined here
find_bits.o: In function `data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i686-linux-gnu/5/crtbegin.o:(.data+0x0): first defined here
find_bits.o:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
find_bits.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crt1.o:(.text+0x0): first defined here
find_bits.o:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crt1.o:(.rodata+0x0): first defined here
find_bits.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i686-linux-gnu/5/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
find_bits.o:(.data+0x8): first defined here
/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.
/usr/bin/ld: error in find_bits.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'open_bits' failed
make: *** [open_bits] Error 1

我的 makefile 代码:

open_bits: find_bits.o
    gcc -g -ansi -pedantic -Wall find_bits.o -o open_bits
find_bits.o: find_bits.c
    gcc -g -ansi -pedantic -Wall find_bits.c -o find_bits.o

我的实际代码:

#include <stdio.h>
int main()
{/*main*/
    /*Creating the 2 long numbers that will get compared
    also creating the count variable to store the amount of equal activated bits in the numbers
    and another one that will have the decimal value of x&y*/
    unsigned long x,y;
    int count, decimal_outcome;
    count = 0;
    printf("\n Enter 2 numbers in the form of 1, 2: ");/*get the numbers from the user*/
    scanf("%lu,%lu",&x,&y);
    printf("\n You have entered the numbers: %lu, %lu",x,y);
    decimal_outcome = x&y;
    /*The while loop will convert the decimal outcome of x&y to binary
    while checking the open bits and adding to count*/
    while(decimal_outcome!=0){/*while*/
        if(decimal_outcome%2&1)
            count++;
        decimal_outcome = decimal_outcome/2;
    }/*while*/
    printf("\n x and y have %d activated bits in the same spots \n", count);/*printing the amount of equal activated bits*/
    return 0;
}/*main*/

我搜索了这个问题的解决方案,但似乎没有找到与我遇到的问题完全相同的解决方案。 我对使用 C 进行编码也很陌生,所以如果我的错误是我不应该用该语言的一些常识了解的错误,我很抱歉打扰你我的问题。

您应该添加 -c 选项来创建目标文件而不是可执行二进制文件。

find_bits.o: find_bits.c
    gcc -c -g -ansi -pedantic -Wall find_bits.c -o find_bits.o

这个make规则...

find_bits.o: find_bits.c
    gcc -g -ansi -pedantic -Wall find_bits.c -o find_bits.o

... 构建并 link 一个名为 find_bits.o程序。那么这条规则...

open_bits: find_bits.o
    gcc -g -ansi -pedantic -Wall find_bits.o -o open_bits

... 要求程序 find_bits.o 像目标文件一样被 link 编辑,以形成程序 open_bits.

使用 -c 选项编译为目标文件,而无需继续 link 结果:

CFLAGS = -g -ansi -pedantic -Wall

open_bits: find_bits.o
        gcc $(CFLAGS) $^ -o $@

find_bits.o: find_bits.c
        gcc $(CFLAGS) -c $< -o $@