Vscode G++ It Isn't Finding .Cpp Definition Files

Vscode g++ it isn't finding .cpp definition files

In your tasks.json you are using the default ${file} which means compile only the active file and not all source files in your folder structure.

The VSCode documentation explains how to fix this for the case of all source files in the same folder here: https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson

The fix is to replace ${file} with "${workspaceFolder}/*.cpp"

In your case you have more than 1 folder containing source files. You can apply a similar fix to the second folder by adding: "${workspaceFolder}/classes/*.cpp"

so the whole tasks.json would be:

{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/*.cpp",
"${workspaceFolder}/classes/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-iquote${workspaceFolder}/headers"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

VSCode not recognizing includes from includepath

You did not tell your compiler anything about a file called Zipper.h or where it is loacted, or anything related to it. "g++ test.cpp -o test" just tells the compiler to compile a source file called test.cpp and link it. You have to understand that Visual Studio Code is not an IDE and can't compile by itself. You should have an file called c_cpp_properties.json file located in your .vscode directory. The one that i use for example looks like this and is configured for mingw64.

{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/Source/**"
],
"compilerPath": "C:\\mingw-w64\\mingw64\\bin\\gcc.exe",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}

This tells Visual Studio Code where your source files and libraries are. This is what is used for IntelliSense (Syntax Highlights, Error Squiggles, Code Completion, etc). However this has absolutly nothing to do with building your project. Your compiler doesn't now know about the include path's you set in Visual Studio Code. So to compile your project you have to tell your compiler everything he needs to know. Visual Studio Code simply executes what you specify in the task. It's the same as going to that directory and type in the same thing in your command promt. So i recommend you to read up on how to compile a c++ project with g++, your problem is not related to Visual Studio Code at all. If youre planning on doing something thats a bit bigger than just a single source file i strongly suggest you to learn CMake. Compiling by manually calling gcc get's really complicated once you have more source files and includes / libraries to link. Once you have set up your Cmake you can just specify a task in Visual Studio Code similar to this one to build your project:

{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "cmake --build Build",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

I also recommend you to read this:
https://code.visualstudio.com/docs/cpp/config-mingw

This is a really good explanation of basicly exactly what you are trying to do by Microsoft and helped me understanding this when i started to use Visual Studio Code for my c++ work.

VSCode compile C++ with external classes - undefined reference to `MyClass::MyClass()'

Change main.cpp's includes to:

#include <iostream>
#include <string>
#include "MyClass.cpp"

By including MyClass.cpp in main.cpp and compiling like this:

g++ -o main main.cpp

you end up including MyClass.h by virtue of it being included in MyClass.cpp. By including MyClass.cpp, you can successfully compile.

If you want to keep your code as it currently is, you can instead change your compilation to be:

g++ -o main main.cpp MyClass.cpp

Which will include MyClass.cpp in compilation.

vscode cannot find header when compile while Intellisense can

c_cpp properties is for intellisense. It is not used for compiling. The compiler is there only because it is queried to find the default include paths it uses for system headers.

The tasks defines how the build task runs.

Vscode doesn't connect the two.



Related Topics



Leave a reply



Submit