i have tried to link a programm of mine with the libmodbus opensource lib. After some trouble with automake, i finally managed to compile it.
我试图将我的程序与libmodbus opensource lib链接起来。在使用automake遇到麻烦之后,我终于成功编译了它。
But now I get a undefined reference error from gcc, when I use one of the functions the library provides in my own programm:
但是现在我从gcc中得到一个未定义的引用错误,当我使用库在我自己的程序中提供的一个函数时:
main.cpp:(.text+0x21): undefined reference to `modbus_udp_init(char*, int, __modbus_udp_handle*)'
collect2: Fehler: ld gab 1 als Ende-Status zurück
From my point of view, the arguments of gcc to compile my project are correct:
从我的角度来看,gcc编译我的项目的论据是正确的:
g++ -v -I ../libmodbus/modbus -o main main.cpp -L../libmodbus/modbus/.libs/ -lmodbus
I checked with nm
if the function is really in that library, but everything looks fine for me (I have deleted some parts from the output, to prevent the output from getting too big):
我检查了nm,如果函数真的在那个库中,但一切看起来都不错(我从输出中删除了一些部分,以防止输出变得太大):
$ nm ../libmodbus/modbus/.libs/libmodbus.a
modbus.o:
0000000000000590 T crc16
U free
U malloc
U memcpy
0000000000000b40 T modbus_diagnostics
0000000000000d90 T modbus_error_get_exception_code
0000000000000100 C modbus_error_str
0000000000000cf0 T modbus_exception
[...]
modbus-udp.o:
U bcopy
U close
U __errno_location
U gethostbyname
U modbus_error_str
U modbus_tcp_frame_pack
U modbus_tcp_frame_parse
0000000000000000 T modbus_udp_close
0000000000000030 T modbus_udp_init
0000000000000220 T modbus_udp_recv
0000000000000190 T modbus_udp_send
0000000000000010 r __PRETTY_FUNCTION__.4582
0000000000000000 r __PRETTY_FUNCTION__.4592
U recvfrom
U sendto
U setsockopt
U snprintf
U socket
U strerror
modbus-tcp.o:
[...]
modbus-serial.o:
[...]
Does someone have an idea why gcc can't resolve the symbol?
有人知道为什么gcc无法解析符号吗?
1 个解决方案
#1
3
As noloader suggested, i missed adding extern "C"
as libmodbus is a C-library. I edited my source:
正如noloader建议的那样,我错过了添加extern“C”,因为libmodbus是一个C库。我编辑了我的来源:
extern "C" {
#include <modbus.h>
#include <modbus-udp.h>
}
Now it compiles fine. Thank you!
现在编译好了。谢谢!
#1
3
As noloader suggested, i missed adding extern "C"
as libmodbus is a C-library. I edited my source:
正如noloader建议的那样,我错过了添加extern“C”,因为libmodbus是一个C库。我编辑了我的来源:
extern "C" {
#include <modbus.h>
#include <modbus-udp.h>
}
Now it compiles fine. Thank you!
现在编译好了。谢谢!