unsigned long strcpy0(char* dst,const char* src)
{
__asm__ __volatile__
(
".intel_syntax noprefix\n"
"POS2:\n"
"lodsb\n"
"stosb\n"
"test al,al\n"
"jnz POS2\n"
:
:
:"rax","bl"
);
}
上面代码使用gcc编译的时候报错,请问该怎么解决:
gcc -masm=intel -O3 -c -o test.o test.c
test.c: Assembler messages:
test.c:7: Error: symbol POS2' is already defined
POS2' is already defined
test.c:7: Error: symbol
test.c:7: Error: symbol `POS2' is already defined
错误信息说标号POS2重复定义了,可能你在源码其他位置也定义了这个符号。我把你这段代码复制下来进行编译时是正常的,并没有报错。
unsigned long strcpy0(char* dst,const char* src)
{
__asm__ __volatile__
(
".intel_syntax noprefix\n"
"POS_1:\n"
"lodsb\n"
"stosb\n"
"test al,al\n"
"jnz POS_1\n"
:
:
:"rax","bl"
);
}
int main()
{
char dst[1024] = {0};
char src[1024] = {0};
memset(src,1,1023);
time_t timbeg = time(0);
for(int i = 0;i < 100*10000;i++)
{
for(int j = 0;j < 3;j++)
{
strcpy0(dst,src);
}
}
printf("==%lu==\n",time(0) - timbeg);
}
已经解决,函数的声明和定义放在了一起导致gcc加-O进行优化时,直接内联函数,并对内层循环进行了展开处理,造成了同一个标号定义了多次。
解决方法是把函数的声明和定义放在不同的文件中。