gpt4 book ai didi

c - 编译C程序出错

转载 作者:行者123 更新时间:2023-11-30 15:14:20 24 4
gpt4 key购买 nike

我正在尝试编写一个程序来获取两个指针和字节数,并对它们进行比较。例如,如果我们有 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

编辑:这是生成文件:

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

最佳答案

<string.h>包含标准功能memcmp() 。所以它的原型(prototype)与你的memcmp()版本冲突.

此外,您的函数原型(prototype)缺少分号 ; .

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

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() .

关于c - 编译C程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34104171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com