Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Gcc-未定义引用,但库包含匹配符号_C++_Linux_G++ - Fatal编程技术网

C++ Gcc-未定义引用,但库包含匹配符号

C++ Gcc-未定义引用,但库包含匹配符号,c++,linux,g++,C++,Linux,G++,我尝试将我的一个程序与开源库链接起来。在automake遇到一些麻烦后,我终于设法编译了它 但是现在我从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 在我看来,gcc编译

我尝试将我的一个程序与开源库链接起来。在automake遇到一些麻烦后,我终于设法编译了它

但是现在我从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
在我看来,gcc编译我的项目的论点是正确的:

g++ -v -I ../libmodbus/modbus -o main main.cpp -L../libmodbus/modbus/.libs/ -lmodbus
我用
nm
检查了函数是否真的在该库中,但对我来说一切都很好(我已经从输出中删除了一些部分,以防止输出变得太大):


有人知道为什么gcc不能解析符号吗?

正如noloader所建议的,我错过了添加外部“C”,因为libmodbus是一个C库。我编辑了我的资料来源:

extern "C" {
    #include <modbus.h>
    #include <modbus-udp.h>
}
extern“C”{
#包括
#包括
}

现在它编译得很好。谢谢大家!

>代码> MOBUSSUDUPIIN <代码>声明为“代码>外部”C“\”/>代码(或是<代码> LBMODBUS C++库)?您没有显示足够的信息让我确定,但我怀疑您已经在书中犯了一个最老的错误:<代码> -LMOBUS < /Cord>选项需要去处理所有的对象文件。(所有其他选项都可以保留在原来的位置。)这是因为
-l
选项意味着“在命令行上从左到右搜索此库中所有未解析的符号”。我认为@noloader在诊断缺少的
外部“C”
时也可能是正确的。(这可能是库中的一个bug,但是可以通过在
extern C{
..
}
中包装一组适当的
#include
}在代码中解决)@noloader:你是对的,缺少了` extern“C'。。。非常感谢。
extern "C" {
    #include <modbus.h>
    #include <modbus-udp.h>
}