c - Xcode 链接器错误(重复符号),而 gcc 工作正常

标签 c xcode linker-errors

正如标题所示,在 Xcode 中编译 C 代码时(尝试过版本 9.4.1 和版本 10.0 beta 6),我收到以下错误:

Ld /Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Products/Debug/list normal x86_64 (in target: list)
cd /Users/dani/Documents/Prog/Name/list
export MACOSX_DEPLOYMENT_TARGET=10.13
/Users/dani/Downloads/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Users/dani/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -L/Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Products/Debug -F/Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Products/Debug -filelist /Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Intermediates.noindex/list.build/Debug/list.build/Objects-normal/x86_64/list.LinkFileList -mmacosx-version-min=10.13 -Xlinker -object_path_lto -Xlinker /Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Intermediates.noindex/list.build/Debug/list.build/Objects-normal/x86_64/list_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -Xlinker -dependency_info -Xlinker /Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Intermediates.noindex/list.build/Debug/list.build/Objects-normal/x86_64/list_dependency_info.dat -o /Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Products/Debug/list

duplicate symbol _students in:
/Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Intermediates.noindex/list.build/Debug/list.build/Objects-normal/x86_64/main.o
/Users/dani/Library/Developer/Xcode/DerivedData/list-cqoddyqwcovrpqdkxxcsxeukjgbr/Build/Intermediates.noindex/list.build/Debug/list.build/Objects-normal/x86_64/functions.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

当使用 gcc 在终端中编译它时,我没有收到任何错误,并且程序按预期工作:

Daniels-MBP:Test dani$ gcc -Wall main.c functions.c -o prog Daniels-MBP:Test dani$ ls functions.c functions.h main.c prog view.csv

当我创建额外的 functions.cfunctions.h 文件时(所以当我只有 ma​​in.c 文件我没有任何问题)。这是代码:

ma​​in.c

#include "functions.h"

int main(int argc, const char * argv[])
{
    int lineCounter = 0;

    char currentLine[STRINGLEN];

    FILE *fp;

    fp = fopen("view.csv", "r");

    //Namen aus File einlesen

    char *currentValue;

    while(fgets(currentLine, STRINGLEN, fp))
    {
        currentValue = strtok(currentLine, ";");
        strcpy(students[lineCounter].nachname, currentValue);

        currentValue = strtok(NULL, ";");
        strcpy(students[lineCounter].vorname, currentValue);

        currentValue = strtok(NULL, ";");
        strcpy(students[lineCounter].mtrkNr, currentValue);

        ++lineCounter;
    }

    selectWinner(students, lineCounter);

    return 0;
}

函数.c

#include "functions.h"

void selectWinner (struct student students[ROWS], int lineCounter)
{
    int randomNumber = 0;

    randomNumber = rand() % 45 + 1;

    srand((unsigned int)(time(NULL)));

    for (int currentIndex = 0; currentIndex < lineCounter; ++currentIndex)
    {
        printf("%s %s %s", students[currentIndex].vorname, students[currentIndex].nachname, students[currentIndex].mtrkNr);
    }

    printf("The winner is --> %s %s\n", students[randomNumber].vorname, students[randomNumber].nachname);
}

functions.h

#ifndef functions_h
#define functions_h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

#define ROWS 50
#define STRINGLEN 40

struct student {
    char nachname[STRINGLEN];
    char vorname[STRINGLEN];
    char mtrkNr[STRINGLEN];
};

struct student students [ROWS];

void selectWinner (struct student students[ROWS], int lineCounter);

#endif /* functions_h */

对于可能出现的错误有什么建议吗? 预先感谢您!

最佳答案

functions.h 中的行 struct Student Students [ROWS]; 包含在两个 .c 文件中,它是导致链接器错误的原因。而是将该行移到两个文件之一中。

正如 @WhozCraig 提到的,将该行移动到 main.c 的顶部,它将正确编译。

关于c - Xcode 链接器错误(重复符号),而 gcc 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51996117/

相关文章:

c - 为什么修改字符串文字会导致段错误?

c - 如何在 C 中的引号中使用变量

c - 在keil中排序

ios - 如何在 Storyboard 场景中嵌入自定义 View xib?

ios - 当你发出网络请求并且它没有带来图像时如何设置默认图像?

ios - 链接器命令失败(Apple Mach-O)

c++ - 对某些 OpenGL 函数的 undefined reference (如果它们尚未在 main 中调用)

c++ - 从另一个应用程序更改表单边框

iphone - 如何在 xcode 中终止我的应用程序或重新启动它?

c++ - 在 QtCreator 中将 user32.lib 添加到 QtWidget 项目中