Bella James
Bella James

Reputation: 47

MinGw compiler errror - collect2.exe: error: ld returned 1 exit status

I'm trying to compile c++ in Sublime text. I've managed to install the the relevant packages for the MinGw on my computer. As well as creating a path file in the System environment variables under the directory C:\MinGW\bin, its exact location. And subsequently generated a new build system in the editor, containg the fallowing:

{
"cmd" : "gcc $file_name -o ${file_base_name} && ${file_base_name}",
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}

I went further to check in cmd if my installation of the compiler was done properly, and it was.

>gcc --version
gcc (MinGW.org GCC Build-2) 9.2.0
Copyright (C) 2019 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.

I typed in sublime this code:

#include<iostream>
using namespace std;
int main(){
    cout<<"hello world";
}

The compiled result was this :

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\44750\AppData\Local\Temp\ccVYP6T2.o:item.cpp:(.text+0x19): undefined reference to `std::cout'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\44750\AppData\Local\Temp\ccVYP6T2.o:item.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\44750\AppData\Local\Temp\ccVYP6T2.o:item.cpp:(.text+0x35): undefined reference to `std::ios_base::Init::~Init()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\44750\AppData\Local\Temp\ccVYP6T2.o:item.cpp:(.text+0x56): undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s]

Upvotes: 1

Views: 12769

Answers (1)

Bitwize
Bitwize

Reputation: 11210

You set gcc as the compiler, but you are building .

Building with gcc may allow it to compile the C++, but it will not automatically link in the required standard library -- which results in the failed link errors that you are experiencing.

Either change the cmd to use g++, or add the flag "-x c++" so that it knows that this is C++ code, which should allow it to pull in the correct libraries.

Upvotes: 1

Related Questions