当我尝试编译包含另一个C头的C代码时,我收到此错误:
x86_64-uefi/../../libk/string.h:9:10: error: function declared 'ms_abi' here was
previously declared without calling convention
KABI int memcmp(const void *d1, const void *d2, uint64_t len);
^
x86_64-uefi/../../libk/string.h:9:10: note: previous declaration is here
编译器是clang,涉及的文件如下:
memcmp.c
#include "../string.h"
KABI int memcmp(const void *d1, const void *d2, uint64_t len) {
const uint8_t *d1_ = d1, *d2_ = d2;
for(uint64_t i = 0; i < len; i += 1, d1_++, d2_++){
if(*d1_ != *d2_) return *d1_ < *d2_ ? -1 : 1;
}
return 0;
}
string.h
#pragma once
#include "systemapi.h"
#include "typedefs.h"
KABI int memcmp(const void *d1, const void *d2, uint64_t len);
systemapi.h
(typedef只定义uintx_t类型)
#pragma once
#define KABI __attribute__((ms_abi))
包含string.h
,libk.h
#pragma once
#include "string.h"
#include "systemapi.h"
#include "typedefs.h"
包含lib.h并在编译时报告错误的文件main.c
(但所有文件在与lib.h
链接时报告错误)
KABI void arch_main(void)
{
// The function does not uses memcmp, just uses the KABI part of lib.h
// Calling the whole lib.h is a convention
}
编译器的标志:-I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -fno-stack-protector -fpic -fshort-wchar -mno-red-zone -DHAVE_USE_MS_ABI -c main.c -o main.o
答案 0 :(得分:2)
如果没有您的构建环境,一个有根据的猜测是您正在重新定义具有与ms_abi
函数属性不兼容的原型的内置函数。如果您使用-ffreestanding
进行编译并提供自己的函数,例如memcpy
,memset
等,则应考虑使用-fno-builtin
选项进行编译,以便CLANG / GCC不会使用其内置形式的函数可能与您自己的函数冲突。