c - 构建 C 程序时出错

标签 c xcode

这是我的 2 个源文件:

ma​​in.c:

#include <stdio.h>
#include "part2.c"

extern int var1;
extern int array1[];

int main()
{
    var1 = 4;
    array1[0] = 2;
    array1[1] = 4;
    array1[2] = 5;
    array1[3] = 7;

    display();

    printf("---------------");

    printf("Var1: %d", var1);
    printf("array elements:");

    int x;
    for(x = 0;x < 4;++x)
        printf("%d: %d", x, array1[x]);

    return 0;
}

part2.c

#include <stdio.h>

int var1;
int array1[4];

void display(void);

void display(void)
{
    printf("Var1: %d", var1);
    printf("array elements:");

    int x;
    for(x = 0;x < 4;++x)
        printf("%d: %d", x, array1[x]);
}

当我尝试编译程序时,这是我得到的:

Ld /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Products/Debug/Test normal x86_64 cd /Users/John/Xcode/Test setenv MACOSX_DEPLOYMENT_TARGET 10.7 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -L/Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Products/Debug -F/Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Products/Debug -filelist /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Intermediates/Test.build/Debug/Test.build/Objects-normal/x86_64/Test.LinkFileList -mmacosx-version-min=10.7 -o /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Products/Debug/Test

ld: duplicate symbol _display in /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Intermediates/Test.build/Debug/Test.build/Objects-normal/x86_64/part2.o and /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Intermediates/Test.build/Debug/Test.build/Objects-normal/x86_64/main.o for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

我正在使用 Xcode,这两个文件都在名为 Test 的 C 项目中

是什么导致了错误,我该如何解决?

最佳答案

删除“#include "part2.c"”并将其更改为“#include "part2.h

并移动你的函数声明:

void display(void);

从“part.c”文件的顶部到一个名为“part2.h”的文件,该文件应该包含在(或放在) 两个 .c 文件。

关于c - 构建 C 程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11252953/

相关文章:

c - 将多维数组作为参数传递给c

c、修剪字符串和宽字符

android - 使用 OpenSL ES 在 Android NDK 中实现 outputMixObject 会导致应用程序崩溃

ios - 调用 "didSelectItemAt"时从单元格内调用函数

ios - 如何通过 Swift 中的 ContainerView 更改 TableView 中项目的属性?

c - 如何将文件中的单词分配给 C 中的二维字符串数组

ios - 如何修复此 Flutter iOS 构建错误?

ios - 将项目提交到 Xcode 中的源代码管理

arrays - Swift:2 个类似错误:无法使用 'x' 类型的参数列表调用 '(([xx]) -> ())'

c - PRNG 在所有进程中返回相同的值