nodejs c++ addon -- unable to node-gyp build using nan module - node.js

I am learning how to build C/C++ addons for nodejs.
I could successfully node-gyp configure, node-gyp build and run node index.js on a simple hellow world program. So, my basic setup is working. The below code (copy-paste from official nodejs documentation) is the working version of my C++ code.
//hello.cc
//#include <node.h>
//#include <nan.h>
#include "./node_modules/nan/nan.h"
#include <iostream>
using namespace v8;
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void Method (const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
std::cout << "Executing some stupid func..." << std::endl;
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(addon, init)
} // namespace demo
However, when I use Nan module, and the version of code documented in the nan site, I get compilation errors indicating NanScope is not declared in this scope.
//hello.cc
#include "./node_modules/nan/nan.h"
#include <iostream>
using namespace v8;
NAN_METHOD(Method) {
Nan::NanScope();
NanReturnValue(String::New("world"));
}
void init(Handle<Object> exports) {
exports->Set(NanSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
Part of the error output...
make: Entering directory '/home/rvnath/projects/comviva/node-addons/hello-world/build'
CXX(target) Release/obj.target/addon/hello1.o
../hello1.cc: In function ‘Nan::NAN_METHOD_RETURN_TYPE Method(Nan::NAN_METHOD_ARGS_TYPE)’:
../hello1.cc:7:14: error: ‘NanScope’ was not declared in this scope
NanScope();
^
After a bit of googling around, a few sites pointed out that we should be using Nan::Scope, and that the Nan documentation was out-of-date. I tried the change but it still didn't work. It gives an error saying "Scope is not a member of Nan".
I am unable to find, how to use the Nan version correctly. Any help here will be highly appreciated.

Related

catch.hpp error messages with visual studio

Using visual studio 2015 to compile a test code using catch.hpp unit test. I need to write code to interface with serial port and will need to interface with Widnows API and need to include windows.h
But compiler generates complains with error messages below.
Severity Code Description Project File Line Suppression State
Error C2888 'Catch::Colour::Colour(Catch::Colour::Code)': symbol cannot be defined within namespace 'Catch' NMCR_Testing c:\users\ahajmousa\google drive\cto projects\new mc receiver\software\testing\nmcr_testing\nmcr_testing\catch.hpp 7796
Error C2888 'Catch::Colour::Colour(const Catch::Colour &)': symbol cannot be defined within namespace 'Catch' NMCR_Testing c:\users\ahajmousa\google drive\cto projects\new mc ....
......
code:
#include "stdafx.h"
#include <windows.h>
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("first Test")
{
REQUIRE(1 == 1);
}
TEST_CASE("2nd Test")
{
REQUIRE(1 == 0);
}
Errors will go away if I do not include widnows.h
Is there a way to get catch.hpp to compile without these errors.

Linux alternative to _NSGetExecutablePath?

Is it possible to side-step _NSGetExecutablePath on Ubuntu Linux in place of a non-Apple specific approach?
I am trying to compile the following code on Ubuntu: https://github.com/Bohdan-Khomtchouk/HeatmapGenerator/blob/master/HeatmapGenerator2_Macintosh_OSX.cxx
As per this prior question that I asked: fatal error: mach-o/dyld.h: No such file or directory, I decided to comment out line 52 and am wondering if there is a general cross-platform (non-Apple specific) way that I can rewrite the code block of line 567 (the _NSGetExecutablePath block) in a manner that is non-Apple specific.
Alen Stojanov's answer to Programmatically retrieving the absolute path of an OS X command-line app and also How do you determine the full path of the currently running executable in go? gave me some ideas on where to start but I want to make certain that I am on the right track here before I go about doing this.
Is there a way to modify _NSGetExecutablePath to be compatible with Ubuntu Linux?
Currently, I am experiencing the following compiler error:
HeatmapGenerator_Macintosh_OSX.cxx:568:13: error: use of undeclared identifier
'_NSGetExecutablePath'
if (_NSGetExecutablePath(path, &size) == 0)
Basic idea how to do it in a way that should be portable across POSIX systems:
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
static char *path;
const char *appPath(void)
{
return path;
}
static void cleanup()
{
free(path);
}
int main(int argc, char **argv)
{
path = realpath(argv[0], 0);
if (!path)
{
perror("realpath");
return 1;
}
atexit(&cleanup);
printf("App path: %s\n", appPath());
return 0;
}
You can define an own module for it, just pass it argv[0] and export the appPath() function from a header.
edit: replaced exported variable by accessor method

GoogleTest CMake doesn't recognize TEST_F: Like it's not recognizing GTest something

OK, I admit it, this is a unique case. When we build our application we are using make, so I've included my tests in a test folder under src. Then at the same level as our release folder we have created a unit-test folder that includes all of our source files and our test source files.
But my IDE is CLion, which uses CMake. In my CMakeLists.txt file, I have included:
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(TestProject ${SOURCE_FILES})
target_link_libraries(TestProject ${GTEST_BOTH_LIBRARIES})
I am creating my first Test Fixture. Here is the code:
#include "OPProperties.h"
#include "gtest/gtest.h"
namespace {
// The fixture for testing class OPPropertiesTestTest.
class OPPropertiesTestTest : public ::testing::Test {
protected:
// You can remove any or all of the following functions if its body
// is empty.
OPPropertiesTestTest() {
// You can do set-up work for each test here.
}
virtual ~OPPropertiesTestTest() {
// You can do clean-up work that doesn't throw exceptions here.
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
virtual void SetUp() {
// Code here will be called immediately after the constructor (right
// before each test).
}
virtual void TearDown() {
// Code here will be called immediately after each test (right
// before the destructor).
}
// Objects declared here can be used by all tests in the test case for OPPropertiesTestTest.
};
TEST_F(OPPropertiesTestTest, ThisTestWillFail) {
EXPECT_EQ(0, 2);
}
} // namespace
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Here is an image capture:
Notice the syntax checker errors in my TEST_F function. When I started to type TEST_F code completion is trying to find a Boost Test Function.
Can someone tell me what else I need to add to the CMakeLists.txt file or what I am not doing that GTest functions are not being recognized?
As πάντα ῥεῖ pointed out, I hadn't actually tried to build the code. When I did I first received a linker error for pthread, so we added the following line the the CMakeLists.txt file:
target_link_libraries(OnePrint pthread)
Then I tried again to build and received these errors:
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
So, I ran a search on these errors and found this question.
The answer that worked for me was here.

Error : Expected a declaration

I am writing a code in Visual C++ to access serial port.
Code is given below:-
#include<stdio.h>
#include<cstring>
#include<string.h>
#include<conio.h>
#include<iostream>
using namespace std;
//#include "stdafx.h"
#ifndef __CAPSTONE_CROSS_SERIAL_PORT__
#define __CAPSTONE_CROSS_SERIAL_PORT__
HANDLE hSerial= CreateFile(L"COM1", GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//serial port does not exist. Inform user.
}
//some other error occurred. Inform user.
}
In the above code I am getting error at if in line
if(hserial==INVALID_HANDLE_VALUE)
Error is given below:-
Error:expected a declaration
I am getting same error at both braces } at the end of if statement
I want to know why I am getting this error and how to resolve it
I think you may want to read this. The problem is you are trying to use an if statement at namespace scope (global namespace) where only a declaration is valid.
You will need to wrap your logic in a function of some kind.
void mySuperCoolFunction()
{
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
//serial port does not exist. Inform user.
}
//some other error occurred. Inform user.
}
}

Native extension .node in Windows to NodeJS

I built a .node extension in Windows using the following code (addon.node):
#include "v8.h"
#include "node.h"
extern "C" void NODE_EXTERN init (Handle<Object> target)
{
HandleScope scope;
target->Set(String::New("hello"), String::New("world"));
}
NODE_MODULE(hello, init)
I compiled in VS10 and my simple code in main.js is:
var addon = require("./addon.node");
console.log(addon.hello());
It should be print the word "world", but I have the following error:
#
# Fatal error in d:\nodejs\deps\v8\src\objects-inl.h, line 3199
# CHECK(heap->isolate() == Isolate::Current()) failed
#
Anyone knows the solution?
Thanks in advance!
SOLVED: My problem was that I included the library to SSL support. I remove it and done!

Resources