gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:43 26 4
gpt4 key购买 nike

我正在学习 Eclipse 和 C++,并且正在开发一个极其简单的程序。我现在无法构建程序。我最近刚刚安装了 HomeBrew,所以我不知道这是否与它有关。这是我的代码(它来 self 正在使用的一个简单教程,所以我知道代码有效,因为我已经看到它在教程中有效):

#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

我在这里看到了其他几个关于转到“项目”和“属性”的类似问题,但我不知道究竟应该更新哪些设置才能使其正常工作。此外,其他一些答案提到需要一个 main() 函数来解决这个问题,但我显然在这段代码中有一个。上周我在其他小项目上使用 Eclipse 时它运行良好,所以我不确定发生了什么变化。

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

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

尝试重置您的项目。有时安装 buildchains 会混淆 eclipse 并使其使用错误的库 - 特别是如果它需要另一个版本。

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

another commenter 也提到了,这也可能是由于某种原因未清理初始失败构建的结果。

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

关于c++ - 尝试在 Eclipse (MacOSX) 中编译时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25224488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com