1

I know this has been asked many many times, but I am a complete beginner at Linux and I have never used it before this. I am trying to set up VSCode for the past hour or so, and now that I finally got the extensions working (did i?) I tried testing it out and it doesn't work. When I type couple basic lines of code:

#include <iostream>
using namespace std;

int main()
{
cout << "It's not working" << endl;

return 0;
}

And when I try to run the code I get this:

[Running] cd "/home/user/School/CPP Codes/" && g++ test.cpp -o test && "/home/user/School/CPP Codes/"test
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function _start': (.text+0x20): undefined reference tomain'
collect2: error: ld returned 1 exit status
[Done] exited with code=1 in 0.096 seconds

I read on some of the posts that I should add -o ?! But again I have 0 clue what that means or what I should do exactly. So any help would be insanely appreciated!

6
  • 3
    The build command looks right. Are you sure test.cpp is the right file? Commented Mar 18, 2019 at 13:47
  • Dear god now it works. When you said that i just closed the file and opened it again and it worked. Didn't change a letter. I was pulling my hair!!! What was that? Commented Mar 18, 2019 at 13:54
  • 1
    just a wild guess, maybe you did not save the file or the file was buffered somewhere... ¯\_(ツ)_/¯ apart from that, your code and command is right as @LightnessRacesinOrbit mentioned Commented Mar 18, 2019 at 14:02
  • @skratchi.at Yes it indeed turned out I have to save the file after every iteration, which is slightly annoying. Commented Mar 18, 2019 at 14:04
  • If you don't save the file to disk, then when the compiler loads the file from disk it will be out of date. Although I'm a little surprised the IDE doesn't save it for you when you hit 'build'; I guess that's because of how the IDE and compiler are isolated in the case of VS Code. Disclaimer: I haven't used it. Commented Mar 18, 2019 at 14:53

1 Answer 1

1

Linker errors are indeed hard to debug. After some years you will get them a bit better. Basically the linker tells you, that he finds the file, but does not find your main. As i mentioned in my comment this could be

  1. you did not safe the file
  2. your file got buffer in any way

In my experience, vs code shows the status of saving a bit hidden. where the cross for closing the file is, there is a round dot.

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

11 Comments

Indeed it turns out I didn't save the file. One quick question: How do i run the code in the terminal? When i run it it shows the code in the "output" window, but since i have to input some information it doesnt work
Command? I don't follow. I use the VS IDE at school which doesn't need any additional steps to set up, but since i have linux i use the vscode at home so I have no clue.
i run vs code under windwos, but should not make any difference. open a terminal in vs code Terminal -> New Terminal. then type .\test
Make shure you are in the same direktory as your binary is compiled to /home/user/School/CPP Codes/. with the command pwd. then check if your binary is in your dir with ls, which should be named test if i read your answer correctly. if all of that applies, you should be able to execute your program with ./test.
make sure you are using the forward slash / NOT the backward slash
|

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.