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 02-02-2021, 06:59 PM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Rep: Reputation: 51
Angry [C++] What is not installed here? (I cannot include "iostream", gcc complains strangely)


I thought the gcc i have installed had everything to basic (at least) C++ programs. But look at this:

File a.cpp:

Code:
#include <iostream>
#include <cstdlib>

int main()
{
    return 0;
}
Compiled with:

Code:
gcc -ggdb -Wall a.cpp
Returns:

Code:
/tmp/ccHxsgn8.o: In function
 `__static_initialization_and_destruction_0(int, int)':
/usr/include/c++/6/iostream:74: undefined reference to
 `std::ios_base::Init::Init()'
/usr/include/c++/6/iostream:74: undefined reference to
 `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
And the file we get removing the first line, with the iostream library include, outputs no error, compiles perfectly.

One command output:

Code:
$ gcc --version
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 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.
It is not a problem on my machine. I remotely accessed an university computer which is used by Computer Science students, mainly, and there also cannot use "#include <iostream>" in programs. What is wrong here??? iostream is so basic in C++!

Last edited by dedec0; 02-02-2021 at 07:59 PM.
 
Old 02-02-2021, 08:10 PM   #2
vmelkon
Member
 
Registered: Feb 2007
Location: Canada
Distribution: Kubuntu 22.04
Posts: 549

Rep: Reputation: 84
I get

Code:
$ gcc -ggdb -Wall a.cpp
/usr/bin/ld: /tmp/ccvBLGTi.o: in function `__static_initialization_and_destruction_0(int, int)':
/usr/include/c++/10/iostream:74: undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: /usr/include/c++/10/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
however, g++ works
Code:
$ g++ -ggdb -Wall a.cpp
 
1 members found this post helpful.
Old 02-02-2021, 08:35 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,713

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
g++ automatically links in the std c++ libraries but gcc does not.

This is why you see the undefined reference errors to std and the program fails to compile.

Code:
gcc a.cpp -lstdc++

Last edited by michaelk; 02-02-2021 at 08:43 PM.
 
1 members found this post helpful.
Old 02-02-2021, 08:49 PM   #4
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by vmelkon View Post

[...]

however, g++ works
Code:
$ g++ -ggdb -Wall a.cpp
Indeed. I forgot about g++. I get no error (not that error) for my C++ files in all machines. Thank you!
 
Old 02-02-2021, 08:52 PM   #5
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by michaelk View Post
g++ automatically links in the std c++ libraries but gcc does not.

This is why you see the undefined reference errors to std and the program fails to compile.

Code:
gcc a.cpp -lstdc++
Very nice! I did not know this. Thank you too!
 
  


Reply

Tags
debian, gcc, iostream



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
NIC not installed anymore, KDE not installed anymore, LILO duplicate Volume ID error scottad Slackware 2 03-31-2012 12:13 AM
Newbie here... just following suggestions to post here mikebyce LinuxQuestions.org Member Intro 3 04-08-2010 07:42 PM
Hi there im new here, come here to learn more bout Linux n Solaris Defution LinuxQuestions.org Member Intro 5 08-18-2009 01:05 PM

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

All times are GMT -5. The time now is 01:23 PM.

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