0
votes

I have the following code:

#include <sstream>
using namespace std;

int
main ()
{
        ostringstream oss;
        unsigned long k = 5;
        oss << k;
}

Compiled with the following parameters:

/usr/local/gcc-10.2.0/bin/g++ -I/usr/local/gcc-10.2.0/include -L/usr/local/gcc-10.2.0/lib64 -Wl,-rpath,/usr/local/gcc-10.2.0/lib64 -lstdc++ b.cpp

Got the following output:

/tmp/cclRSXGV.o: In function main': b.cpp:(.text+0x35): undefined reference to std::ostream::operator<<(unsigned long)'

collect2: error: ld returned 1 exit status

What is needed to get it to compile and link correctly?

Using GNU gcc 10.2.0.

2
g++ -Wall -std=c++11 works for me -- try specifying -std=c++11wcochran
works for me - there any other error / warning messages ?lostbard
@PriyadarshSS, please note that I'm using g++ and not gcc, and I've also specified -lstdc++ so I've basically tried both options available.Fun Mun Pieng
sounds like perhaps a broken gcc tool chain - how about compiling a simple program: #include <iostreams> int main() { std::cout << "test\n"; return 0; }lostbard

2 Answers

1
votes

When you specify what libraries to link the order matters. In this order

-lstdc++ b.cpp

libstdc++ will not resolve any symbols in b.cpp. Specify the library afterwords:

b.cpp -lstdc++ 
0
votes

Turns out, it will compile with:

/usr/local/gcc-10.2.0/bin/g++ -I/usr/local/gcc-10.2.0/include -L/usr/local/gcc-10.2.0/lib64 -Wl,-rpath,/usr/local/gcc-10.2.0/lib64 b.cpp -lstdc++ /usr/local/gcc-10.2.0/lib64/libstdc++.a

I just need to specific the exact libstdc++.a after the cpp file. While I worried that this would cause libstdc++.a to be statically linked, I don't think it will. Static linking requires -static-libstdc++.

Still can't figure out why I need to specify libstdc++.a when my earlier gcc (4.4.7) didn't need it.