mac 上的 C++:链接器命令失败,退出代码为 1(使用 -v 查看调用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25596021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:22:00  来源:igfitidea点击:

C++ on mac : linker command failed with exit code 1 (use -v to see invocation)

c++xcodemacos

提问by rrr babu

Ld /Users/rahulshrestha/Library/Developer/Xcode/DerivedData/101-bdjjlwlibkuaakgjcxqoslsirofh/Build/Products/Debug/101 normal x86_64
    cd /Users/rahulshrestha/Dropbox/C++/101
    export MACOSX_DEPLOYMENT_TARGET=10.9
    /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.9.sdk -L/Users/rahulshrestha/Library/Developer/Xcode/DerivedData/101-bdjjlwlibkuaakgjcxqoslsirofh/Build/Products/Debug -F/Users/rahulshrestha/Library/Developer/Xcode/DerivedData/101-bdjjlwlibkuaakgjcxqoslsirofh/Build/Products/Debug -filelist /Users/rahulshrestha/Library/Developer/Xcode/DerivedData/101-bdjjlwlibkuaakgjcxqoslsirofh/Build/Intermediates/101.build/Debug/101.build/Objects-normal/x86_64/101.LinkFileList -mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/rahulshrestha/Library/Developer/Xcode/DerivedData/101-bdjjlwlibkuaakgjcxqoslsirofh/Build/Intermediates/101.build/Debug/101.build/Objects-normal/x86_64/101_dependency_info.dat -o /Users/rahulshrestha/Library/Developer/Xcode/DerivedData/101-bdjjlwlibkuaakgjcxqoslsirofh/Build/Products/Debug/101

duplicate symbol _main in:
    /Users/rahulshrestha/Library/Developer/Xcode/DerivedData/101-bdjjlwlibkuaakgjcxqoslsirofh/Build/Intermediates/101.build/Debug/101.build/Objects-normal/x86_64/main.o
    /Users/rahulshrestha/Library/Developer/Xcode/DerivedData/101-bdjjlwlibkuaakgjcxqoslsirofh/Build/Intermediates/101.build/Debug/101.build/Objects-normal/x86_64/praca.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)



#include <iostream>
using namespace std;

int main() {
    double radius, circumference, area; // Declare 3 floating-point variables
    const double PI = 3.14159265;       // Declare and define PI

cout << "Enter the radius: ";  // Prompting message
cin >> radius;                 // Read input into variable radius

// Compute area and circumference
area = radius * radius * PI;
circumference = 2.0 * radius * PI;

// Print the results
cout << "The radius is: " << radius << endl;
cout << "The area is: " << area << endl;
cout << "The circumference is: " << circumference << endl;

return 0;
}

回答by Sooner

You can only have one main() - you decide which one you need to keep and which one needs to be deleted. Keep only one page, with the main() method.

你只能有一个 main() - 你决定哪一个需要保留,哪一个需要删除。只保留一页,使用 main() 方法。

回答by Sooner

The error message tells you everything you need to know - you have two mains - one in main.cpp and one in praca.cpp There can be only 1 main method.

错误消息告诉您需要知道的一切 - 您有两个主方法 - 一个在 main.cpp 中,一个在 praca.cpp 中 只能有 1 个主方法。

回答by shazzing

Others on the thread have already indicated the issue here. I will try give the context.

线程上的其他人已经在这里指出了这个问题。我会尝试给出上下文。

When you compile your program the c++ compiler looks for main function definition amongst the object files it has compiled as an entry-point to invoke your program.

当您编译程序时,c++ 编译器会在它编译为调用程序的入口点的目标文件中查找主函数定义。

As your error indicates the compiler finds 2 main function definition one in praca.o(praca.cpp ?) and the other in main.o(main.cpp ?)

由于您的错误表明编译器在 praca.o(praca.cpp ?) 中找到 2 个主函数定义,另一个在 main.o(main.cpp ?)

So you have to choose either the main in main.cpp or praca.cpp and remove the other.

所以你必须在 main.cpp 或 praca.cpp 中选择 main 并删除另一个。