LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [C++] What is not installed here? (I cannot include "iostream", gcc complains strangely) (https://www.linuxquestions.org/questions/programming-9/%5Bc-%5D-what-is-not-installed-here-i-cannot-include-iostream-gcc-complains-strangely-4175689768/)

dedec0 02-02-2021 06:59 PM

[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++!

vmelkon 02-02-2021 08:10 PM

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

michaelk 02-02-2021 08:35 PM

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++

dedec0 02-02-2021 08:49 PM

Quote:

Originally Posted by vmelkon (Post 6215375)

[...]

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!

dedec0 02-02-2021 08:52 PM

Quote:

Originally Posted by michaelk (Post 6215377)
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!


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