簡體   English   中英

具有C共享庫的ffi Node.js中未定義的符號-libmodbus

[英]undefined Symbols in ffi Node.js with a C shared library - libmodbus

我的計划:使用libmodbus在C中創建一個共享庫。 使用node.js(ffi)訪問此庫。

這是C庫:

#include <modbus.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

extern int read(void);

int main(void)
{
        int i=0;
        printf("starte\n");
        i = read();
        return i;
}

extern int read(void)
{
        modbus_t *ctx;
        uint16_t tab_reg[32];
        int anzahl = 1;
        int rc;

        ctx = modbus_new_tcp("10.69.69.103",502);
        modbus_set_slave(ctx, 180);

        if(modbus_connect(ctx) == -1)
        {
                modbus_free(ctx);
                return -1;
        }

        rc = modbus_read_input_registers(ctx, 100, anzahl, tab_reg);

        if (rc == -1)
        {
                modbus_free(ctx);
                return -1;
        }

        printf("Reg: %d\n", tab_reg[0]);
        return tab_reg[0];
}

如果我編譯並運行它,它將起作用。

然后,我從中創建一個共享庫:

gcc -c -Wall -Werror -fPIC modtest.c -I/usr/local/include/modbus/ -L/usr/lib/ -lmodbus

在那里,我得到了對象文件並創建了共享對象

gcc -shared -o libmodtest.so modtest.o

獲得.so文件后,將其復制到usr / lib /並賦予其權限(chmod 0755)並使用ldconfig加載它。

現在,我想使用Node.js和ffi模塊訪問此庫:

var ffi = require('ffi');

var libmod = ffi.Library('libmodtest', {
        'read': ['int', [ 'void'] ]

});


var cb = libmod.read();
console.log(cb);

但是,如果我運行它,則會出現錯誤:

/home/frala/tmp/node_modules/ffi/lib/dynamic_library.js:74
    throw new Error('Dynamic Linking Error: ' + err)
          ^
Error: Dynamic Linking Error: /usr/lib/libmodtest.so: undefined symbol: modbus_connect
    at new DynamicLibrary (/home/frala/tmp/node_modules/ffi/lib/dynamic_library.js:74:11)
    at Object.Library (/home/frala/tmp/node_modules/ffi/lib/library.js:45:12)
    at Object.<anonymous> (/home/frala/tmp/test/modtest.js:4:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

我不知道怎么了。

如果我用“ nm”搜索符號,則會得到:

000006a8 T main
         U modbus_connect
         U modbus_free
         U modbus_new_tcp
         U modbus_read_input_registers
         U modbus_set_slave
         U printf@@GLIBC_2.4
         U puts@@GLIBC_2.4
000006d0 T read
00000608 t register_tm_clones
0000904c d __TMC_END__

所以我猜你代表未定義。 如何定義它?

運行在Ubuntu 13.10上

我有一個類似的問題,可以通過在鏈接標志周圍添加-Wl,-whole-archive / no-whole-archive標簽來解決。 顯然,這可確保將整個存檔(庫)存儲在共享庫中並使其可訪問。

因此,您可能想嘗試:

gcc -c -Wall -Werror -fPIC modtest.c -I / usr / local / include / modbus / -L / usr / lib / -Wl,-整個存檔-lmodbus -Wl,-無整個存檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM