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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-12-2007, 10:22 PM
|
#1
|
|
Member
Registered: Oct 2006
Posts: 75
Rep:
|
trying to compile a program in C++ gives me a bunch of errors ( Total N00b)
hey, I am brand new to c++ and trying to learn it and have it work on Linux.
I tried to compile a basic program via command line ( copied it from a book ) :
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{ //enter celsius
int celsius;
cout << "enter the Celsius here:";
cin >> celsius;
//Calculate conversion factor
int factor;
factor = 212 - 32;
//use conversion factor
int fahrenheit;
fahrenheit = factor * celsius / 200 + 32;
//output it all
cout << "in °F:";
cout << fahrenheit << endl ;
//wait for termination
system ("PAUSE");
return 0; #include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{ //enter celsius
int celsius;
cout << "enter the Celsius here:";
cin >> celsius;
//Calculate conversion factor
int factor;
factor = 212 - 32;
//use conversion factor
int fahrenheit;
fahrenheit = factor
}
so I try to compile it, but I get:
ME@UBUNTU:~/Desktop$ gcc -o degrees celcius.cpp
celcius.cpp:23: error: stray ‘#’ in program
celcius.cpp: In function ‘int main(int, char**)’:
celcius.cpp:23: error: ‘include’ was not declared in this scope
celcius.cpp:23: error: ‘cstdio’ was not declared in this scope
celcius.cpp:26: error: expected primary-expression before ‘using’
celcius.cpp:26: error: expected `;' before ‘using’
celcius.cpp:29: error: a function-definition is not allowed here before ‘{’ token
celcius.cpp:40: error: expected `}' at end of input
Why is it doing this and what am I doing wrong?
another question: Are there differences between scripting c++ programs on Windows and scripting on Linux? because oftentimes some programs I want to write won't compile on Linux...I get a bunch of errors, but they compile on the windows IDE I use.
Last edited by Ryupower; 06-12-2007 at 10:35 PM.
|
|
|
|
06-12-2007, 10:39 PM
|
#2
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Your program has two main functions. You seemed to have cat'ed two programs into one.
Also, it would be easier for others looking at your code if you enclosed it in code blocks and added line numbers.
E.G. "cat -n celcius.cpp"
Code:
1 #include <cstdio>
2 #include <cstdlib>
3 #include <iostream>
4 using namespace std;
5
6 int main(int nNumberofArgs, char* pszArgs[])
7 { //enter celsius
8 int celsius;
9 cout << "enter the Celsius here:";
10 cin >> celsius;
11
12 //Calculate conversion factor
13 int factor;
14 factor = 212 - 32;
15 //use conversion factor
16 int fahrenheit;
17 fahrenheit = factor * celsius / 200 + 32;
18 //output it all
19 cout << "in °F:";
20 cout << fahrenheit << endl ;
21 //wait for termination
22 system ("PAUSE");
23 return 0;
24 }
Last edited by jschiwal; 06-12-2007 at 10:43 PM.
|
|
|
|
06-13-2007, 01:30 PM
|
#3
|
|
Member
Registered: Oct 2006
Posts: 75
Original Poster
Rep:
|
ah I see, lol.
Very stupid question. XD
Sorry about that.
I fixed that, made sure I only had one program:
Code:
1. #include <cstdio>
2. #include <cstdlib>
3. #include <iostream>
4 using namespace std;
5. int main(int nNumberofArgs, char* pszArgs[])
6. { //enter celsius
7. int celsius;
8. cout << "enter the Celsius here:";
9. cin >> celsius;
10. //Calculate conversion factor
11. int factor;
12. factor = 212 - 32;
13. //use conversion factor
14. int fahrenheit;
15. fahrenheit = factor * celsius / 200 + 32;
16.//output it all
17. cout << "in °F:";
18. cout << fahrenheit << endl ;
19. //wait for termination
20. system ("PAUSE");
21. return 0;
22. }
and I get:
Code:
/Desktop$ gcc -o degrees celcius.cpp
/tmp/cc4NALEN.o: In function `__static_initialization_and_destruction_0(int, int)':
celcius.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc4NALEN.o: In function `__tcf_0':
celcius.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc4NALEN.o: In function `main':
celcius.cpp:(.text+0x8e): undefined reference to `std::cout'
celcius.cpp:(.text+0x93): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
celcius.cpp:(.text+0xa1): undefined reference to `std::cin'
celcius.cpp:(.text+0xa6): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
celcius.cpp:(.text+0xe5): undefined reference to `std::cout'
celcius.cpp:(.text+0xea): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
celcius.cpp:(.text+0xf8): undefined reference to `std::cout'
celcius.cpp:(.text+0xfd): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
celcius.cpp:(.text+0x105): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
celcius.cpp:(.text+0x10d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/cc4NALEN.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
I must be doing something wrong... AGAIN...
And are there differences in c++ between linux and windows?
thanks guys for having patience with me.
Last edited by Ryupower; 06-13-2007 at 01:41 PM.
|
|
|
|
06-13-2007, 03:44 PM
|
#4
|
|
Member
Registered: Apr 2006
Posts: 81
Rep:
|
You need to compile it with g++
|
|
|
|
06-13-2007, 06:09 PM
|
#5
|
|
Member
Registered: Oct 2006
Posts: 75
Original Poster
Rep:
|
like this?
~/Desktop$ g++ celcius.cpp
celcius.cpp:1: error: stray ‘#’ in program
celcius.cpp:2: error: stray ‘#’ in program
celcius.cpp:3: error: stray ‘#’ in program
celcius.cpp:7:1: error: invalid suffix "int" on floating constant
celcius.cpp:1: error: expected unqualified-id before numeric constant
celcius.cpp:5: error: expected unqualified-id before numeric constant
|
|
|
|
06-13-2007, 10:09 PM
|
#6
|
|
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,538
Rep:
|
try:
Code:
g++ celcius.cpp -o celcius
then run './celcius' from the directory you compiled it in.
|
|
|
|
06-15-2007, 12:46 PM
|
#7
|
|
Member
Registered: May 2004
Location: Underground base in the mountains
Distribution: FreeBSD, Fedora, Ubuntu
Posts: 87
Rep:
|
If you are going to compile a C program use gcc. But if you're going compile a C++ program then you can use g++. It will give you errors if you're going to compile a C program using g++ or vice versa. :/
|
|
|
|
06-15-2007, 02:57 PM
|
#8
|
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
Quote:
|
Originally Posted by Ryupower
like this?
~/Desktop$ g++ celcius.cpp
celcius.cpp:1: error: stray ‘#’ in program
celcius.cpp:2: error: stray ‘#’ in program
celcius.cpp:3: error: stray ‘#’ in program
celcius.cpp:7:1: error: invalid suffix "int" on floating constant
celcius.cpp:1: error: expected unqualified-id before numeric constant
celcius.cpp:5: error: expected unqualified-id before numeric constant
|
Ok, I'm guessing you're learning C++ coming from a BASIC background. Is that right? The reason I ask is because the errors you report suggest your source code actually contains the line numbers at the beginning of each line. In other words, you're trying to compile exactly what you posted previously. As I recall, the BASIC I learned used line numbers. In C/C++ don't use them. The code you should be compiling is the following (with spacing in the way I prefer it):
Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
//enter celsius
int celsius;
cout << "enter the Celsius here:";
cin >> celsius;
//Calculate conversion factor
int factor;
factor = 212 - 32;
//use conversion factor
int fahrenheit;
fahrenheit = factor * celsius / 200 + 32;
//output it all
cout << "in °F:";
cout << fahrenheit << endl ;
//wait for termination
system ("PAUSE");
return 0;
}
Compile it with the command nadroj gave you:
Code:
g++ celcius.cpp -o celcius
|
|
|
|
06-16-2007, 06:41 PM
|
#9
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
My suggestion was to add line numbers when posting questions about the code. Then you can refer to lines by their linenumber. An editor like kate and vim can display line numbers without them being part of the file.
|
|
|
|
06-16-2007, 07:36 PM
|
#10
|
|
Member
Registered: Oct 2006
Posts: 75
Original Poster
Rep:
|
Quote:
|
Originally Posted by nadroj
try:
Code:
g++ celcius.cpp -o celcius
then run './celcius' from the directory you compiled it in.
|
:O
It worked! Without error it compiled! The program calculated wrong...but that's an error on my part.
Thanks. ^^
Thanks to the rest too. 
|
|
|
|
06-21-2007, 11:22 AM
|
#11
|
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
I know this thread has been taken care of. I just wanted to respond really quick...
Quote:
|
Originally Posted by jschiwal
My suggestion was to add line numbers when posting questions about the code. Then you can refer to lines by their linenumber. An editor like kate and vim can display line numbers without them being part of the file.
|
I honestly don't remember if I saw that suggestion in my original reading. I got thrown off looking at the error messages being reported. Specifically:
Quote:
|
celcius.cpp:7:1: error: invalid suffix "int" on floating constant
|
That error suggested to me that the line numbers, including the period, were in the file as well. The line numbers, with a trailing period, would be a floating point constant, and would also explain the "stray `#'" errors.
My apologies for not reading things as closely as I should have.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:39 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|