简体   繁体   English

错误编译C程序

[英]error compiling C program

I'm trying to write a program that gets two pointers , and a count of bytes, and compares them . 我正在尝试编写一个程序,该程序获得两个指针和一个字节数,并对其进行比较。 for example , if we have p1 and p2 , and our count is 2 , then we'll check the 2 bytes p1[0]+p1[1] and p2[0]+p1[1]. 例如,如果我们有p1和p2,并且计数为2,那么我们将检查2个字节p1 [0] + p1 [1]和p2 [0] + p1 [1]。

mem_cmp : 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: 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;
   }
}

when I try to compile I get this : 当我尝试编译时,我得到这个:

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

EDIT: this is the makefile: 编辑:这是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

and now this is the error log I see: 现在这是我看到的错误日志:

    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

EDIT 2: 编辑2:

no I get only tose warnings : 不,我只收到警告:

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

<string.h> contains the standard function memcmp() . <string.h>包含标准函数memcmp() So its prototype conflicts with your version of memcmp() . 因此其原型与您的memcmp()版本冲突。

Also, your function prototype is missing a semi-colin ; 另外,您的函数原型缺少半分号; .

Rename your function and add semi-colon: 重命名函数并添加分号:

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

and

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

When you declare: 当您声明:

   char* p1Char, p2Char;

only p1Char is a pointer; 只有p1Char是指针; p2Char is just a char. p2Char只是一个字符。 You want: 你要:

   char* p1Char, *p2Char;

Also, note you need <string.h> since you are using memcpy() . 另外,请注意,由于您正在使用memcpy() ,因此需要<string.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM