尝试在Eclipse (MacOSX)中编译时出错
Error When Trying to Compile in Eclipse (MacOSX)
我正在学习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相同:重新启动它,重做出错的地方。
相关文章:
- 在Linux for Windows上编译C++代码时出错
- 为x86而非x64编译时出错
- 当我尝试通过 mingw 使用 CMake 和 SFML 库编译项目时出错
- 尝试在Windows上执行C++时出错(通过MinGW编译)
- 如何使用我构建的库,而不会从源代码出错,但不为我自己的项目编译?
- constexpr 使用 clang 编译 TensorFlow 时出错
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 编译时出错 - 从 DWORD 到 LPCVOID 的转换
- 在发布模式下启动使用库的静态链接编译的应用程序时出错
- 编译时出错 - 链接.cpp和头文件
- <limits> 使用 VSC2019 编译旧代码时出错
- 在 ubuntu 焦点上编译虚幻引擎 4.25 时出错
- 编译包含LLVM API标头的C++文件时出错
- 编译以下代码时出错
- 使用交叉工具ng编译gcc时出错
- 在 dll 中为 qLibrary 编译 qobject 时出错
- QML/C++编译出错
- Proc*C编译出错
- 应用程序在编译出错后执行时关闭,但在调试时运行良好