I have this piece of code that prints out a pattern, I ran it on an online IDE and it compiled and gave correct output. But in VS code I get build error when I copy paste it to the file and try to build it. I am working on Manjaro KDE and using VS code 1.52.1. I installed g++ using
sudo pacman -S base-devel
.
The error:
Starting build...
/usr/bin/gcc -g /home/aditya/Desktop/scratches/helloworld.cpp -o /home/aditya/Desktop/scratches/helloworld
/usr/bin/ld: /tmp/ccgMjJGk.o: warning: relocation against `_ZSt4cout' in read-only section `.text'
/usr/bin/ld: /tmp/ccgMjJGk.o: in function `main':
/home/aditya/Desktop/scratches/helloworld.cpp:6: undefined reference to `std::cin'
/usr/bin/ld: /home/aditya/Desktop/scratches/helloworld.cpp:6: undefined reference to `std::istream::operator>>(int&)'
/usr/bin/ld: /home/aditya/Desktop/scratches/helloworld.cpp:9: undefined reference to `std::cout'
/usr/bin/ld: /home/aditya/Desktop/scratches/helloworld.cpp:9: undefined reference to `std::ostream::operator<<(int)'
/usr/bin/ld: /home/aditya/Desktop/scratches/helloworld.cpp:11: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: /home/aditya/Desktop/scratches/helloworld.cpp:11: undefined reference to `std::cout'
/usr/bin/ld: /home/aditya/Desktop/scratches/helloworld.cpp:11: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: /tmp/ccgMjJGk.o: in function `__static_initialization_and_destruction_0(int, int)':
/usr/include/c++/10.2.0/iostream:74: undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: /usr/include/c++/10.2.0/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
Build finished with error(s).
The terminal process terminated with exit code: -1.
Terminal will be reused by tasks, press any key to close it.
The code:
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=i;j<2*i;j++){
cout<<j;
}
cout<<endl;
}
return 0;
}
I followed the instructions on the Visual Studio website and got a hello world program build successfully and because of this I think there is a problem with the code itself. But it runs on
online IDE which is contradictory
The online IDE: codechef.com
question from:
https://stackoverflow.com/questions/66054775/my-code-runs-on-online-ide-but-fails-build-task-in-vscode 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…