Puff
Puff

Reputation: 1

How to succsessfully compille .cpp files using gcc with out adding -lstdc++ flag?

The autograder of my class uses gcc to grade our projects, it only takes file uploads(.cpp .h), students can't pass in any argument flags. My project compiles with g++ with no issues:

g++ main.cpp func1.cpp func2.cpp func1.h -o a.out

but with gcc I have to add -lstdc++ at the end for it to compile:

gcc main.cpp func1.cpp func2.cpp func1.h -o a.out -lstdc++

otherwise the errors:

/usr/bin/ld: /tmp/cck9qDRT.o: in function main': main.cpp:(.text+0x60): undefined reference to std::cout' /usr/bin/ld: main.cpp:(.text+0x65): undefined reference to std::basic_ostream<char, std::char_traits >& std::operator<< std::char_traits<char >(std::basic_ostream<char, std::char_traits >&, char const*)' /usr/bin/ld: main.cpp:(.text+0x83): undefined reference to std::cin' /usr/bin/ld: main.cpp:(.text+0x88): undefined reference to std::istream::operator>>(int&)' /usr/bin/ld: main.cpp:(.text+0x96): undefined reference to std::cin' /usr/bin/ld: main.cpp:(.text+0x9b): undefined reference to std::istream::operator>>(int&)' /usr/bin/ld: main.cpp:(.text+0xb3): undefined reference to std::cout' /usr/bin/ld: main.cpp:(.text+0xb8): undefined reference to std::ostream::operator<<(int)' /usr/bin/ld: main.cpp:(.text+0xc2): undefined reference to std::basic_ostream<char, std::char_traits >& std::endl<char, std::char_traits >(std::basic_ostream<char, std::char_traits >&)' /usr/bin/ld: main.cpp:(.text+0xcd): undefined reference to std::ostream::operator<<(std::ostream& ()(std::ostream&))' /usr/bin/ld: main.cpp:(.text+0xe5): undefined reference to std::cout' /usr/bin/ld: main.cpp:(.text+0xea): undefined reference to std::ostream::operator<<(int)' /usr/bin/ld: main.cpp:(.text+0xf4): undefined reference to std::basic_ostream<char, std::char_traits >& std::endl<char, std::char_traits >(std::basic_ostream<char, std::char_traits >&)' /usr/bin/ld: main.cpp:(.text+0xff): undefined reference to std::ostream::operator<<(std::ostream& ()(std::ostream&))' /usr/bin/ld: /tmp/cck9qDRT.o: in function __static_initialization_and_destruction_0(int, int)': main.cpp:(.text+0x147): undefined reference to std::ios_base::Init::Init()' /usr/bin/ld: main.cpp:(.text+0x15c): undefined reference to std::ios_base::Init::~Init()' /usr/bin/ld: /tmp/ccTpZnaT.o: in function fib1()': func1.cpp:(.text+0x31): undefined reference to std::cout' /usr/bin/ld: func1.cpp:(.text+0x36): undefined reference to std::basic_ostream<char, std::char_traits >& std::operator<< std::char_traits<char >(std::basic_ostream<char, std::char_traits >&, char const*)' /usr/bin/ld: func1.cpp:(.text+0x40): undefined reference to std::basic_ostream<char, std::char_traits >& std::endl<char, std::char_traits >(std::basic_ostream<char, std::char_traits >&)' /usr/bin/ld: func1.cpp:(.text+0x4b): undefined reference to std::ostream::operator<<(std::ostream& ()(std::ostream&))' /usr/bin/ld: /tmp/ccTpZnaT.o: in function __static_initialization_and_destruction_0(int, int)': func1.cpp:(.text+0x85): undefined reference to std::ios_base::Init::Init()' /usr/bin/ld: func1.cpp:(.text+0x9a): undefined reference to std::ios_base::Init::~Init()' /usr/bin/ld: /tmp/cc2MzCjT.o: in function fib2()': func2.cpp:(.text+0x23): undefined reference to std::cout' /usr/bin/ld: func2.cpp:(.text+0x28): undefined reference to std::basic_ostream<char, std::char_traits >& std::operator<< std::char_traits<char >(std::basic_ostream<char, std::char_traits >&, char const)' /usr/bin/ld: /tmp/cc2MzCjT.o: in function __static_initialization_and_destruction_0(int, int)': func2.cpp:(.text+0x62): undefined reference to std::ios_base::Init::Init()' /usr/bin/ld: func2.cpp:(.text+0x77): undefined reference to std::ios_base::Init::~Init()' collect2: error: ld returned 1 exit status

How do I compile using gcc without needing the -lstdc++? Does manually including all the standard libraries used on the project in code fix it?

Upvotes: 0

Views: 453

Answers (1)

James K. Lowden
James K. Lowden

Reputation: 7837

The answer, surprisingly, is it can't be done. According to the gcc manual, the gcc command recognizes .cpp files as C++, but the use of gcc does not add the C++ library.

From your description, this sounds like a technical error in how the autograder was set up, and everyone in your class will encounter the same difficulty.

Does manually including all the standard libraries

That way lies madness. Don't even think of it.

Upvotes: 2

Related Questions