正如标题所示,在 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.c 和 functions.h 文件时(所以当我只有 main.c强> 文件我没有任何问题)。这是代码:
main.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/