Node addon development - cannot open source file "napi.h" - c++

Just started fiddling around with developing an addon for Node in C++ using Node API (for the first time, I might just add). Everything is working great for the most part - I'm using the built-in compiler in Visual Studio for starting it, debugging and playing around, and node-gyp for building it for requiring it in Node. I have an entrypoint for local testing, let's call it main.cpp, where my int main() function is defined - which is executed when I run/debug my solution in Visual Studio (using Ctrl+F5/F5).
I also have a source file where the 'node communication' is defined (NodeCommunication.cpp), and it is this file (among others) that is listed in the sources array in my binding.gyp file. It is here that napi.h is included as well, and its APIs are used. Mousing over the include however, there is error text saying "cannot open source file napi.h" - which makes sense, I guess (?) that header file is something that is only injected on build time in node-gyp somehow. However it'd be really nice, DX-wise, to be able to resolve those somehow, because right now I have to comment out all the source code in that file before compiling and running the solution in visual studio for testing purposes.
What are my options here? Might as well also attach my binding.gyp file for reference.
{
"targets": [
{
"target_name": "foobar",
"sources": [ "NodeCommunication.cpp", "Sample.cpp", "Sample.h" ],
"libraries": ["../SDK/Library/EDSDK.lib"],
'include_dirs': ["<!#(node -p \"require('node-addon-api').include\")"],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'defines': [ 'NAPI_CPP_EXCEPTIONS' ]
}
]
}

Related

#include errors detected. Please update your includePath in visual studio code

I'm trying to compile C++ files in Visual Studio Code How to use C/Cpp extension and add includepath to configurations but I'm getting this errors:
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit.
cannot open source file "iostream"
I already saw this question and any of the solutions worked for me. Also I recorded a video following the steps of the visual studio code tutorial. Here is my code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
}
And this are my C/C++ settings:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\x86_64-w64-mingw32\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
open command pallete (ctrl + shift + P) then search edit configuration , select C/C++ edit configuration(UI) . Then scroll down and you will see include path , copy the location of your include path by finding it in file manager , By default the location would be C:\Program Files(x86)\mingw-w64\......\mingw32\include. Set intellisense to default. Now you are ready to enjoy your coding journey.
Got the same error.
I just disabled the C/C++ Intellisense extension of microsoft and reloaded it.
If you got this problem, Relax, u r probablu 1000th one here ;)
I have added screenshots, these are not links to some terrifying third party documentations (;
Solution
1.installation of c++ :
check you have the c++ installed, (it dosent come pre-installed! You have to do it)
If not install it. If you have installed it, navigate to it, and get the path
click on the path at top in file explorer, and Ctrl+C to copy it here is how u get the path
2.path environment variable setup
steps:
i.left click This pc(in file explorer)here
ii. manage this menu will open
iii. scroll down in the about section, find "Advanced System Settings" and follow along this
V. "edit environment variable" dialogue box will open, click new, then paste the link to the gcc compiler that you installed firstly.
Now, this path is set as environment variable, try running command : gcc --version in command panel, if still there is error, try uploading different paths of gcc.exe to the environment variable "Path". Also, re-starting pc is recommended if path variable change doesn't appear.
Now we start(or restart if it was open) the Vs-code(just a little to go)
i. Shift+Ctrl+p then : follow this
ii.then this
if the dropdown dosnt show any option, try re-starting vs-code, or pc, re-setting the environment variable step, or re-installing c++ and starting over(in worst case)
iii.scroll down the same page, and paste the path to include folders from c++ to text box corrosponding to "Include path" . you have to first get this path, search for folder with name "include", copy and add its path.(many times, ide is capable to automate this step;))
Now try running the code, it must be good to go, but iff it still throws error, try repeating process of adding environment variable, re-staqrting vscode, or even then, re-installing c++.

Setup vscode fortran intellisense to get procedure information from external library/module

I'm using vscode as editor, under Ubuntu system, for programming in Fortran. I decided to build a static library for not compiling it from the beginning each time I compile the main. Now when I write a code using that library, fortran intellisense wants the .mod file to be in the same directory of the main file, but I'd like to avoid that (since it is an archive so I have more .mod files).
I've already setup the fortran intellisense IncludePath to include the folder where the .mod files are present, it doesn't give me an error, but it still cannot find the module in the project, so when I write a procedure, I'm not able to check the arguments and it is quite annoying, is it possible to solve this without copying the .mod files in the same folder of the main?
The fortran linter (Modern Fortran + fortran IntelliSense) actually compiles your code (without producing any exe) in order to display warnings and find files. This results in a lot of .mod files spawning in your code/ directory which is not convenient at all.
To solve this, you need to tell it where to put the .mod files. This should be done in the workspace settings.
Create a “.fmod/” repo at the root of your workspace.
Open workspace.code-workspace at the root of your workspace and add the following lines in it:
"settings": {
"fortran.linterExtraArgs": [
"-J../.fmod", "Wall",
],
"fortran.includePaths": [
"../.fmod"
],
"files.exclude": {
"**/.fmod": true },
}

VSCode C++ IntelliSense works fine except for PCL (Point Cloud Library)

On SO itself, there are already many questions about VSCode IntelliSense, e.g., que1, que2, que3, etc. However, they mainly talk about IntelliSense not working in general; not working for STL; and so on.
On the other hand, in my case, I'm already using VSCode C/C++ Extension by Microsoft and IntelliSense works fine for all the different in-built classes, functions, STLs, etc. But, when it comes to PCL (Point Cloud Library), somehow, mysteriously, IntelliSense doesn't work.
If you are familiar with PCL then must be knowing that most of its syntaxes are too big. And as the IntelliSense not working for it, there are very high chances of making typos if you don't pay close attention while typing every single character, e.g.,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
In fact, auto-completion for even those variables and functions do not work--i.e., from the above example, I have to manually type every single character of point_cloud_ptr every time I want to use it.
So, how can I fix this? or it's not possible for PCL?
Note: I'm facing this issue on my Ubuntu 16 as well as on Ubuntu 18. I have installed PCL using sudo apt install libpcl-dev and the header files are located in /usr/include/ directory.
After trying out different things and getting help from Microsoft VSCode (and C/C++ extension) team on GitHub, it looks like the IntelliSense is not working probably because the extension is not able to find PCL headers.
In the case of Ubuntu, the PCL headers are generally stored at /usr/include/pcl-<version>. In my Ubuntu 18.04, I have pcl-1.8. So, ideally, adding "/usr/include/pcl-1.8/**" under "includePath" in c_cpp_properties.json file should work. e.g.
{
"configurations": [
{
.
.
"includePath": [
"${workspaceFolder}/**",
"/usr/include/pcl-1.8/**"
],
.
.
.
}
],
"version": 4
}
In "/usr/include/pcl-1.8/**", ** is for recursive search under /usr/include/pcl-1.8/ directory. However, by the time when I had posted the question (and I think as of today as well), it doesn't work. It's not able to go through the subdirectories under pcl-<version> directory. So, alternate solutions are below:
Solution 01: (Use this one if you want to apply changes only to the current project)
Open C/C++ Extension Configuration file: c_cpp_properties.json (On Linux, press Ctrl + Shift + P and select C/C++: Edit Configurations (JSON))
Now, as the recursive search is not working, an alternate way is to specify all the subdirectories under includePath:
{
"configurations": [
{
.
.
"includePath": [
"${workspaceFolder}/**",
"/usr/include/eigen3",
"/usr/include/pcl-1.8",
"/usr/include/pcl-1.8/pcl",
"/usr/include/pcl-1.8/pcl/2d",
"/usr/include/pcl-1.8/pcl/apps",
"/usr/include/pcl-1.8/pcl/common",
"/usr/include/pcl-1.8/pcl/compression",
"/usr/include/pcl-1.8/pcl/console",
"/usr/include/pcl-1.8/pcl/features",
"/usr/include/pcl-1.8/pcl/filters",
"/usr/include/pcl-1.8/pcl/geometry",
"/usr/include/pcl-1.8/pcl/impl",
"/usr/include/pcl-1.8/pcl/in_hand_scanner",
"/usr/include/pcl-1.8/pcl/io",
"/usr/include/pcl-1.8/pcl/kdtree",
"/usr/include/pcl-1.8/pcl/keypoints",
"/usr/include/pcl-1.8/pcl/ml",
"/usr/include/pcl-1.8/pcl/modeler",
"/usr/include/pcl-1.8/pcl/octree",
"/usr/include/pcl-1.8/pcl/outofcore",
"/usr/include/pcl-1.8/pcl/people",
"/usr/include/pcl-1.8/pcl/range_image",
"/usr/include/pcl-1.8/pcl/recognition",
"/usr/include/pcl-1.8/pcl/registration",
"/usr/include/pcl-1.8/pcl/ros",
"/usr/include/pcl-1.8/pcl/sample_consensus",
"/usr/include/pcl-1.8/pcl/search",
"/usr/include/pcl-1.8/pcl/segmentation",
"/usr/include/pcl-1.8/pcl/stereo",
"/usr/include/pcl-1.8/pcl/surface",
"/usr/include/pcl-1.8/pcl/tracking",
"/usr/include/pcl-1.8/pcl/visualization"
],
.
.
.
}
],
"version": 4
}
Solution 02: (Use this one if you want to apply changes globally -- across all the projects)
As C/C++ Configuration File: c_cpp_properties.json is specific to each project, editing that will only apply changes to the current project. So, to apply changes across all the projects, one has to update settings.json and then c_cpp_properies.json.
Open settings.json and add the following text:
"C_Cpp.default.includePath": [
"/usr/include/eigen3",
"/usr/include/pcl-1.8",
"/usr/include/pcl-1.8/pcl",
"/usr/include/pcl-1.8/pcl/2d",
"/usr/include/pcl-1.8/pcl/apps",
"/usr/include/pcl-1.8/pcl/common",
"/usr/include/pcl-1.8/pcl/compression",
"/usr/include/pcl-1.8/pcl/console",
"/usr/include/pcl-1.8/pcl/features",
"/usr/include/pcl-1.8/pcl/filters",
"/usr/include/pcl-1.8/pcl/geometry",
"/usr/include/pcl-1.8/pcl/impl",
"/usr/include/pcl-1.8/pcl/in_hand_scanner",
"/usr/include/pcl-1.8/pcl/io",
"/usr/include/pcl-1.8/pcl/kdtree",
"/usr/include/pcl-1.8/pcl/keypoints",
"/usr/include/pcl-1.8/pcl/ml",
"/usr/include/pcl-1.8/pcl/modeler",
"/usr/include/pcl-1.8/pcl/octree",
"/usr/include/pcl-1.8/pcl/outofcore",
"/usr/include/pcl-1.8/pcl/people",
"/usr/include/pcl-1.8/pcl/range_image",
"/usr/include/pcl-1.8/pcl/recognition",
"/usr/include/pcl-1.8/pcl/registration",
"/usr/include/pcl-1.8/pcl/ros",
"/usr/include/pcl-1.8/pcl/sample_consensus",
"/usr/include/pcl-1.8/pcl/search",
"/usr/include/pcl-1.8/pcl/segmentation",
"/usr/include/pcl-1.8/pcl/stereo",
"/usr/include/pcl-1.8/pcl/surface",
"/usr/include/pcl-1.8/pcl/tracking",
"/usr/include/pcl-1.8/pcl/visualization"
]
Open, c_cpp_properties.json and update includePath:
{
"configurations": [
{
.
.
"includePath": [
"${workspaceFolder}/**",
"${default}"
],
.
.
.
}
],
"version": 4
}
P.S. I had asked the same question on the official GitHub issues page of the C/C++ Extension by Microsoft and eventually, found the solution (but, not an ideal one) I mentioned above.
Here is the link for more info: https://github.com/microsoft/vscode-cpptools/issues/5843
Because VSCode always told me that it could not find some dependency like pcl/PCLHeader.h. So I tried copying /usr/include/pcl-1.10/pcl to /usr/include/ and it worked.

#include errors detected in vscode

I am using Visual Studio Code in my C++ project. I installed Microsoft C/C++ Extension for VS Code. I got the following error:
#include errors detected. Please update your includePath. IntelliSense features for this translation unit (/path/to/project/file.cpp) will be provided by the Tag Parser.
Close and re-open Visual Studio Code.
The answer is here: How to use C/Cpp extension and add includepath to configurations.
Click the light bulb and then edit the JSON file which is opened. Choose the right block corresponding to your platform (there are Mac, Linux, Win32 – ms-vscode.cpptools version: 3). Update paths in includePath (matters if you compile with VS Code) or browse.paths (matters if you navigate with VS Code) or both.
Thanks to #Francesco Borzì, I will append his answer here:
You have to Left 🖰 click on the bulb next to the squiggled code line.
If a #include file or one of its dependencies cannot be found, you can also click on the red squiggles under the include statements to view suggestions for how to update your configuration.
If you are working with cmake-tools and the error messages says something is wrong with the configurationProvider, then you have 2 options:
Use ms-vscode.cpptools instead of ms-vscode.cmake-tools
Define the headers in CMakeLists.txt
Option 1: Use ms-vscode.cpptools instead of ms-vscode.cmake-tools.
Open c_cpp_properties.json. (windows key on windows or cmd key on mac + shift + p, enter "c/c++ edit configurations" and chose 'json'.
Enter ms-vscode.cpptools as value for configurationProvider instead of ms-vscode.cmake-tools or whatever you have.
How it should look like after the replacement of configurationProvider:
One other important configuration is the include path. The assumption is that you have this configuration right. May be like following
Option 2: Define the headers in CMakeLists.txt
When configurationProvider is set to ms-vscode.cmake-tools in c_cpp_properties.json, VS Code uses the include information defined in the CMakeLists.txt instead of reading the includePath configs in VS Code.
So you need to setup the include path correctly:
using the include_directories command (rather than the target_include_directories command) to define the headers
Configure the project to reflect the change happened in the previous step.
Left mouse click on the bulb of error line
Click Edit Include path
Then this window popup
Just set Compiler path
I ended up here after struggling for a while, but actually what I was missing was just:
If a #include file or one of its dependencies cannot be found, you can also click on the red squiggles under the include statements to view suggestions for how to update your configuration.
source: https://code.visualstudio.com/docs/languages/cpp#_intellisense
The error message "Please update your includePath" does not necessarily mean there is actually a problem with the includePath. The problem may be that VSCode is using the wrong compiler or wrong IntelliSense mode. I have written instructions in this answer on how to troubleshoot and align your VSCode C++ configuration with your compiler and project.
I'm on a Macbook M1 Pro, and I had red squiggly error lines all over my C++ files. To solve, I did this:
Open Preferences -> Settings
Search for intelliSenseEngine
Change "C_Cpp: Intelli Sense Engine" from Default to Tag Parser
alternatively, you could create a .vscode folder in your project root, and then create a settings.json with the content of
{
"C_Cpp.intelliSenseEngine" : "Tag Parser"
}
ideally, you should have a c_cpp_properties.json file in the same folder with the right settings as well.
Note: I got this idea from Github here.
I was trying a hello world program, and this line:
#include <stdio.h>
was underlined green. I tried:
Deleting the line
Re-writing the line
Clicking the yellow bulb and choosing to update
fixed the error warning. i don't know if it fixed the actual problem. But then i'm compiling via a linux VM on Windows 10
Go to your c_cpp_properties.json file by searching from settings.There you might see the following code
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
Change the compiler path as below
"compilerPath": "/usr/bin/g++",
After closing and reopening VS, this should resolve.
For Windows:
1.Install Mingw-w64
2.Then Edit environment variables for your account "C:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin"
3.Reload
For MAC
1.Open search ,command + shift +P, and run this code “c/c++ edit configurations (ui)”
2.open file c_cpp_properties.json and update the includePath from "${workspaceFolder}/**" to "${workspaceFolder}/inc"
If someone have this problem, maybe you just have to install build-essential.
apt install build-essential
For me, using Ubuntu, I just had to install gcc to solve this issue.
sudo apt install gcc
Then, set the compiler path to gcc. Go to your c_cpp_properties.json file, set:
"compilerPath": "/usr/bin/gcc"
An alternative answer would be opening VS Code in remote WSL, if you going to compile files with g++. Just close your VS Code and open WSL and type code . After that the File Explorer shows that VS Code is now running in the context of WSL with the title bar [WSL: Ubuntu]. But make sure you'd installed the GNU compiler tools and the GDB debugger on WSL.
source: https://code.visualstudio.com/docs/cpp/config-wsl
In my case I did not need to close the whole VS-Code, closing the opened file (and sometimes even saving it) solved the issue.
I had luck removing the comments from c_cpp_properties.json in the .vscode folder. Comments aren't permitted in json files by default and you can't simply rename it .jsonc. Referenced In VS Code, disable error "Comments are not permitted in JSON"
My header file was in include/head.h, code in src/code.cpp. I wrote
#include "head.h"
and got this error. Changing it to
#include "../include/head.h"
fixed it.
I solved this problem, once I removed configurationProvider node and set
cStandard and cppStandard to default values:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}",
"${workspaceFolder}/**",
"${workspaceFolder}/test",
"/opt/qt5/include/QtCore",
"${workspaceFolder}/test"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}
after you install the c/c++ extension, two files are created inside .vscode folder.
open c_cpp_properties.json file and paste this key-value pair inside configuration object.(if it doesn't already exists)
"configurationProvider": "ms-vscode-cpptools"
if it does already exists in the object, see if the value part is ms-vscode-cmaketools. if such it is, replace that existing line with above line.
this will allow you to execute your cpp files along with c files.
I solved the error on my Mac by just clicking on the Edit "include path settings" and changing the compiler path to /usr/bin/clang.
For Windows:
Please add this directory to your environment variable(Path):
C:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\
For Include errors detected, mention the path of your include folder into
"includePath": [
"C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/include/" ]
, as this is the path from where the compiler fetches the library to be included in your program.
In case you've copied and pasted code into your new file in VS Code
Please delete #include <iostream> and try to build again.

How can I use environment variable as compile-time constant in native nodejs addon?

I am writing a native addon for NodeJS. How can I use an environment variable as a constant at compile time? That is, "inject" a constant in to the NodeJS C++ addon from an environment variable set during node-gyp build or npm install. I found this answer, however as far as I can see, there is no equivalent option for passing through variables to node-gyp
I found that the defines block and variable expansion in binding.gyp will achieve what I'm after:
{
"targets": [
{
"target_name": "targetName",
"sources": [ "source.cc" ],
"defines": [
'MY_DEFINE="<!(echo $MY_ENV_VAR)"'
]
}
]
}
Then MY_DEFINE is available with value equal to whatever MY_ENV_VAR set set to at compile time.
Normally when you create a Makefile yourself you can pass options to the compiler like:
-D name=definition
which is equivalent of having this in the source code:
#define name "definition"
so using:
-D NAME=$NAME
would put the NAME environment variable as a NAME constant in the compiled source code.
But with node-gyp the Makefile is generated for you, see:
https://nodejs.org/api/addons.html
You may need to change the generated Makefile after you run:
node-gyp configure
but before you run:
node-gyp build
or you can make a simple library which entire purpose would be to have a given value defined that would be used by your Node addon.
Another option would be to have a script that does something like:
echo "#define NAME \"$NAME\"" > config.h
and you can then include the config.h file by your Node native addon or any other code written in C or C++.