DEV Community

Cover image for Setup Visual Studio Code for Multi-File C++ Projects

Setup Visual Studio Code for Multi-File C++ Projects

Talha Balaj on February 04, 2020

C++ is a statically-typed general-purpose language middle-level programming language and superset of the C programming language used everywhere but...
Collapse
 
jrodriguezv profile image
jrodriguez-v • Edited

I copy-pasted the Makefile content but when running make I was getting the error "mising separator. Stop".
I fixed adding a tab instead of spaces in lines 15, 16, 19 and 22:

run: clean all
[tab]clear
[tab]./$(BIN)/$(EXECUTABLE)

$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
[tab]$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBRARIES)

clean:
[tab]-rm $(BIN)/*

Note: It woud be nice if the main.cpp file could be put in the root of the project folder, outside of src.

Great post by the way. I spent hours trying to compile in VS Code a multi file project before I seeing this.
Thanks!.

Collapse
 
talhabalaj profile image
Talha Balaj

I'm glad you found a work-around, I copy-pasted the makefile from my project, so maybe tabs were removed.

If you want main.cpp in your root directory of your project. try this.

$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp main.cpp
Enter fullscreen mode Exit fullscreen mode

Thank you, I'm glad it helped.

Collapse
 
faizan3165 profile image
Faizan Mehmood • Edited

I did the setup as mentioned but I keep getting the following error

C:\temp\gcc\build-mingw-w64\mingw-w64-crt/../../src/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
make: *** [bin/main] Error 1
Enter fullscreen mode Exit fullscreen mode
Collapse
 
talhabalaj profile image
Talha Balaj

Its' not for windows.

Collapse
 
aholi2007 profile image
Aholi2007

for me it always says:
make: The term "make" was not recognized as the name of a cmdlet, function, script file, or executable program. Check the spelling of the name or whether the path is correct (if included),
and repeat the process.
In line: 1 character: 1

  • make
  • ~~~~
    • CategoryInfo: ObjectNotFound: (make: String) [], CommandNotFoundException
    • FullyQualifiedErrorId: CommandNotFoundException
Collapse
 
talhabalaj profile image
Talha Balaj

You are using windows? or maybe PowerShell Core on Linux?

You need to install make on your System and you won't get error again.

Collapse
 
confuze profile image
Confuze

Thank you so much for this guide!
For anyone struggling with intellisense errors, just add a file called c_cpp_properties.json in .vscode/ with the following contents:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-clang-x64"
        }
    ],
    "version": 4
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
matiaslgonzalez profile image
Matías González

Great guide!
But when setting up the ProblemMatcher, should it be added after the group {}, or as a field of the group

Collapse
 
talhabalaj profile image
Talha Balaj

Thank you!
After the group, like this.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
           "problemMatcher": "$gcc"
        }
    ]
}
Collapse
 
matiaslgonzalez profile image
Matías González

Thanks! Actually VSCodium complains if you do it wrongly, but thanks for taking the time! 😁

Collapse
 
thanoskrk profile image
ThanosKrk

Really helpful, well said! One thing though
I notice there is a missing ',' in the tasks.json file needed after "command": "make"

Collapse
 
talhabalaj profile image
Talha Balaj

Thank you. I'm glad that it helped.
Yeah I did a mistake, I'll fix it now.

Collapse
 
bemayer profile image
Benoît Mayer

Thank you Talha, it helped a lot !

Just one typo: "and voilà !"
("viola" means "raped" in French 😉)

Collapse
 
talhabalaj profile image
Talha Balaj

You welcome. I'm glad it helped.
Sorry for the typo, I had no idea. I'll fix it right away.