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

时间:2022-01-20 21:25:27

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

我在Linux中使用c++ (Eclipse),并希望使用一个库。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);
}

8 个解决方案

#1


184  

You have to link against libdl, add

您必须链接到libdl,添加。

-ldl

低密度脂蛋白

to your linker options

你的链接器选项

#2


41  

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

我花了一点时间才弄明白……

#3


5  

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,——Makefile中不需要的"。

I've also tried -ldl for LDFLAGS but to no avail.

我也尝试过-ldl作为LDFLAGS,但没有用。

#4


2  

You needed to do something like this for the makefile:

你需要为makefile做这样的事情:

LDFLAGS='-ldl' make install

LDFLAGS =“低密度脂蛋白”进行安装

That'll pass the linker flags from make through to the linker. Doesn't matter that the makefile was autogenerated.

这将会将链接器标记传递给链接器。这个makefile是自动生成的并不重要。

#5


2  

you can try to add this

你可以试着加上这个。

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

to the configure options

配置选项

#6


1  

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

除了这个选项,还需要在库之前放置源文件,查看未定义的“dlopen”引用。

#7


1  

In order to use dl functions you need to use the -ldl flag for the linker.

为了使用dl函数,您需要为链接器使用-ldl标志。

how you do it in eclipse ?

如何在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.

按项目——>属性——> C/ c++构建——>设置——> GCC c++ Linker——>库——>在“库(-l)”框中,按“+”号——>写“dl”(没有引号)——>按ok——> clean &重建您的项目。

#8


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

一个很好的说明为什么-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.

#1


184  

You have to link against libdl, add

您必须链接到libdl,添加。

-ldl

低密度脂蛋白

to your linker options

你的链接器选项

#2


41  

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

我花了一点时间才弄明白……

#3


5  

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,——Makefile中不需要的"。

I've also tried -ldl for LDFLAGS but to no avail.

我也尝试过-ldl作为LDFLAGS,但没有用。

#4


2  

You needed to do something like this for the makefile:

你需要为makefile做这样的事情:

LDFLAGS='-ldl' make install

LDFLAGS =“低密度脂蛋白”进行安装

That'll pass the linker flags from make through to the linker. Doesn't matter that the makefile was autogenerated.

这将会将链接器标记传递给链接器。这个makefile是自动生成的并不重要。

#5


2  

you can try to add this

你可以试着加上这个。

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

to the configure options

配置选项

#6


1  

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

除了这个选项,还需要在库之前放置源文件,查看未定义的“dlopen”引用。

#7


1  

In order to use dl functions you need to use the -ldl flag for the linker.

为了使用dl函数,您需要为链接器使用-ldl标志。

how you do it in eclipse ?

如何在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.

按项目——>属性——> C/ c++构建——>设置——> GCC c++ Linker——>库——>在“库(-l)”框中,按“+”号——>写“dl”(没有引号)——>按ok——> clean &重建您的项目。

#8


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

一个很好的说明为什么-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.