0

I've installed Microsoft VSCode under Linux Mint, and opened a folder named test1 containing 3 files:

test.cpp - pre-declares foo() and bar(), then calls both functions in main()
foo.cpp - defines function foo()
bar.cpp - defines function bar()

When I compile test.cpp (using F5 Start debugging in VSCode) it fails with undefined reference to foo() and undefined reference to bar(). When I compile foo.cpp and bar.cpp, they both fail with undefined reference to main.

I found VS Code will not build c++ programs with multiple .ccp source files as asked here previously, from which I discover I can type the following in the VSCode "Terminal" window...

g++ test.cpp foo.cpp bar.cpp -o a.out
./a.out

...and my program compiles and runs as expected (but I can't do any debugging, obviously).


So what I want to know in the first instance is How do I tell VSCode to compile and link in those two additional source files?

Unless it's going to be blindingly difficult, I'd also like some guidance on how to go about moving on to Phase 2 of my task - compiling foo and bar into a "library" file that I can link across to when working on a project in folder test2 (sibling to test1).

If at all possible, I'd like to achieve this entirely within the context of the VSCode environment (maybe I'll think about learning the complexities of g++ and makefiles sometime in the future).

11
  • The documentation tells you exactly what to do. The change needed is right here: https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson Commented Jan 1, 2021 at 14:19
  • Thanks. It took me a while to figure out that the reason it still didn't work when I did exactly what it told me in your link (without giving a particularly enlightening error message) was because I edited tasks.json as instructed, to use an argument like "${workspaceFolder}\*.cpp" instead of ${file}. But the double backslashes part of that must be relevant to a Windows environment. Eventually I thought of replacing it with a single forward slash /, which did the trick. Maybe I'm not making enough effort, but I could have used better help than that. Commented Jan 1, 2021 at 14:34
  • There is other documentation for linux or macOS that show using / instead of \ for the same modifying tasks.json. I guess because you mentioned linux mint I should have linked that instead. Commented Jan 1, 2021 at 14:36
  • 1
    The linux documentation mentions the change here: https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson Commented Jan 1, 2021 at 14:37
  • That being the case, why did you give me a link to documentation aimed at a Windows user? I made it clear in the quesztion I'm on Linux Mint. Anyway, thanks for the updated link. Commented Jan 1, 2021 at 14:37

2 Answers 2

1

First make the a.out file first and change the /.vscode/lauch.json file. In that file change the "program": "whatever" to "program": "${workspaceFolder}/a.out" and if there is a "preLaunchTask": "C/C++: g++ build active file", then cut that line then press F5 and the debugger should work fine.

Check out here for more clearification.

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

3 Comments

That's great, thank you! Pressing F5 does now run my program, and your link looks useful too. But before I start wading through that link, can I just ask if by any chance there's some really easy way to make the Debugger run up to some specific line in my source code (without having to set an actual "breakpoint", which I haven't looked into yet). I recall that in Windows MS Visual C years ago I could "Run to current cursor position" (perhaps invoked by Ctrl+F10, I disremember). Is there any equivalent to that in my current "VSCode using g++" environment?
Don't know , sorry.
okay, well thanks for your help anyway. Maybe someone else will enlighten me here. Or I'll raise another question if I can't find the answer myself after a reasonable amount of research
0

I found that it wasn't particularly helpful for me to override the default output file (from test) to a.out as per the question I linked to in my OP. In the end, I just amended the highlighted element in .vscode/tasks.json from...

"args": [
"-g",
"${file}",
"-o",
...

...to...

"args": [
"-g",
"*.cpp",
"-o",
....

Pressing F5 with VSCode compiles and runs the program, just as I wanted. But I will just mention one other thing I found confusing. The output program file is simply written out as test (no extension) in folder test1 (alongside the 3 source files). But when I typed test + ENTER into the VSCode "Terminal" window, I got no error, but I didn't get the expected output from my program.

It turned out this was because (a) I forgot to type ./test and (b) there's an executable file called test in my /usr/bin folder (that's obviously nothing to do with me; it's dated earlier than when I installed Linux Mint!) I've no idea what that test program is supposed to do, but it certainly doesn't display Hello World the way my test program does!

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.