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).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
FumbleFingers
  • 226
  • 5
  • 15
  • The documentation tells you exactly what to do. The change needed is right here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jan 01 '21 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. – FumbleFingers Jan 01 '21 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. – drescherjm Jan 01 '21 at 14:36
  • 1
    The linux documentation mentions the change here: [https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson) – drescherjm Jan 01 '21 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. – FumbleFingers Jan 01 '21 at 14:37
  • I don't think I'm that dumb, but I kinda thought VSCode might be smart enough by now to present me with some kind of controlled GUI "project properties / definition" input screen where I'd be asked to identify additional source files, libraries, etc. I didn't want to have to learn g++ command line syntax, which is what editing that .json file seems to amount to. Whatever - thanks very much for your help – FumbleFingers Jan 01 '21 at 14:42
  • I think with VSCode if you want to do anything complex you will have to learn your compiler's syntax. It works great for single file programs that don't link to external libraries but once you surpass that you will have to edit some json files and understand the command line for your compiler. – drescherjm Jan 01 '21 at 14:44
  • Unless someone is going to post a really basic "step-by-step" guide to creating and linking to a library (as opposed to posting links to documentation that's bound to be much harder for me to take in), I guess I'm done here for now. But is it reasonable for me to just leave this question open anyway? – FumbleFingers Jan 01 '21 at 14:47
  • To be honest I would like to see a good answer (one that more than just links to the documentation) for this question since it comes up often for all operating systems. There are a few other older duplicates for this. – drescherjm Jan 01 '21 at 14:51
  • Good. On that basis, I will leave it open. I know I'm fully intending to be somewhat lazy about getting to grips with coding in C++ under Linux Mint. I also know that many if not most respondents in the Linux world (and specifically SO) favour linking to (possibly more extensive and thus complex) documentation over "spoonfeeding" querents. I haven't really done any coding for a decade, and it was all in a Windows environment anyway, so just about ***anything*** that I want to do involves a lot of new concepts I'm unfamiliar with (such as "What the hell is a json file?", which I need to know). – FumbleFingers Jan 01 '21 at 15:13
  • (My first attempt to implement the required change here failed miserably. I thought I was being "cautious" by sticking # hash symbol on the front of the existing line, mistakenly supposing this would simply comment it out! :) – FumbleFingers Jan 01 '21 at 15:17

2 Answers2

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.

Suvajit Patra
  • 190
  • 1
  • 4
  • 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? – FumbleFingers Jan 01 '21 at 16:21
  • Don't know , sorry. – Suvajit Patra Jan 01 '21 at 16:26
  • 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 – FumbleFingers Jan 01 '21 at 16:33
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!

FumbleFingers
  • 226
  • 5
  • 15