2009-06-05 21 views
91

我在C++(Eclipse)的Linux工作,并希望使用库。 Eclipse给我显示了一个错误:Linux的c + +错误:未定义引用'dlopen'

undefined reference to 'dlopen' 

你知道一个解决方案吗?

这里是我的代码:

#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); 
} 

回答

181

你必须对libdl链接,添加

-ldl

你的链接选项

+2

我遇到了同样的问题,在文档 从$男人GCC一个相当简洁的解释... 我在Project> Properties> C/C++ Build> Settings>(My Linker)下添加了编译器标志, >链接器标志文本字段中的其他内容。它什么都没做。 – MirroredFate 2013-02-02 07:09:49

+3

哈,好吧,对于任何有此问题的人,请使用上面的路径,除了去图书馆而不是杂项,并添加'dl' – MirroredFate 2013-02-02 07:53:42

+1

这个答案有帮助。对于任何想要查找libdl.so位置的人,只需进入根目录并键入'locate libdl.so' – Nav 2013-07-04 11:46:22

2

您需要的makefile文件做这样的事情:

LDFLAGS =' - ldl' make install

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

40

@Masci是正确的,但如果你正在使用C(和gcc编译器)采取帐户,这不起作用:

但这:

gcc dlopentest.c -ldl 

带我了解一点...

+4

这是一个令人讨厌的“功能”肯定... – User1291 2016-05-11 19:31:49

+1

我发现选项的顺序也很重要。在使用sqlite3的项目中,我必须在-lsqlite3之后放置-ldl(和-lpthread)。不知道那是什么,如果我只是RTFM,我相信答案就在那里。 – 2017-04-04 18:55:35

+0

神圣的废话,就是这样!我绝对不会猜到,将选项放在第一位(这对我来说更有意义)不起作用,而把它们放在后面。谢谢@knocte! – 2017-07-21 18:18:30

5

该主题是相当古老的,但我今天在编译cegui 0.7.1(openVibe先决条件)的同时遇到了同样的问题。

对我来说有效的是在Makefile中设置:LDFLAGS="-Wl,--no-as-needed"

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

2

,你可以尝试添加该

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

的配置选项

+0

使用LIBS变量为我工作得到配置将-ldl放在命令行上的正确位置。 – duncan 2015-01-14 05:38:45

1

为了使用您需要使用-ldl标志链接DL功能。

如何在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 & rebuild your project.

1
$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

但也有

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