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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/956640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:24:18  来源:igfitidea点击:

Linux c++ error: undefined reference to 'dlopen'

c++linuxeclipse

提问by user101375

I work in Linux with C++ (Eclipse), and want to use a library. Eclipse shows me an error:

我使用 C++ (Eclipse) 在 Linux 中工作,并且想要使用一个库。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);
}

采纳答案by Masci

You have to link against libdl, add

你必须链接到 libdl,添加

-ldl

-ldl

to your linker options

到您的链接器选项

回答by Rob

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. Doesn't matter that the makefile was autogenerated.

这会将链接器标志从 make 传递到链接器。生成文件是自动生成的并不重要。

回答by Deqing

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'

回答by knocte

@Masci is correct, but in case you're using C (and the gcccompiler) 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...

花了我一点时间来弄清楚......

回答by bawey

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 中。

I've also tried -ldlfor LDFLAGSbut to no avail.

我也尝试过-ldlLDFLAGS但无济于事。

回答by user2948547

you can try to add this

你可以尝试添加这个

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

to the configure options

到配置选项

回答by Amitk

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-->
Libraries--> in the "Libraries(-l)" box press the "+" sign--> write "dl" (without the quotes)-> press ok--> clean & rebuildyour project.

Project--> Properties--> C/C++ build--> Settings--> GCC C++ Linker-->
Libraries--> 在“Libraries(-l)”框中按“+”号--> 写“ dl”(不带引号)-> 按确定-->清理并重建您的项目。

回答by flerb

 $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.)
   -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.

回答by vulcan raven

this doesn't work:

gcc -ldl dlopentest.c

But this does:

gcc dlopentest.c -ldl

That's one annoying "feature" for sure

这不起作用:

gcc -ldl dlopentest.c

但这确实:

gcc dlopentest.c -ldl

这肯定是一个令人讨厌的“功能”

I was struggling with it when writing heredoc syntax and found some interesting facts. With CC=Clang, this works:

我在编写 heredoc 语法时遇到了它,并发现了一些有趣的事实。使用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 -x c -o app.exe - << EOF
  • $CC -x c -ldl -o app.exe - << EOF
  • $CC -x c -o app.exe -ldl - << EOF
  • $CC -x c -o app.exe - -ldl << EOF
  • $CC -ldl -x c -o app.exe - << EOF
  • $CC -x c -ldl -o app.exe - << EOF
  • $CC -x c -o app.exe -ldl - << EOF
  • $CC -x c -o app.exe - -ldl << EOF

However, with CC=gcc, only the last variant works; -ldlafter -(the stdin argument symbol).

但是,对于CC=gcc,只有最后一个变体有效;-ldl之后-(标准输入参数符号)。

回答by Lucas Coelho

I was using CMake to compile my project and I've found the same problem.

我正在使用 CMake 编译我的项目,但我发现了同样的问题。

The solution described hereworks like a charm, simply add ${CMAKE_DL_LIBS} to the target_link_libraries() call

这里描述的解决方案就像一个魅力,只需将 ${CMAKE_DL_LIBS} 添加到 target_link_libraries() 调用