当我尝试编译包含另一个 C header 的 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))
另一个 header ,包含 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
最佳答案
如果没有构建环境,有根据的猜测是您正在重新定义具有与 ms_abi
函数属性不兼容的原型(prototype)的内置函数。如果您使用 -ffreestand
进行编译并提供自己的函数,例如 memcpy
、memset
等,则应考虑使用 - 进行编译fno-builtin
选项,以便 CLANG/GCC 不会使用可能与您自己的函数冲突的内置函数形式。
关于c - 错误: function declared 'ms_abi' here was previously declared without calling convention (clang),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991757/