尝试在Eclipse (MacOSX)中编译时出错

Error When Trying to Compile in Eclipse (MacOSX)

本文关键字:编译 出错 MacOSX Eclipse      更新时间:2023-10-16

我正在学习Eclipse和c++,并且正在编写一个非常简单的程序。我现在不能建立这个程序。我最近刚刚安装了HomeBrew,所以我不知道这是否有什么关系。这是我的代码(它来自一个简单的教程,我正在工作,所以我知道代码工作,因为我已经看到它的工作在教程):

#include <iostream>
#include <string>
using namespace std;
int main()
{
    // Ask the user to input a word while the word length < 5
    string word = "";
    do
    {
        cout << "Enter a word that has at least 5 characters: " << endl;
        cin >> word;
    }while(word.size() < 5);
    // Ask the user to input a character
    char searchCh = '0';
    cout << "Enter a character and the program will tell" <<
        " you how many times it appears in the word " << word << "." << endl;
    cin >> searchCh;
    int counter = 0;
    // Iterate over the word
    for(int i = 0; i < word.size(); i++)
    {
        // Get a character
        char ch = word.at(i);
        // If the character matches the character we're looking for
        if(searchCh == ch)
        {
            // Increment a counter
            counter++;
        }
    }
    // Output the number of times the character was found in the word.
    cout << "The number of " << searchCh << "'s in the word " << word << " is " << counter << "n";
    return 0;       
}

当构建时我得到的错误是:

g++  -o "Program 5"  ./Program5.o   
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Program 5] Error 1

我在这里看到了其他几个类似的问题,关于去"项目"answers"属性",但我不知道到底应该更新什么设置来获得这个工作。此外,其他一些答案提到需要一个main()函数来解决这个问题,但我显然在这段代码中有一个。当我上周在其他小项目上使用Eclipse时,它工作得很好,所以我不确定是什么改变了。

更多信息:当我第一次设置项目时,它被设置为"空项目"项目类型,"MacOSX GCC"作为工具链。然后,我在项目中创建了一个源文件(扩展名为.cpp),编写了代码,然后这就是我在哪里。

任何帮助都将非常感激。谢谢!

尝试重置您的项目。有时安装构建链会混淆eclipse,使它使用错误的库——特别是当它期待另一个版本时。

我认为这里发生的事情是eclipse混淆了使用不包含c++文件的构建路径(由于升级移动它们?),而是包含C文件,这些文件在正常情况下不与c++链接。

正如另一位评论者所提到的,这也可能是由于某种原因而没有清理初始构建失败的结果。

有时,eclipse的解决方案与Windows相同:重新启动它,重做出错的地方。