Find the answer to your Linux question:
Results 1 to 7 of 7
Whenever I try to compile a c++ program using gcc using command gcc program.cpp , it give me errors even if it is with return statement only and no other ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Location
    Delhi, India
    Posts
    72

    Question Tell me how to use GCC

    Whenever I try to compile a c++ program using gcc using command
    gcc program.cpp
    , it give me errors even if it is with return statement only and no other statement is there. Though if I ask it to create the object file only, it does so without any problem.Please tell what is the correct syntax. I am able to compile c programs.



    A new member of the forum so I expect that you all will understand that it will take me some time to learn the rules of the forum.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Hello and welcome,

    it would have been nice if you had given us the exact errors so we don't have to guess what the problem could be.
    GCC is only a front end which dispatches the source files to the correct compiler. Make sure the c++ compiler is installed (type g++ --version).

    When it is, the syntax should work:
    Code:
    >$ cat program.cpp
    int main()
    {
      return 33;
    }
    >$ ./a.out
    >$ echo $?
    33

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Location
    Delhi, India
    Posts
    72
    Thanks for the reply and it worked with g++ command.

    #include<iostream>
    int main()
    {
    std::cout<<"HI";
    return 0;
    }


    When I used gcc I got the output as shown in the file. TextFile
    The forum doesn't allow to post the code in the post itself.

  4. #4
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Hello,

    are there no copy&paste facilities on you desktop?

    To whom else it may be of interest, here is the content of the file thinkfree has uploaded:
    Code:
    gcc new.cpp
    /tmp/ccqJw58y.o: In function `__static_initialization_and_destruction_0(int, int)':
    new.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
    /tmp/ccqJw58y.o: In function `__tcf_0':
    new.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
    /tmp/ccqJw58y.o: In function `main':
    new.cpp:(.text+0x8e): undefined reference to `std::cout'
    new.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*)'
    /tmp/ccqJw58y.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    Now to the actual problem. Contrary to what you have stated above, the program indeed does more than just returning a number.
    It uses the std:cout class from the c++ runtime. The definitions are known by including the headers, which you did.

    But the actual code has to be linked to your executable. This is done by adding the -lstdc++ argument to the command line.

    Whenever you get a "undefined reference to" this is an indicator you have forgotten a required library. (The standard C++ library in this case.)

  5. #5
    Just Joined!
    Join Date
    Jul 2008
    Location
    Delhi, India
    Posts
    72
    It would have been better had you been a little polite. I tried to paste the code but the forum told that I can't put more than 6 images in my post. Still thanks for providing help.

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I'm not sure how he was rude. And for future reference, just copy the text of the code and paste it here. Not sure what you were doing to get an image error.

    In other news, GNU-Fan proposes one solution (-lstdc++). Another option is to, instead of using the "gcc" command, use the "g++" command. Both "gcc" and "g++" invoke the same program, but with different settings, such as libraries to load by default, and default language.
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Just Joined!
    Join Date
    Jul 2008
    Location
    Delhi, India
    Posts
    72
    I had copied and tried to paste the code but it didn't work.

Posting Permissions

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