错误编译C程序

时间:2015-12-05 11:13:36

标签: c compilation

我试图编写一个获得两个指针和一个字节数的程序,并对它们进行比较。例如,如果我们有p1和p2,并且我们的计数是2,那么我们将检查2个字节p1 [0] + p1 [1]和p2 [0] + p1 [1]。

mem_cmp:

#include <stdio.h>

int memcmp(const void *p1, const void *p2, int count)

int main()
{
   char p1[5];
   char p2[5];
   int lexValue;

    memcpy(p1, "01Aa", 5);
    memcpy(p2, "01aA", 5);   

   lexValue = memcmp( p1, p2, 5 );
   if( lexValue == 0 )
        printf( "Both are equal" );
   else if( lexValue < 0 )
    printf( "p2 is bigger than p1" );
   else
    printf( "p1 is bigger than p2" );

return 0;
}

memcmp:

    int memcmp(const void *p1, const void *p2, int count)
{
   char *p1Char;
   char *p2Char;
   if ( count == 0 )
    return 0;
   p1Char = (char *)p1;
   p2Char = (char *)p2;

   if( *p1Char == *p2Char )
       return memcmp( p1Char+1, p2Char+1, count-1 );
   else
   {
       if( *p1Char > *p2Char )
           return 1;     
       else
       return -1;
   }
}

当我尝试编译时,我得到了这个:

gcc -g -ansi -Wall -pedantic memcmp.c -o memcmp.o
memcmp.c: In function ‘memcmp’:
memcmp.c:11:7: warning: statement with no effect [-Wunused-value]
memcmp.c:19:1: warning: control reaches end of non-void function [-Wreturn-type]
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [memcmp.o] Error 1

编辑: 这是makefile:

memcmp :     mem_cmp.o memcmp.o
              gcc -g -ansi -Wall -pedantic mem_cmp.o memcmp.o -o memcmp
mem_cmp.o:   mem_cmp.c
          gcc -c -ansi -Wall -pedantic mem_cmp.c -o mem_cmp.o
memcmp.o:    memcmp.c
          gcc -c -ansi -Wall -pedantic memcmp.c -o memcmp.o

现在这是我看到的错误日志:

    gcc -g -ansi -Wall -pedantic mem_cmp.o memcmp.o -o memcmp
mem_cmp.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.fini+0x0): first defined here
mem_cmp.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.data+0x0): first defined here
mem_cmp.o: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
mem_cmp.o:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
mem_cmp.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.text+0x0): first defined here
mem_cmp.o:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata+0x0): first defined here
mem_cmp.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i686-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
mem_cmp.o:(.dtors+0x4): first defined here
/usr/bin/ld: error in mem_cmp.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
make: *** [memcmp] Error 1

编辑2:

不,我只收到警告:

mem_cmp.c:11:2: warning: implicit declaration of function ‘memcpy’ [-Wimplicit-function-declaration]
mem_cmp.c:11:2: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
gcc -g -ansi -Wall -pedantic mem_cmp.o memcmp.o -o memcmp

1 个答案:

答案 0 :(得分:1)

<string.h>包含标准函数memcmp()。因此,它的原型与您的memcmp()版本冲突。

此外,您的函数原型缺少一个半列;

重命名您的函数并添加分号:

int my_memcmp(const void *p1, const void *p2, int count);

int my_memcmp(const void *p1, const void *p2, int count) {
   ...
   ...
}

宣布:

   char* p1Char, p2Char;

只有p1Char是一个指针; p2Char只是一个字符。你想要:

   char* p1Char, *p2Char;

此外,请注意,由于您使用的是<string.h>,因此需要memcpy()