简体   繁体   English

Linux C++ 错误:未定义对“dlopen”的引用

[英]Linux c++ error: undefined reference to 'dlopen'

I work in Linux with C++ (Eclipse), and want to use a library.我使用 C++ (Eclipse) 在 Linux 中工作,并且想要使用一个库。 Eclipse shows me an error: Eclipse 显示了一个错误:

undefined reference to 'dlopen' 

Do you know a solution?你知道解决办法吗?

Here is my code:这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
    void *handle;
    double (*desk)(char*);
    char *error;

    handle = dlopen ("/lib/CEDD_LIB.so.6", RTLD_LAZY);
    if (!handle) {
        fputs (dlerror(), stderr);
        exit(1);
    }

    desk= dlsym(handle, "Apply");

    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }

    dlclose(handle);
}

You have to link against libdl, add你必须链接到 libdl,添加

-ldl -ldl

to your linker options到您的链接器选项

@Masci is correct, but in case you're using C (and the gcc compiler) take in account that this doesn't work: @Masci 是正确的,但如果您使用 C(和gcc编译器),请考虑到这不起作用:

gcc -ldl dlopentest.c

But this does:但这确实:

gcc dlopentest.c -ldl

Took me a bit to figure out...花了我一点时间来弄清楚......

this doesn't work:这不起作用:

 gcc -ldl dlopentest.c

But this does:但这确实:

 gcc dlopentest.c -ldl

That's one annoying "feature" for sure这肯定是一个令人讨厌的“功能”

I was struggling with it when writing heredoc syntax and found some interesting facts .我在编写 heredoc 语法时遇到了它,并发现了一些有趣的事实 With CC=Clang , this works:使用CC=Clang ,这有效:

$CC -ldl -x c -o app.exe - << EOF
#include <dlfcn.h>
#include <stdio.h>
int main(void)
{
  if(dlopen("libc.so.6", RTLD_LAZY | RTLD_GLOBAL))
    printf("libc.so.6 loading succeeded\n");
  else
    printf("libc.so.6 loading failed\n");
  return 0;
}
EOF

./app.exe

as well as all of these:以及所有这些:

  • $CC -ldl -xc -o app.exe - << EOF
  • $CC -xc -ldl -o app.exe - << EOF
  • $CC -xc -o app.exe -ldl - << EOF
  • $CC -xc -o app.exe - -ldl << EOF

However, with CC=gcc , only the last variant works;但是,对于CC=gcc ,只有最后一个变体有效; -ldl after - (the stdin argument symbol). -ldl- (标准输入参数符号)。

The topic is quite old, yet I struggled with the same issue today while compiling cegui 0.7.1 (openVibe prerequisite).这个话题已经很老了,但我今天在编译 cegui 0.7.1(openVibe 先决条件)时遇到了同样的问题。

What worked for me was to set: LDFLAGS="-Wl,--no-as-needed" in the Makefile.对我LDFLAGS="-Wl,--no-as-needed"是在 Makefile 中设置: LDFLAGS="-Wl,--no-as-needed"

I've also tried -ldl for LDFLAGS but to no avail.我也为LDFLAGS尝试过-ldl但无济于事。

I was using CMake to compile my project and I've found the same problem.我正在使用 CMake 编译我的项目,但我发现了同样的问题。

The solution described here works like a charm, simply add ${CMAKE_DL_LIBS} to the target_link_libraries() call这里描述的解决方案就像一个魅力,只需将 ${CMAKE_DL_LIBS} 添加到 target_link_libraries() 调用

you can try to add this你可以尝试添加这个

LIBS=-ldl CFLAGS=-fno-strict-aliasing

to the configure options到配置选项

You needed to do something like this for the makefile:您需要为 makefile 执行以下操作:

LDFLAGS='-ldl'
make install

That'll pass the linker flags from make through to the linker.这会将链接器标志从 make 传递到链接器。 Doesn't matter that the makefile was autogenerated.生成文件是自动生成的并不重要。

I met the same problem even using -ldl .即使使用-ldl我也遇到了同样的问题。

Besides this option, source files need to be placed before libraries, see undefined reference to `dlopen' .除了这个选项,源文件需要放置在库之前,参见undefined reference to `dlopen'

In order to use dl functions you need to use the -ldl flag for the linker.为了使用 dl 函数,您需要为链接器使用 -ldl 标志。

how you do it in eclipse ?你如何在日食中做到这一点?

Press Project --> Properties --> C/C++ build --> Settings --> GCC C++ Linker -->项目-->属性--> C/C++ 构建-->设置--> GCC C++ 链接器-->
Libraries --> in the "Libraries(-l)" box press the "+" sign --> write " dl " (without the quotes)-> press ok --> clean & rebuild your project.--> 在“库(-l)”框中按“+”号--> 写“ dl ”(不带引号)-> 按确定-->清理并重建您的项目。

 $gcc -o program program.c -l <library_to_resolve_program.c's_unresolved_symbols>

A good description of why the placement of -l dl matters一个很好的描述为什么 -l dl 的位置很重要

But there's also a pretty succinct explanation in the docs From $man gcc但是在 $man gcc 的文档中也有一个非常简洁的解释

 -llibrary -l library Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
       It makes a difference where in the command you write this option; the
       linker searches and processes libraries and object files in the order
       they are specified.  Thus, foo.o -lz bar.o searches library z after
       file foo.o but before bar.o.  If bar.o refers to functions in z,
       those functions may not be loaded.

Try to rebuild openssl (if you are linking with it) with flag no-threads .尝试使用标志no-threads重建openssl (如果您正在与它链接)。

Then try to link like this:然后尝试像这样链接:

target_link_libraries(${project_name} dl pthread crypt m ${CMAKE_DL_LIBS})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM