How do I set include path library directory and linker for VSCODE when I have Visual C++ build tools to work with (not g++)
As mentioned in the comments, you must use a C++ build system to manage dependencies and build your project. VSCode doesn't come with built-in build systems like Visual Studio.
VSCode tasks allow you to specify a command line, which can then be easily invoked in the IDE. The task shown is just a task of creating a running file, which is only really useful for trivial programs with no dependencies. It callscl.exe
to the current source file (passing a few other arguments).
You can specify include directories and pass arguments to the linker by adding "args" to the array in the task, e.g. e.g.:
"/I", "D:\\Codebibliotheken\\boost_1_77_0",
"/link", "/LIBPATH:\"D:\\Codebibliotheken\\boost_1_77_0\\stage\\lib\"",
which assumes the Boost headers and (statically created) libraries are in the specified locations.
You could probably figure out how to build an entire project by adding command lines with VSCode tasks, but it's probably easier to use a build system (even if that system is CMake).
VSCode doesn't recognize includes from includepath
You haven't told your compiler about a file called Zipper.h or its location or anything related to it. "g++ test.cpp -o test" just tells the compiler to compile and link a source file called test.cpp. You must understand that Visual Studio Code is not an IDE and cannot be compiled by yourself. You should have a file called c_cpp_properties.json file in your .vscode directory. The one I'm using 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": {
"Away": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": wahr,
"DatabaseFilename": ""
}
}
],
"Version": 4
}
This tells Visual Studio Code where your source files and libraries are located. This is used for intellisense (syntax highlights, error squiggles, code completion, etc.). However, this has absolutely nothing to do with the construction of your project. Now your compiler doesn't know about the include paths you set in Visual Studio Code. So in order to compile your project, you need to tell your compiler everything it needs to know. Visual Studio Code simply runs what you specify in the task. It's the same as going into that directory and typing the same thing into your command prompt. So I recommend you to read up on how to compile a C++ project with g++, your problem has nothing to do with Visual Studio Code at all. If you're planning on doing something a little larger than just a single source file, I highly recommend you learn CMake. Compiling by manually invoking gcc gets really complicated once you have more source files and includes/libraries to link. Once you have your cmake set up, you can simply specify a task in Visual Studio Code similar to this to build your project:
{
"Version": "2.0.0",
"Tasks": [
{
"label": "Build",
"type": "shell",
"Befehl": "cmake --build Build",
"Group": {
"Type": "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 what exactly Microsoft is trying to do and helped me understand this when I first started using Visual Studio Code for my C++ work.
Visual Studio Code, #include stdio.h labeled Add include path to settings
A more recent view of the situation. During 2018, the C++ extension added another option to the configurationCompilerPfad
of thec_cpp_properties.json
File;
CompilerPfad
(Optional)
The absolute path to the compiler you are using to build your project. The extension queries the compiler to determine the system include paths and default definitions to use for IntelliSense.
If used, theincludePath
not required as IntelliSense uses the compiler to figure out the system include paths.
Originally,
How and where can I add include paths in the following configurations?
The list is a string array, so adding an include path would look something like this:
"Configurations": [
{
"name": "Mac",
"includePath": ["/usr/local/include",
"/path/to/additional/contains",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include"
]
}
]
Source; cpptools blog March 31, 2016.
The linked source has a gif showing the format for the Win32 configuration, but the same goes for the others.
The example above includes the SDK path (OSX 10.11) when Xcode is installed.
noteI find that the update can take a while once the include path has been changed.
The cpptools extension can be found here.
More documentation (from Microsoft) on C++ language support in VSCode can be found here.
For the sake of retention (from discussion) below are basic snippets for the contents of the tasks.json file to compile and run either a C++ file or a C file. They allow spaces in the filename (requires escaping the extra quotes in the json with\"
). The shell is used as a runner, enabling compilation (clink ...
) and the execution (&& ./a.out
) of the program. It also assumes that the tasks.json file "lives" in the local workspace (under the .vscode directory). Further details on task.json, such as supported variables etc. can be found here.
For C++;
{
"Version": "0.1.0",
"isShellCommand": true,
"taskName": "GenericBuild",
"showOutput": "always",
"Command": "sh",
"suppressTaskName": false,
"args": ["-c", "clang++ -std=c++14 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"]
}
for C;
{
"Version": "0.1.0",
"isShellCommand": true,
"taskName": "GenericBuild",
"showOutput": "always",
"Command": "sh",
"suppressTaskName": false,
"args": ["-c", "clang -std=c11 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"] // Befehlsargumente...
}
Visual Studio-Code: C++-Include-Pfad
Okay, that was silly, but in case anyone uses itVisual Studio-Code
and has no trivial project. These instructions assume you are using the Clang compiler:
- Open your project directory
- Open
.vscode/settings.json
Configure the following line inside the JSON object:
// compiler options for C++ (e.g. ['-std=c++11'])
"clang.cxxflags": [
"-I/path/to/my/include/directory" // Header-Dateien
],
Elegant solution for VS Code C/C++, including path for IntelliSense and building project
Here's what I do: Uninstall Microsoft's C/C++ extension and replace it with Clangd for code completion and Native Debug for debugging. I've had better experiences with these two, including an easier configuration.
Clangd is configured by a single file calledcompile_commands.json
, which is just a list of compiler flags for each source file.
Then start using an appropriate build system. For example, CMake can generate this file by default. For Make there is a tool to generate it. In a pinch, you can write it yourself.
Related topics
Here's how to peek at the next element in a range-for loop
Why should I use <Bits/Stdc++.H≫
What are R-values, L-values, X-values, Gl-values and Pr-values?
How to get the list of files in a directory using C or C++
Output Unicode strings in the Windows console app
How to implement classic sorting algorithms in modern C++
Why are C character literals ints instead of chars?
How to declare an interface in C++
Avx2 What is the most efficient way to pack links based on a mask
View a C/C++ source file after preprocessing in Visual Studio
Vector converted all negative values to zero
What is object slicing?
Passing a 2D array to a C++ function
Can num++ for 'int num' be atomic?
"Unwrap" a tuple to invoke an appropriate function pointer
What is a virtual base class in C++?
Is Delete[] equal to Delete
What is the lifetime of a static variable in a C++ function?
FAQs
HOW include C++ libraries in VS Code? ›
Install Visual Studio Code. Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X). Install the Microsoft Visual C++ (MSVC) compiler toolset.
How do I change the include path in Visual Studio code? ›Visual Studio Code and the C/C++ language plugin need to be able to locate all of the header files referenced in our programme in order to understand it. The lightbulb to the left of the error will be seen if the pointer is on it. Then update the “includePath” setting by clicking that.
What is includePath in Visual Studio Code? ›includePath An include path is a folder that contains header files (such as #include "myHeaderFile. h" ) that are included in a source file. Specify a list of paths for the IntelliSense engine to use while searching for included header files. Searching on these paths is not recursive.
How will you include a library in C++? ›Right-click on the application project node in Solution Explorer and then choose Properties. In the VC++ Directories property page, add the path to the directory that contains the LIB file to Library Paths. Then, add the path to the library header file(s) to Include Directories.
Can you include C libraries in C++? ›Yes - C++ can use C libraries. Except of course that you're using the C++ version of cstdio . To use the C one you need to #include <stdio. h> .
How do I get the full path of a file in VS Code? ›- Select File > Preference > Settings. Visual Studio Code Settings.
- Click on WORKSPACE SETTINGS. Visual Studio Code Workspace Settings.
- Type window.title into the search bar. ...
- Click on the pencil tool and select Copy to Settings. ...
- Set the title to ${dirty}${activeEditorLong}
In the Windows search bar, type 'settings' to open your Windows Settings. Search for Edit environment variables for your account. Choose the Path variable in your User variables and then select Edit. Select New and add the Mingw-w64 destination folder path to the system path.
How do I change the full path in Visual Studio? ›In Visual Studio, click Tools > Options. Expand Projects and Solutions and click Locations. The Projects location field defines the default location for storing new projects. You can change this path if you are using a different working folder.
How do I add the include path in Visual Studio? ›Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio. Select the Configuration Properties > C/C++ > General property page. Modify the Additional Include Directories property.
How to setup cpp in VS Code? ›- Install Mingw-w64 via the SourceForge website. Click Mingw-w64 to download the Windows Mingw-w64 installer.
- Run the installer.
- For Architecture select x86_64 and then select Next.
- Add the path to your Mingw-w64 bin folder to the Windows PATH environment variable by using the following steps:
Where is C_cpp_properties JSON in VS Code? ›
(solved) VS Code: proper location for c_cpp_properties. json? The c_cpp_properties. json is a file specific to the Microsoft C/C++ extension and usually exists only in the workspace folder.
What is the blue squiggly line in VS code? ›It means you've modified that line since you last made a commit in version control.
How do you get rid of the blue squiggly lines in Vscode? ›- Press command+shift+p (open command pallete)
- Then type Disable Error Squiggles.
- And click on that Disable Error Squiggles.
- Right click in the status bar.
- Uncheck the workspace trust entry.
If your class declaration references types in the header, you will need to include it there. If it's only in the implementation, then you can include it in the cpp file.
What are the 3 main libraries of C++? ›The Diagnostics Library. The General Utilities Library.
How do I add a library to Visual Studio Code? ›Start Visual Studio Code. In the Open Folder dialog, create a ClassLibraryProjects folder and click Select Folder (Open on macOS). Open the Terminal in Visual Studio Code by selecting View > Terminal from the main menu. The Terminal opens with the command prompt in the ClassLibraryProjects folder.
Does C++ have a lot of libraries? ›The only problem is, since C++ is not a new language, and it has a large community in stack, there are many libraries you can make use of, so choosing the right ones can be quite a challenge.
Does C++ have a JSON library? ›There's no native support for JSON in C++ but there are a number of libraries that provide support for working with JSON. Perhaps the most widely used is JsonCpp, available from GitHub at https://github.com/open-source-parsers/jsoncpp.
How do I install all C++ libraries? ›- Acquiring the Library. In order to use the library in our application, we first need to acquire the library. ...
- Install the Library. The next step once we obtain the library is to install the library. ...
- Include the Library path for the Compiler. ...
- Link the Library path.
What is relative path vs absolute path? ›
A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory. When learning to access data files through programming, we regularly use relative file paths.
How do you set a path to System Variables? ›- In Search, search for and then select: System (Control Panel)
- Click the Advanced system settings link.
- Click Environment Variables. ...
- In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. ...
- Reopen Command prompt window, and run your java code.
Start the System Control Panel applet (Start - Settings - Control Panel - System). Select the Advanced tab. Click the Environment Variables button. Under System Variables, select Path, then click Edit.
How to install gcc and g ++? ›- Step 2: Download MinGW. ...
- Step 3: Locate the MinGW-get-setup.exe File and Start Installation. ...
- Step 4: Specify Installation Preferences. ...
- Step 5: Download and Set up MinGW Installation Manager. ...
- Step 6: Select Packages Required for the Compiler. ...
- MinGW32-gcc-g++ Package.
- Go to Windows Start and type REGEDIT.
- Choose the Registry Editor.
- In the Registry Editor, navigate to the following location: at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem.
- Select the entry named: LongPathsEnabled.
To enable long file paths in Windows, open Registry Editor, create a new DWORD named "LongPathsEnabled" in "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem" and set the value to 1. Pro and Enterprise users can enable "Enable Win32 Long Paths" in the Local Group Policy Editor.
How do I fix my path problem? ›- Solution 1: Check path for errors.
- Solution 2: Check directory or folder.
- Solution 3: Remove invalid environment variable.
- Solution 4: Repair system files with SFC and DISM.
- Solution 5: Clean up Windows update.
- Solution 6: Reset Windows update components.
Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio. Select the Configuration Properties > Linker > Advanced property page. Modify the Import Library property.
How do I link libraries in VS Code? ›...
Extension Settings
- Open command panel ( Shift+CMD+P on OSX or Shift+Ctrl+P on Windows and Linux).
- Search for 'Extlibraries: Add external library' and press Intro.
- Input external directory or file path.
- Input name and press Intro.
- In Solution Explorer, right-click the References or Dependencies node, and then choose either Add Project Reference, Add Shared Project Reference, or Add COM Reference from the context menu. ...
- Select a reference to add, and then select OK.
How to install cpp library? ›
Installation on Windows is as simple as unzipping the contents of the library in a folder. On Linux, we can invoke the package manager to install the library. We need to set the path of the library so that the compiler knows where to look for the library files.
How do I manually add a library to processing? ›Find the 'Processing' folder
Drag and drop the contributed library into the folder 'libraries' -- you should be able to add as many libraries like this as you want.
The difference between import and from import in Python is: import imports an entire code library. from import imports a specific member or members of the library.
How do I import library files? ›You can use LIB with the /DEF option to create an import library and an export file. LINK uses the export file to build a program that contains exports (usually a dynamic-link library (DLL)), and it uses the import library to resolve references to those exports in other programs.
How do I link a static library in code blocks? ›Go to Project->Build Options->(Select project name) . Then select the Linker settings and click on the Add button under Link Libraries , and select libmysql. lib . This should statically compile your program, AFAIK.
How do I link libraries with GCC? ›- Change to the directory containing your code.
- Compile the program source files with headers of the foo library: Copy. Copied! $ gcc ... - Iheader_path -c ... ...
- Link the program with the foo library: Copy. Copied! $ gcc ... - Llibrary_path -lfoo ... ...
- To execute the program, run: Copy. Copied! $ ./program.
- git clone vcpkg into home directory.
- add VCPKG_ROOT env variable that points to the directory.
- vcpkg install catch2.
- vcpkg integrate install.
- open vs code, I have already installed required cmake plugins.
- configure cmake project.