LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2007, 03:35 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
A small problem


Now I am running FC5.
I tried to rerun one of my old programs on the shell.
The name of the program is 'Scripting6.cpp'.
-----------------------------------------------------------------------------------
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
cout << "prints N times the message you want" << endl;
int count=0;
cout << "How much times do you want to write it?" << endl;
cin >> count;
cout << "What do you want to write?" << endl;
char what[10];
cin >> what;
for (int i=0; i<=count; ++i)
{
cout << what << endl;
}
return 0;
}
--------------------------------------------------------------------------------------

[root@c83-250-99-43 Shell Scripting]# chmod 755 Scripting6.cpp
[root@c83-250-99-43 Shell Scripting]# ./Scripting6.cpp
./Scripting6.cpp: line 7: using: command not found
./Scripting6.cpp: line 9: syntax error near unexpected token `('
./Scripting6.cpp: line 9: ` int main()'
[root@c83-250-99-43 Shell Scripting]#

What is the problem? I can't fathom out the meaning of 'command not found', 'syntax error' and 'int main'.
 
Old 01-09-2007, 03:48 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Are you for real? Scripting6.cpp? That is C++ code not some scripting language. C++ programs are compiled to binaries to be run. Use g++ for that on linux.
 
Old 01-09-2007, 03:53 AM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks Hivemind

Use g++ for that on linux.
How do I do this?
 
Old 01-09-2007, 03:58 AM   #4
cyber-worx
Member
 
Registered: May 2004
Location: England / Switzerland
Distribution: Gentoo, Ubuntu, Debian, Slackware
Posts: 138

Rep: Reputation: 15
Code:
g++ Scripting6.cpp -o my_program
I would like to point out that program is both badly written and unsafe to run.

-ciao
 
Old 01-09-2007, 04:09 AM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
I renamed the program to 'g++ Scripting.cpp'.

[root@c83-250-99-43 Shell Scripting]# chmod 755 'g++ Scripting6.cpp'
[root@c83-250-99-43 Shell Scripting]#


[root@c83-250-99-43 Shell Scripting]# ./'g++ Scripting6.cpp'
./g++ Scripting6.cpp: line 7: using: command not found
./g++ Scripting6.cpp: line 9: syntax error near unexpected token `('
./g++ Scripting6.cpp: line 9: ` int main()'
[root@c83-250-99-43 Shell Scripting]#

[ Still it refuses to run? What is the problem?]

 
Old 01-09-2007, 04:22 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You need to compile the program using g++ first. Then run the compiled program, if it compiles.

If you want to write a script instead, then learn to write bash programs instead.
http://www.tldp.org/LDP/abs/abs-guide.pdf

If you want to learn C or C++, pick up one of many books on learning C or C++. Be sure not to skip the first couple chapters that explain the process.
 
Old 01-09-2007, 04:53 AM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks for the comments
How do I compile this program on Linux?

In Windows, you install a C++ program like Borland or Bloodshed Dev C++.
First I write the program, then I compile it. I don't run Windows nowadays.

How do I do it here?
 
Old 01-09-2007, 05:21 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The command to compile is the one given by cyber-worx above. You must have the package gcc-c++ installed. BTW I agree with cyber-worx that the code is badly written and can produce unpredictable results. Good luck!
 
Old 01-09-2007, 05:53 AM   #9
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks colucix for the comments.

I think I have gcc installed.
---------------------------------------------------------------------------------------------
[Nissanka@c83-250-99-43 ~]$ gcc --version
gcc (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[Nissanka@c83-250-99-43 ~]$
------------------------------------------------------------------------------------------------------

Why didn't it compile? I am not good at debugging.
 
Old 01-09-2007, 10:19 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
./'g++ Scripting6.cpp'
The single quotes around the line make it a single command to the shell.
Don't precede it with "./" because g++ isn't in your current directory.
You should indicate your output file. Otherwise it will be called a.out.
 
Old 01-09-2007, 03:34 PM   #11
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
[root@c83-250-99-43 Shell Scripting]# ./'g++ Scripting6.cpp'
./g++ Scripting6.cpp: line 7: using: command not found
./g++ Scripting6.cpp: line 9: syntax error near unexpected token `('
./g++ Scripting6.cpp: line 9: ` int main()'


[root@c83-250-99-43 Shell Scripting]# /'g++ Scripting6.cpp'
bash: /g++ Scripting6.cpp: No such file or directory
[root@c83-250-99-43 Shell Scripting]#


[root@c83-250-99-43 Shell Scripting]# 'g++ Scripting6.cpp'
bash: g++ Scripting6.cpp: command not found
[root@c83-250-99-43 Shell Scripting]#

[Why didn't this work?]
 
Old 01-09-2007, 04:11 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Please, don't go further and type simply this...

Code:
g++  Scripting6.cpp
if you don't get any error you should have a file called a.out which is the executable. Execute it by typing

Code:
./a.out
 
Old 01-09-2007, 04:20 PM   #13
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
[root@c83-250-99-43 Shell Scripting]# g++ Scripting6.cpp
bash: g++: command not found
[root@c83-250-99-43 Shell Scripting]#


[root@c83-250-99-43 Shell Scripting]# g++ Scripting6.cpp
bash: g++: command not found
[root@c83-250-99-43 Shell Scripting]#


[root@c83-250-99-43 Shell Scripting]# g++Scripting6.cpp
bash: g++Scripting6.cpp: command not found
[root@c83-250-99-43 Shell Scripting]#

[What is the problem?]
 
Old 01-09-2007, 04:33 PM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have to install the gcc-c++ package. It provides g++.
 
Old 01-09-2007, 04:52 PM   #15
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
BASH is probably giving you the answer itself: g++ is not installed. You could try which g++ (which will return the path to the program if it's found), or searching for a program file with this name, but it is quite likely that you will need to install it if you wish to be able to compile c++ programs.

For further info, look at article on compilers at Wikipedia.

Rob
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A small problem Gins Programming 14 07-18-2006 07:57 AM
small c/c++ problem Garda Programming 16 05-24-2006 11:31 AM
A small problem in C indian Programming 2 09-07-2004 08:56 AM
small problem s9722 Linux - Newbie 4 06-05-2003 07:13 PM
this problem is so small destkid Linux - Newbie 3 10-19-2001 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration