using linux header files in visual studio
I was trying to use linux kernel header file on the visual studio 2013. I want to do this for auto completion purpose. I noticed that stdio.h file is inside of the C:\Program Files\Visual Studio 12.0\VC\crt\src, So I copied my linux folder, which contains kernel header files, into the src folder. However, Visual studio does not seem to find these header files. For example: #include
3 Answers 3
and add C:\Program Files\Visual Studio 12.0\VC\crt\src\linux to the include path (if one is using VS2017 which shipped with it).
To obtain the kernel.h file, i.e. kernel source, determine the kernel version one needs and obtain the src from kernel.org, e.g. 5.4.26
@GrantCurell I think the usually most reliable way of obtaining the file is to get it from kernel.org
In Visual Studio 2015, the alternative is to install the Visual C++ for Linux Development and the Visual C++ for Android Development templates. At time of writing the Linux headers are only present in the Android development folder, on my machine it was C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r10e\platforms\android-21\arch-x86_64\usr\include . Add that path to the include path and Intellisense works for common Linux files like .
Current location(s) under VS 2017 with Android files in a separate tree, 32 and 64 bit:
C:\Microsoft\AndroidNDK\android-ndk-rc\platforms\android-\arch-x86\usr\include NDK_VER -- versions of the NDK, currently 12,13,15 P_VER -- target android platform version (aka level), currently the highest is 26
C:\Microsoft\AndroidNDK\android-ndk-r15c\platforms\android-26\arch-x86\usr\include C:\Microsoft\AndroidNDK\android-ndk-r15c\platforms\android-26\arch-x86_64\usr\include C:\Microsoft\AndroidNDK64\android-ndk-r15c\platforms\android-26\arch-x86\usr\include C:\Microsoft\AndroidNDK64\android-ndk-r15c\platforms\android-26\arch-x86_64\usr\include
This reflects general «new style» in VS with build platforms and target platforms being separated and orthogonal. There are also arch- so 6 folders under arch target platform version.
Include files are the same in AndroidNDK and AndroidNDK64 and under one NDK version only files under machine and asm folders are different (for Intel and Mips even files under asm are identical for 32 and 64 bits).
How to set include paths within Windows Subsystem for Linux in Visual Studio Code
at the moment I am using a windows machine and wanted to try out windows subsystem for linux(wsl) for c++ development. Visual studio code(vsc) provides a neat extension for working on the subsystem that works quite well. One thing I could not get to work yet is getting vsc to include header files that are located inside the subsystem. I know that I can manually add new paths to include in ´c_cpp_properties.json´ but I do not know any of the paths to point from windows into the subsystems /usr/include(as an example). I found some resources (wsl include paths) but could not get it to work anyway. I also tried generating the paths myself as described in the gitlab issue but that did not help either. Anyone got some experience setting up vsc with wsl for c++ projects who would be able to help me?
Kinda. But I could not find information on how to set include paths on there ? Or am I just blind .. 😀
I have seen that people use tools like rsync to just copy the needed header files to the local machine to point vac to them. That seems kinda tedious in my opinion. Still looking for a more integrated way.
What distro are you using in WSL, and did you install the necessary packages and VS Code extensions already?
3 Answers 3
The question is missing some information, like what distro you’re using and what setup steps you have followed. This means I will make some assumptions and repeat information you may already know.
Assumption: a Debian-based distro sudo apt update && sudo apt upgrade -y && sudo apt install build-essential -y You may also need to install git or cmake or whatever other tools are needed by your project.
Install the C/C++ extension into the remote. Install any other extensions that would be beneficial, like cmake if you’re using cmake.
Settings that need to be changed (global)
«C_Cpp.default.compilerPath»: «/absolute/path/to/your/compiler»,
«files.eol»: «\n»,
«C_Cpp.default.cppStandard»: «»,
«C_Cpp.default.cStandard»: «»,
«C_Cpp.default.intelliSenseMode»: «»,
The last three need to be set according your company/project guidelines.
At this point, you should be able to write C++ code that uses the C++ Standard Library and have proper Intellisense working.
Your main question seems to be about adding project specific include paths. That is handled by the c_cpp_properties.json file. Your hard drives are located at /mnt . Specify your paths. The better approach, though, is to simply use the VS Code variables. Something like $/include should be all you need.
C/C++ Properties (Linux C++)
Linux support is available in Visual Studio 2017 and later.
General
Property | Description | Choices |
---|---|---|
Additional Include Directories | Specifies one or more directories to add to the include path. Use semi-colons to separate multiple directories. (-I[path]). | |
Debug Information Format | Specifies the type of debugging information generated by the compiler. | None — Produces no debugging information, so compilation may be faster. Minimal Debug Information — Generate minimal debug information. Full Debug Information (DWARF2) — Generate DWARF2 debug information. |
Object File Name | Specifies a name to override the default object file name. It can be a file or directory name. (-o [name]). | |
Warning Level | Selects how strict you want the compiler to be about code errors. Add other flags directly to Additional Options. (/w, /Weverything). | Turn Off All Warnings — Disables all compiler warnings. EnableAllWarnings — Enables all warnings, including ones disabled by default. |
Treat Warnings As Errors | Treats all compiler warnings as errors. For a new project, it may be best to use /Werror in all compilations. Resolve all warnings to ensure the fewest possible hard-to-find code defects. | |
C Additional Warnings | Defines a set of additional warning messages. | |
C++ Additional Warnings | Defines a set of additional warning messages. | |
Enable Verbose mode | When Verbose mode is enabled, prints out more information to diagnose the build. | |
C Compiler | Specifies the program to invoke during compilation of C source files, or the path to the C compiler on the remote system. | |
C++ Compiler | Specifies the program to invoke during compilation of C++ source files, or the path to the C++ compiler on the remote system. | |
Compile Timeout | Remote compilation timeout, in milliseconds. | |
Copy Object Files | Specifies whether to copy the compiled object files from the remote system to the local machine. | |
Max Parallel Compilation Jobs | The number of processes to create in parallel during compilation. The default is 1. If you’re using Windows Subsystem for Linux (WSL) version 1, the limit is 64. | |
Validate Architecture | Specify whether to check if the platform the project targets matches the remote system. | |
Enable Address Sanitizer | Compile the program with Address Sanitizer, which is a fast memory error detector that can find runtime memory issues such as use-after-free, and perform out of bounds checks. |
Optimization
Property | Description | Choices |
---|---|---|
Optimization | Specifies the optimization level for the application. | Custom — Custom optimization. Disabled — Disable optimization. Minimize Size — Optimize for size. Maximize Speed — Optimize for speed. Full Optimization — Expensive optimizations. |
Strict Aliasing | Assumes the strictest aliasing rules. An object of one type is never assumed to have the same address as an object of a different type. | |
Unroll Loops | Unrolls loops to make the application faster by reducing the number of branches executed, at the cost of larger code size. | |
Link Time Optimization | Enables inter-procedural optimizations by allowing the optimizer to look across object files in your application. | |
Omit Frame Pointer | Suppresses creation of frame pointers on the call stack. | |
No Common Blocks | Allocates even uninitialized global variables in the data section of the object file, rather than generate them as common blocks. |
Preprocessor
Property | Description |
---|---|
Preprocessor Definitions | Defines preprocessing symbols for your source file. (-D) |
Undefine Preprocessor Definitions | Specifies one or more preprocessor undefines. (-U [macro]) |
Undefine All Preprocessor Definitions | Undefines all previously defined preprocessor values. (-undef) |
Show Includes | Generates a list of include files with compiler output. (-H) |
Code Generation
Property | Description | Choices |
---|---|---|
Position Independent Code | Generates position-independent code (PIC) for use in a shared library. | |
Statics are thread safe | Emits extra code to use routines specified in the C++ ABI for thread-safe initialization of local statics. | No — Disable thread-safe statics. Yes — Enable thread-safe statics. |
Floating Point Optimization | Enables floating-point optimizations by relaxing IEEE-754 conformance. | |
Inline Methods Hidden | When enabled, out-of-line copies of inline methods are declared private extern . | |
Symbols Hidden By Default | All symbols are declared private extern unless explicitly marked for export by using the __attribute macro. | |
Enable C++ Exceptions | Specifies the exception-handling model used by the compiler. | No — Disable exception handling. Yes — Enable exception handling. |
Language
Property | Description | Choices |
---|---|---|
Enable Run-Time Type Information | Adds code for checking C++ object types at run time (runtime type information). (frtti, fno-rtti) | |
C Language Standard | Determines the C language standard. | Default C89 — C89 Language Standard. C99 — C99 Language Standard. C11 — C11 Language Standard. C99 (GNU Dialect) — C99 (GNU Dialect) Language Standard. C11 (GNU Dialect) — C11 (GNU Dialect) Language Standard. |
C++ Language Standard | Determines the C++ language standard. | Default C++03 — C++03 Language Standard. C++11 — C++11 Language Standard. C++14 — C++14 Language Standard. C++03 (GNU Dialect) — C++03 (GNU Dialect) Language Standard. C++11 (GNU Dialect) — C++11 (GNU Dialect) Language Standard. C++14 (GNU Dialect) — C++14 (GNU Dialect) Language Standard. |
Advanced
Property | Description | Choices |
---|---|---|
Compile As | Selects compilation language option for .c and .cpp files. (-x c, -x c++) | Default — Detect based on the .c or .cpp extension. Compile as C Code — Compile as C code. Compile as C++ Code — Compile as C++ code. |
Forced Include Files | Specifies one or more forced include files (-include [name]) |