C++ program not building and running in VS code

When trying to build a C++ program in VS Code, it does not build the program and ends with exit code -1

(The program does not have errors and is running in other IDEs.)

The compiler used is MinGW

the full terminal output is as follows –

Executing task: C/C++: gcc.exe build active file

Starting build...

C:\\mingw64\\bin\\gcc.exe -fdiagnostics-color=always -g D:\\main.cpp -o D:\\\\main.exe
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\\Users\\parth\\AppData\\Local\\Temp\\ccHLaBmr.o: in function \`main':
D:/main.cpp:6:(.text+0x22): 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:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/main.cpp:7:(.text+0x38): undefined reference to \`std::istream::operator\>\>(int&)'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/main.cpp:8:(.text+0x51): 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:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\\Users\\parth\\AppData\\Local\\Temp\\ccHLaBmr.o:main.cpp:(.rdata$.refptr.\_ZSt3cin\[.refptr.\_ZSt3cin\]+0x0): undefined reference to \`std::cin'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\\Users\\parth\\AppData\\Local\\Temp\\ccHLaBmr.o:main.cpp:(.rdata$.refptr.\_ZSt4cout\[.refptr.\_ZSt4cout\]+0x0): undefined reference to \`std::cout'

collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

\*  The terminal process failed to launch (exit code: -1).

\*  Terminal will be reused by tasks, press any key to close it.

I was expecting the program to build successfully as the program does not have any syntax errors.

  • 3

    You need to use the g++ command to build C++ code, not gcc. Update your tasks.json file.

    – 




  • 2

    You are building and linking C++ code using a C compiler. You will need to figure out what’s broken in your configuration/build system, and change it to use g++ instead of gcc.

    – 

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.

    – 
    Bot

Leave a Comment