0

gcc 5.4.0 cygwin 2.8.0 Win10

I've been been knocking my head around this problem.When I compile a simple program, see below, I get an error in one of the gcc include files. I checked the cygwin mailing list and no one has reported an error in the gcc download so I think it's a misunderstanding on my part but I can't figure what I did wrong. Prior to this point all the gcc include fileswere included automatically. Oh, and the compile is correct for other libraries.

The code is:

gcc -std=c++11 test.cpp or gcc test.cpp

include iostream

using namespace std;

int main(int argc, char** argv) { }

and the error message is:

/tmp/ccfBvaqg.o:test.cpp:(.text+0x44): undefined reference to std::ios_base::Init::Init()' /tmp/ccfBvaqg.o:test.cpp:(.text+0x44): relocation truncated to fit: R_X86_64_PC32 against undefined symbolstd::ios_base::Init::Init()'

/tmp/ccfBvaqg.o:test.cpp:(.rdata$.refptr._ZNSt8ios_base4InitD1Ev[.refptr._ZNSt8ios_base4InitD1Ev]+0x0): undefined reference to `std::ios_base::Init::~Init()'

3
  • It has nothing to do with include files, it's a linking error. And fact is you didn't link against standard c++ library (-lstdc++). Commented Jun 22, 2017 at 22:40
  • Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it? Commented Jun 22, 2017 at 22:46
  • That's not valid code. C++ code does not start with gcc -std, include has a hash (pound sign) in front of it and the filename is wrapped in <>. In addition, this is a duplicate, which you would have learned if you'd searched here for undefined reference before posting. (And BTW, code isn't formatted with blockquotes; it's formatted as code. Select it and hit Ctrl+K on your keyboard or click the {} button on the toolbar.) Commented Jun 22, 2017 at 22:48

1 Answer 1

1

gcc is the C compiler driver. The compiler automatically detects the language based on the file name; that is why the compilation succeeded. However, the linker is not affected by the names of the source files. By default, the C compiler driver does not link with the C++ standard library.

Since you used the standard library (<iostream> is a bit atypical header file in such a way that merely including it causes a standard library function to be called at the start of the program), but did not link it, the linker fails. The solution is to link with the C++ standard library. The simplest way to do that is to use the C++ compiler driver (g++) which links the standard library by default.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.