c - 错误: function declared 'ms_abi' here was previously declared without calling convention (clang)

标签 c clang abi osdev freestanding

当我尝试编译包含另一个 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.hlibk.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 进行编译并提供自己的函数,例如 memcpymemset 等,则应考虑使用 - 进行编译fno-builtin 选项,以便 CLANG/GCC 不会使用可能与您自己的函数冲突的内置函数形式。

关于c - 错误: function declared 'ms_abi' here was previously declared without calling convention (clang),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991757/

相关文章:

C: 信号量 - 如何仅在 n 次回调后释放信号量

python - LLVM clang 的 CMake 构建失败并显示 "Unexpected failure executing llvm-build: Traceback (...) import llvmbuild"

c++ - 跨平台汇编 ((x64 || x86) && (Microsoft x64 || SystemV))

gcc - amd64 中的传递参数如何?

c - 不将字符串从文件复制到链接列表参数

c - c中的指针运算和二维数组?

c - intt *p=a 和 int *p1=&a 之间的区别

c++ - 替换函数 'operator new' 无法声明 'inline' [-Werror,-Winline-new-delete]

c++ - 为什么 clang++ 和 gcc/g++ 生成不同链接的可执行文件

windows - 基于表的异常处理与 32 位 Windows SEH 相比有何优势?