我在github上clone了一个hiredis Repo(redis的C客户端代码) 我想在node addon 上使用这段代码做一些功能(原因是我需要在addon里头用到redis), 我已经好久没写C了,大多数的内容都还给老师了,好不容易成功编译出来,但是在nodejs上就不行,因为没办法链接上一个叫libhiredis.so的动态库(事实上我是不知道如何在nodejs上配置链接)。
各位高人,可以帮助下我吗?
另外贴一下代码:
#include <node.h>
#include <v8.h>
#include "hiredis.h" //这里我可以索性写一个绝对地址
using namespace v8;
Handle<Value> ValueQuene(const Arguments& args){
HandleScope scope;
redisContent *c; //由于没有链接so,所以这段是报错的,没生命
redisReply *reply; //报错not declared
const char *hostname = args[0]->IsUndefined() ? "192.168.0.147" : args[0]->StringValue() ;
const int port = args[1]->IsUndefined() ? 6379 : args[1]->NumberValue();
const struct timeval timeout = {1,500000} ;
c = redisConnectionWithTimeout(hostname,port,timeout); //报错not declared
if( c == NULL || c->err){
if(c){
printf("Connection Error %s \n",c->errstr);
redisFree(c);//报错not declared
}else{
printf("UnKnow Error \n");
}
return scope.close(Error::New("Error!!"));
}
reply = redisCommand(c,"PING");//报错not declared
char* pong = reply->str;
freeReplyObject(reply);//报错not declared
redisFree(c);//报错not declared
return scope.Close(String::NewSymbol(pong));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("ValueQuene"),
FunctionTemplate::New(ValueQuene)->GetFunction());
}
NODE_MODULE(ValueQuene, init)
HINSTANCE hDLL; hDLL=LoadLibrary(“dlldemo.dll”);//加载动态链接库dlldemo.dll文件; typedef int(*pMax)(int a,int b);//函数指针 pMax Max=NULL; Max=(pMax)GetProcAddress(hDLL,"Max"); 这是windows上VC++的代码,http://cnodejs.org/topic/51c2ba5c73c638f3703e4185
你好,能否在我的代码基础上进行修改呢? 在linux下gcc编译一个文件是使用gcc xxx.c -I /home/xxx/github/hiredis /home/xxx/github/hiredis/libhiredis.so -o xxx 但在node-gyp里头我就不会配置了,一直报问题,但是我就不会修改,能否帮我修改下?
@forfuns 你的gyp还是照常写,之后会编译出一个.o文件,把这个和你的静态库一起编译成.node
gcc -fexceptions -O2 -o hsfProtocol.node ./build/Release/obj.target/hsfProtocol/hsf_protocol.o \
$HSFPROTOCOL_HOME/libhsf.a -shared $EXTRA_FLAG
@dead-horse 我按照你上面的提示操作,但是出现这堆错误:
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o:在函数‘_start’中:
(.text+0x20):对‘main’未定义的引用
./build/Release/obj.target/ValueQuene/ValueQuene.o:在函数‘ValueQuene(v8::Arguments const&)’中:
ValueQuene.cpp:(.text+0xf):对‘v8::HandleScope::HandleScope()’未定义的引用
ValueQuene.cpp:(.text+0x1b):对‘command(char*)’未定义的引用
ValueQuene.cpp:(.text+0x28):对‘v8::String::NewSymbol(char const*, int)’未定义的引用
ValueQuene.cpp:(.text+0x33):对‘v8::HandleScope::RawClose(v8::internal::Object**)’未定义的引用
ValueQuene.cpp:(.text+0x40):对‘v8::HandleScope::~HandleScope()’未定义的引用
ValueQuene.cpp:(.text+0x57):对‘v8::HandleScope::~HandleScope()’未定义的引用
./build/Release/obj.target/ValueQuene/ValueQuene.o:在函数‘init(v8::Handle<v8::Object>)’中:
ValueQuene.cpp:(.text+0x85):对‘v8::FunctionTemplate::New(v8::Handle<v8::Value> (*)(v8::Arguments const&), v8::Handle<v8::Value>, v8::Handle<v8::Signature>)’未定义的引用
ValueQuene.cpp:(.text+0x8d):对‘v8::FunctionTemplate::GetFunction()’未定义的引用
ValueQuene.cpp:(.text+0xa1):对‘v8::String::NewSymbol(char const*, int)’未定义的引用
ValueQuene.cpp:(.text+0xb1):对‘v8::Object::Set(v8::Handle<v8::Value>, v8::Handle<v8::Value>, v8::PropertyAttribute)’未定义的引用