simple steps to run a simple c++ program
I installed ubuntu 7.10 and now I want simple steps to compile and run a simple program like this:
#include <iostream.h>
int main()
{
int a;
cin >> a ;
}
it seems there is no any c++ editor in ubuntu by default
Question information
- Language:
- English Edit question
- Status:
- Solved
- For:
- Ubuntu Edit question
- Assignee:
- No assignee Edit question
- Solved by:
- hadi
- Solved:
- Last query:
- Last reply:
Revision history for this message
|
#1 |
Hi, first of all install g++, type in a terminal,
sudo apt-get install g++
then write your code in your favorite editor, in this case you can use gedit, in a terminal type,
gedit example.cpp
copy the lines you post above and save it.
Finally, compile it
g++ -o example example.cpp
and run it,
./example
Revision history for this message
|
#2 |
You can you use some IDE (for example Anjuta, not installed by default but it is in repository) or GNU C++ Compiler (g++).
Revision history for this message
|
#3 |
ok thanks for your aid
at this time i have g++ and anjuta installed on my system.
Now i have another question
when i write my program in a editor like BorlandC in windows seems to be different from the codes in an standard compiler like g++
because when i going to compile a code written and worked in BorlandC the program encounters dozens of errors
now what can i do ?
Revision history for this message
|
#4 |
Maybe you are using some library which is not supported/presented in Ubuntu...
Revision history for this message
|
#5 |
how can i add other libraries to the ubuntu?
Revision history for this message
|
#6 |
Hi,
could you post the error messages from the compiler to see what's going wrong?
Revision history for this message
|
#7 |
this is my simple snake program :
=======
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
unsigned short int far *screen=(unsigned short int far *)0xb8000000;
const int height=80, width=80, maxLength=50; // these would be constant during the execution
int xHead=40, yHead=25, snake[maxLength];
void initialize( int direction, int food);
void setFood(int food);
void setGround(void);
void setSnake(int direction);
unsigned int getPoint(int xScreen, int yScreen);
int isFunctionKey(char &order);
void move(int length, int direction);
void drawHead(void);
void changeHead(int order);
void findTail(int xHead, int yHead, int length, int &xTail, int &yTail);
void run(int &order, int &endOfGame);
int o ;
int a=1;
//===MAIN=
void main(void)
{
int endOfGame=0, direction=1, length=3;
char food='1', order='M';
randomize();
initialize(
while(!endOfGame)
{ /*/ my delay function
int q,g=1,e,w;
for (q=0;q<27;q++)
for (w=0;w<30000;w++)
for (e=1;e<20;e++)
g=g*e;
//===
*/
delay(200);
switch (direction)
{ case 1 :xHead++; break;
case 2 :yHead++; break;
case 3 :yHead--; break;
case 4 :xHead--; break;
}
switch (screen[
{ case 20*256+49:
length = length+1;
a=a+1;
setFood(a);
break;
case 20*256+50:
length = length+2;
a=a+1;
setFood(a);
break;
case 20*256+51 :
length = length+3;
a=a+1;
setFood(a);
break;
case 20*256+52 :
length = length+4;
a=a+1;
setFood(a);
break;
case 20*256+53 :
length=
a=a+1;
setFood(a);
break;
case 20*256+54 :
length = length+6;
a=a+1;
setFood(a);
break;
case 20*256+55:
length = length+7;
a=a+1;
setFood(a);
break;
case 2*256:
endOfGame=1;
break;
}
move(
if(kbhit())
{ int order=getch();
switch (order )
{
case 77 : direction=1 ; break;
case 75 : direction=4 ; break;
case 80 : direction=2 ; break;
case 72 : direction=3 ; break;
}
run(order, endOfGame);
}
}
getchar();
}
//===INITIALIZE
void initialize( int direction, int food)
{ clrscr();
setGround();
setSnake(
screen[
screen[
screen[
snake[1]=25*80+40;
snake[2]=25*80+41;
snake[3]=25*80+42;
setFood(a);
// drawCommands();
}
//===SETGROUND=
void setGround(void)
{int H;
for (H=1;H<4000 ;H++)
screen[
}
//===SETSNAKE=
void setSnake(int direction)
{ xHead=42;
yHead=25;
screen[
// for(int i=1; i<3; i++)
// snake[i]=1; //right
return;
}
//===SETFOOD=
void setFood(int food)
{ int i=rand()%78;
int j=rand()%49;
switch (food)
{
case 1 : screen[
break ;
case 2 : screen[
break;
case 3 : screen[
break;
case 4 : screen[
break ;
case 5 : screen[
break ;
case 6 : screen[
break;
case 7 : screen[
break ;
case 8 : screen[
break ;
}
return;
}
//===DRAWCOMMAN
//===MOVE=
void move(int length, int direction)
{ int s ;
{ for (int i=1;i<length+1;i++)
{
snake[
}
s=0;
if (xHead<0 && direction==4)
{
xHead=79;
}
if (xHead>79 && direction==1)
{ xHead=0;
}
if (yHead>49 && direction==2)
{
yHead=0;
}
if (yHead<0 && direction==3)
{
yHead=49;
}
snake[
screen[
if (s!=1)
{
screen[
}
}
}
//===GETPOINT=
unsigned int getPoint(int xScreen, int yScreen)
{ return *(screen+
}
//===ISFUNCTION
int isFunctionKey(char &order)
{ if(order=='\x0') //k is a function key
{ order=getch();
return 1;
}
else
return 0;
}
//===CHANGEHEAD
void changeHead(int order)
{ switch(order)
{ case 'H': yHead++;
case 'M': xHead++;
case 'P': yHead--;
case 'K': xHead--;
}
return;
}
//===DRAWHEAD=
void drawHead(void)
{ screen[
}
//===FINDTAIL=
void findTail(int xHead, int yHead, int length, int &xTail, int &yTail)
{
xTail=xHead;
yTail=yHead;
for(int i=0; i<length; i++)
{ switch(snake[i])
{ case 0 : yTail++; break;
case 1 : xTail++; break;
case 2 : yTail--; break;
case 3 : xTail--; break;
}
}
}
//===DOCLEARHEA
//===CLEARTAIL=
//===RUN=
void run(int &order, int &endOfGame)
{ if(order == 27)
endOfGame=1;
}
=======
and these are the errors occurd in SciTE text editor when i try to compile it :
>g++ -pedantic -Os -c tet3.cpp -o tet3.o
tet3.cpp:6:19: error: conio.h: No such file or directory
tet3.cpp:10:17: error: dos.h: No such file or directory
tet3.cpp:12: error: expected initializer before â*â token
tet3.cpp:30: error: â::mainâ must return âintâ
tet3.cpp: In function âint main()â:
tet3.cpp:34: error: ârandomizeâ was not declared in this scope
tet3.cpp:46: error: âdelayâ was not declared in this scope
tet3.cpp:53: error: âscreenâ was not declared in this scope
tet3.cpp:95: error: âkbhitâ was not declared in this scope
tet3.cpp:96: error: âgetchâ was not declared in this scope
tet3.cpp: In function âvoid initialize(int, int)â:
tet3.cpp:114: error: âclrscrâ was not declared in this scope
tet3.cpp:117: error: âscreenâ was not declared in this scope
tet3.cpp: In function âvoid setGround()â:
tet3.cpp:131: error: âscreenâ was not declared in this scope
tet3.cpp: In function âvoid setSnake(int)â:
tet3.cpp:138: error: âscreenâ was not declared in this scope
tet3.cpp: In function âvoid setFood(int)â:
tet3.cpp:149: error: âscreenâ was not declared in this scope
tet3.cpp: In function âvoid move(int, int)â:
tet3.cpp:197: error: âscreenâ was not declared in this scope
tet3.cpp: In function âunsigned int getPoint(int, int)â:
tet3.cpp:211: error: âscreenâ was not declared in this scope
tet3.cpp: In function âint isFunctionKey(
tet3.cpp:217: error: âgetchâ was not declared in this scope
tet3.cpp: In function âvoid drawHead()â:
tet3.cpp:235: error: âscreenâ was not declared in this scope
>Exit code: 1
Revision history for this message
|
#8 |
I had a quick look at your example. This seems to be a DOS program, correct? I don't think dos.h and conio.h exist on linux systems. I found something on google about curses.h and ncurses.h as replacement for conio.h. Don't know if there is a linux equivalent for dos.h.
Next problem may be the way the program writes to the screen ( using the far pointer that seems not to be supported on Linux ). I don't know if this can be done on linux the same way you did it on DOS, think I can't help you there.
Revision history for this message
|
#9 |
there are some programs called dos emulator in the ubuntu repository
can they be useful?
Revision history for this message
|
#10 |
I installed one of those dos emulators and installed the borlandc from there
thanks problem solved.