gpt4 book ai didi

c 在 xcode : Redeclaration of function must have the overloadable attribute error

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:37 26 4
gpt4 key购买 nike

当我在终端中使用 gcc(带有 -Wall -Werror 标志)运行我的 C 代码时,它没有给我任何错误。但是,当我在 Xcode 中运行我的代码时,它只在两个函数上给出了上述错误。这是怎么回事?

ma​​in.c

#include "canvas.h"
#include "paint.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

void program_loop(Canvas * canvas) {
// ...
}

int main(int argc, char ** argv) {
// ...
return 0;
}

Canvas .h

#ifndef CANVAS_H
#define CANVAS_H

typedef struct {
// ...
} Canvas;

void create_canvas(Canvas * canvas, int row_num, int col_num);
void print_canvas(Canvas * canvas);
void write(Canvas * canvas); // "Previous overload of function is here."
void write_horizontal(Canvas * canvas, int row, int start_column, int end_column);
void write_vertical(Canvas * canvas, int start_row, int end_row, int column);
void write_right_diagonal(Canvas * canvas, int start_row, int end_row, int start_column, int end_column);
void write_left_diagonal(Canvas * canvas, int start_row, int end_row, int start_column, int end_column);
void erase(Canvas * canvas); // "Previous overload of function is here."
void resize(Canvas * canvas);
void add(Canvas * canvas);
void del(Canvas * canvas);
void clear_canvas(Canvas * canvas);

#endif

Canvas .c

#include "canvas.h"
#include <stdio.h>
#include <stdlib.h>

void create_canvas(Canvas * canvas, int row_num, int col_num) {
// ...
}

void print_canvas(Canvas * canvas) {
// ...
}

void write(Canvas * canvas) { // Error here.
// ...
}

void write_horizontal(Canvas * canvas, int row, int start_column, int end_column) {
// ...
}

void write_vertical(Canvas * canvas, int start_row, int end_row, int column) {
// ...
}

void write_right_diagonal(Canvas * canvas, int start_row, int end_row, int start_column, int end_column) {
// ...
}

void write_left_diagonal(Canvas * canvas, int start_row, int end_row, int start_column, int end_column) {
// ...
}

void erase(Canvas * canvas) { // Error here.
// ...
}

void resize(Canvas * canvas) {
// ...
}

void add(Canvas * canvas) {
// ...
}

void del(Canvas * canvas) {
// ...
}

void clear_canvas(Canvas * canvas) {
// ...
}

paint.h

#ifndef PAINT_H
#define PAINT_H

#include "canvas.h"

// ...

#endif

paint.c

#include "paint.h"
#include <stdio.h>
#include <stdlib.h>

// ...

错误发生在 void write(Canvas * canvas) 和 void erase(Canvas * canvas) 函数中。任何帮助将不胜感激!

最佳答案

首先,请注意 write(可能还有 erase)已经在某些系统头文件中定义。具体来说,在unistd.h中,有:

ssize_t write(int __fd, const void * __buf, size_t __nbyte)

在这个 question 中,我们发现重载在 C 中无法开箱即用。因此,这里有两个不同的函数,名为 write ,具有不同的类型,普通 C 不支持。

现在,我们有一个特殊的情况,当您在命令行通过 clang 编译时它可以工作:

clang -c canvas.c

不会收到错误,但通过 Xcode 您会收到。那么,在 Xcode 中你可以查看 Xcode 执行的实际编译命令,对我来说它看起来像:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-x c -arch x86_64 -fmessage-length=0
-fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0
-std=gnu99 -fmodules -gmodules
-fmodules-cache-path=/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/ModuleCache
-fmodules-prune-interval=86400 -fmodules-prune-after=345600
-fbuild-session-file=/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/ModuleCache/Session.modulevalidation
-fmodules-validate-once-per-build-session
-Wnon-modular-include-in-framework-module
-Werror=non-modular-include-in-framework-module -Wno-trigraphs
-fpascal-strings -O0 -fno-common -Wno-missing-field-initializers
-Wno-missing-prototypes -Werror=return-type -Wdocumentation
-Wunreachable-code -Werror=deprecated-objc-isa-usage
-Werror=objc-root-class -Wno-missing-braces -Wparentheses -Wswitch
-Wunused-function -Wno-unused-label -Wno-unused-parameter
-Wunused-variable -Wunused-value -Wempty-body
-Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow
-Wno-four-char-constants -Wno-conversion -Wconstant-conversion
-Wint-conversion -Wbool-conversion -Wenum-conversion
-Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -DDEBUG=1 -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk
-fasm-blocks -fstrict-aliasing -Wdeprecated-declarations
-mmacosx-version-min=10.12 -g -Wno-sign-conversion
-Winfinite-recursion -iquote
/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/paint-generated-files.hmap
-I/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/paint-own-target-headers.hmap
-I/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/paint-all-target-headers.hmap
-iquote
/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/paint-project-headers.hmap
-I/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Products/Debug/include
-I/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/DerivedSources/x86_64
-I/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/DerivedSources
-F/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Products/Debug
-MMD -MT dependencies -MF
/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/Objects-normal/x86_64/canvas.d
--serialize-diagnostics
/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/Objects-normal/x86_64/canvas.dia
-c /Users/donaldpinckney/Stack/paint/canvas.c -o
/Users/donaldpinckney/Library/Developer/Xcode/DerivedData/paint-fovqgkhrrknzbxemgzpydqtwbtge/Build/Intermediates/paint.build/Debug/paint.build/Objects-normal/x86_64/canvas.o

这告诉我们,虽然 Xcode 使用 Clang,但它还使用 Clang,并提供比 -Wall -Werror 更多的选项。这种情况下的重要标志是 -fmodules,它启用 modules 的 Clang 语言功能。这将改变 Xcode 包含系统库的方式。我不知道为什么这特别会导致错误,但确实如此。您可以通过运行来测试它:

clang -fmodules -c canvas.c

你会得到同样的错误。

要修复它,您可以执行以下两项操作之一:

  1. 将这些函数重命名为其他名称。
  2. 使用 Clang overloadable attribute ,它看起来像:

canvas.h:

// ...
void __attribute__((overloadable)) write(Canvas * canvas);
// ...

canvas.c:

// ...
void __attribute__((overloadable)) write(Canvas * canvas) {
// ...
}
// ..

关于c 在 xcode : Redeclaration of function must have the overloadable attribute error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41249041/

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