现在我们将使用GNU C ++编译器编译该文件。我们 用
g++
调用编译器。因此发出命令g++ Lab4.cpp
。现在做 一个ls
你会得到什么?
当我发出这个命令时,我明白了。
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
我做错了什么?
我在OS X中使用终端。
这是我教授的代码
#include <iostream>
using namespace std;
/*Start of Program*/
/*Control*/
int main ()
{
/*Variable Definition*/
int iNum1; //names the storage area for user entered value
int iNum2; //names the storage area for the second user entered value
int iResult; //names the storage area for a computed value
/*Screen Output is done with a cout*/
cout << "Hello World!" << endl;
cout << "My name is Burak Ersoy, I am learning to program in C++" << endl;
/*Read*/
cout << "Enter your 1st number: "; //prompts user to enter a number
cin >> iNum1; //stores the user entered value in iNum1
cout << "The number you entered is " << iNum1 << endl; //shows user what they entered
cout << "Enter your 2nd number: "; //asks user for another value
cin >> iNum2; //stores the second user entered value in iNum2
cout << "The second number you entered is " << iNum2 << endl;
/*Calc*/
iResult = iNum1 / iNum2; //computes division using iNum1 and iNum2
/*Print*/
cout << "When I divide " << iNum1 << " by " << iNum2 <<
" the result is " << iResult << " with a remainder of "
<< iNum1 % iNum2 << endl;
/*Outputs the entire process and result of the computation to screen*/
return 0;
}
答案 0 :(得分:1)
听起来你正在编译没有定义main的代码。您需要调整代码(可能是拼写错误,或者忘了定义代码),或者将代码编译成一个不是可执行文件的对象文件(在您的级别,即&#39;不太可能是你想要的。)
这是一个最小的C / C ++程序(我没有声称它符合每个规范)以及编译和运行它时会发生什么:
$ echo 'int main(){}' > foo.cpp
$ g++ foo.cpp -ofoo
$ ./foo
$ echo $?
0