我在Code :: Blocks上测试LibConfuse,试图在tutorial上构建最简单的第一个示例:

#include <stdio.h>
#include <confuse.h>

int main(void)
{
    cfg_opt_t opts[] =
    {
        CFG_STR("target", "World", CFGF_NONE),
        CFG_END()
    };
    cfg_t *cfg;

    cfg = cfg_init(opts, CFGF_NONE);
    if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR)
        return 1;

    printf("Hello, %s!\n", cfg_getstr(cfg, "target"));

    cfg_free(cfg);
    return 0;
}


但是编译时出现以下错误:

Undefined reference to "cfg_init".
Undefined reference to "cfg_parse".
Undefined reference to "cfg_getstr".
Undefined reference to "cfg_free".


请注意,关于#include <confuse.h>行也没有关于常量的错误,例如CFGF_NONE

我安装了LibConfuse与:

# sudo apt-get update
# sudo apt-get install libconfuse*


这是怎么回事,我该如何解决?

更多数据:


操作系统:Ubuntu 16.04。
在两台不同的PC计算机上进行了测试。
在运行RaspBian的RaspBerry Pi上测试。
代码:: Blocks版本17.12。
在Clang上测试了编译,结果相同。


另一个细节:我曾经交叉编译到FreeBSD目标,所以我用通常的过程测试了这个基本程序,以便为FreeBSD x64生成二进制文件:

$ clang -target x86_64-unknown-freebsd10.0 --sysroot=/opt/cross-freebsd-10/ main.c
/tmp/main-0044e3.o: En la función `main':
main.c:(.text+0x96): referencia a `cfg_init' sin definir
main.c:(.text+0xb3): referencia a `cfg_parse' sin definir
main.c:(.text+0xde): referencia a `cfg_getstr' sin definir
main.c:(.text+0x104): referencia a `cfg_free' sin definir
clang: error: linker command failed with exit code 1 (use -v to see invocation)


可以看出,结果是相同的。

使用clang时有关编译(到x64 Linux)的更多详细信息:

$ clang main.c -v   clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8.5
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.0.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/usr/lib/llvm-3.8/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib/llvm-3.8/bin/../lib/clang/3.8.0 -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /media/Almacen01/Temporal/CPlusPlus/PruebaGeneral -ferror-limit 19 -fmessage-length 83 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/main-06952f.o -x c main.c
clang -cc1 version 3.8.0 based upon LLVM 3.8.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
 "/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../.. -L/usr/lib/llvm-3.8/bin/../lib -L/lib -L/usr/lib /tmp/main-06952f.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crtn.o
/tmp/main-06952f.o: En la función `main':
main.c:(.text+0x96): referencia a `cfg_init' sin definir
main.c:(.text+0xb3): referencia a `cfg_parse' sin definir
main.c:(.text+0xde): referencia a `cfg_getstr' sin definir
main.c:(.text+0x104): referencia a `cfg_free' sin definir
clang: error: linker command failed with exit code 1 (use -v to see invocation)


更新:

我已经测试了LibConfig(只要它应该实现等效的功能):

sudo apt-get install libconfig-dev


并且,当尝试编译包含的/examples/example1.c文件时,我得到了等效的结果:LibConfig函数上有多个未定义的引用。

最佳答案

您需要告诉链接器在哪里可以找到库。
检查https://c-for-dummies.com/blog/?p=2159

关于c - C:对LibConfuse内部函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55677756/

10-09 02:14