Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.
    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>
    */
    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.

    Regards,
    OldAl.

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    658
    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$
    Add 'using namespace...' or use 'std::cout' and use double quotes around the string.

    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.

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Location
    Canberra, Australia
    Posts
    9
    Quote Originally Posted by kakariko81280 View Post
    [code]chris@angua:~/dev/hello$ cat hello.cpp
    [...]
    Add 'using namespace...' or use 'std::cout' and use double quotes around the string.

    All hints to my also rusty c++ were taken from this c++ tutorial site

    Let us know how you get on,

    Chris...
    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.

  4. #4
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by OldAl View Post
    ... 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...
    If your problem was with linking, then perhaps this will explain it:
    Compiling 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 gcc
    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, drl
    Welcome - 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 )

  5. #5
    Just Joined!
    Join Date
    Dec 2007
    Location
    Canberra, Australia
    Posts
    9
    Hi "drl",
    Quote Originally Posted by drl View Post
    Hi.
    [...]
    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, 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

  6. #6
    drl
    drl is online now
    Linux Engineer drl's Avatar
    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, drl
    Welcome - 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 )

  7. #7
    Just Joined!
    Join Date
    Dec 2007
    Location
    Canberra, Australia
    Posts
    9
    Hi drl,
    Quote Originally Posted by drl View Post
    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, 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...