Results 1 to 7 of 7
Hi,
I have just returned to coding C++ after a long break away from any programming. As is customary, I am trying to start with the "hello world" "program". I ...
- 12-02-2007 #1Just Joined!
- Join Date
- Dec 2007
- Location
- Canberra, Australia
- Posts
- 9
gcc and "Hello World" (in C++)
Hi,
I have just returned to coding C++ after a long break away from any programming. As is customary, I am trying to start with the "hello world" "program". I need to use gcc and am trying it. I expect it to recognise the code as C++ and use the appropriate front-end.
I am using openSUSE10.3 on an amd64 dual core computer. The compilation fails -- and that makes me feel really, really miserable. Here is the code. At the end of it, I add the dialog of gcc attempted compilation, as a comment to the code.
I would be most grateful for any suggestions, including a recommendation of a good elementary text on C++ and URLs of any other forums that may be helpful.Code:#include <iostream> //hello.cpp int main() { cout << 'Hello world!\n'; return 0; } /* ak@amd64:~/gcc> gcc hello.cpp -o hello hello.cpp:7:11: warning: character constant too long for its type hello.cpp: In function ‘int main()’: hello.cpp:7: error: ‘cout’ was not declared in this scope ak@amd64:~/gcc> */
Regards,
OldAl.
- 12-02-2007 #2Linux Enthusiast
- Join Date
- Apr 2004
- Location
- UK
- Posts
- 658
Add 'using namespace...' or use 'std::cout' and use double quotes around the string.Code:chris@angua:~/dev/hello$ cat hello.cpp #include <iostream> using namespace std; //hello.cpp int main() { cout << "Hello world!\n"; return 0; } chris@angua:~/dev/hello$ g++ hello.cpp -o hello chris@angua:~/dev/hello$ ./hello Hello world! chris@angua:~/dev/hello$
All hints to my also rusty c++ were taken from this c++ tutorial site
Let us know how you get on,
Chris...To be good, you must first be bad. "Newbie" is a rank, not a slight.
- 12-02-2007 #3Just Joined!
- Join Date
- Dec 2007
- Location
- Canberra, Australia
- Posts
- 9
Chris,
Many thanks for your reply and the information. In particular, the link to the c++ tutorial site will be usefull.
The "hello program" was copied from a book, albeit with "replacement" of double quotes with single quotes, which was all my own doing in desperation...
When I corrected the quotes and added "using namespace", the "hello program" worked ok when compiled with g++, but it still fails if I use gcc instead of g++. Strange...
I will read the tutorial that you have referred me to.
Also, I will mark this forum as a great reference, thanks to you!
OldAl.
- 12-03-2007 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
If your problem was with linking, then perhaps this will explain it:
You can try compiling and omit the linking by using "-c" with gcc, if that seems to compile your code correctly, then the problem was with the library selection ... cheers, drlCompiling C++ Programs
C++ source files conventionally use one of the suffixes .C, .cc, .cpp,
.c++, .cp, or .cxx; preprocessed C++ files use the suffix .ii. GCC
recognizes files with these names and compiles them as C++ programs
even if you call the compiler the same way as for compiling C programs
(usually with the name gcc).
However, C++ programs often require class libraries as well as a com-
piler that understands the C++ language---and under some circumstances,
you might want to compile programs from standard input, or otherwise
without a suffix that flags them as C++ programs. g++ is a program
that calls GCC with the default language set to C++, and automatically
specifies linking against the C++ library.
-- man gccWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 12-03-2007 #5Just Joined!
- Join Date
- Dec 2007
- Location
- Canberra, Australia
- Posts
- 9
Hi "drl",
I followed your advice and compiled with gcc with -c switch. It did compile. As you pointed out, this suggests that a library is missing.
Yet the man gcc says that g++ calls gcc with identification of the program being C++. And g++ **does** compile and link producing code that works. I am bushed.
Thank you for your help - great to get these hints here!
OldAl.Last edited by OldAl; 12-03-2007 at 10:14 AM. Reason: More polite IMHO to address a person
- 12-03-2007 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
The libraries are missing in the sense that they were not selected in the linking phase when one uses gcc. Using g++ will cause those extra, appropriate libraries to be selected and so the link phase will be successful.
With some work you could find those extra libraries and continue to use gcc, but it's less bother simply to use g++ ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 12-03-2007 #7Just Joined!
- Join Date
- Dec 2007
- Location
- Canberra, Australia
- Posts
- 9
Hi drl,
I think you are absolutely right - on both counts. I should drop (temporarily) my obsession with gcc and just use g++ and work through the tutorial, mentioned in the first reply to this thread. My interest in gcc came from a KDE project (kmymoney), whose mailing list I subscribed to some years back.
Parallel to using g++, I think it would be worth while to have a look at the "Make" files of that project and see how the developers invoke gcc.
Thanks for all your help. It is great to have the horse that pulls my cart to be turned in the right direction!
Very gratefully,
OldAl.


Reply With Quote
