Dlopen Linker Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business dlopen error codes Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Undefined Reference To Dlopen Linux
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just undefined reference to dlopen ubuntu like you, helping each other. Join them; it only takes a minute: Sign up undefined reference to `dlopen' up vote 1 down vote favorite I have a program that does not build with modern GCC with the
Undefined Reference To Dlopen Gcc
foollowing output: gcc -I/usr/lib/qt3/include -I/opt/kde3/include/ -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -lqt-mt -ldl -L/usr/lib/qt3/lib64 -o autocheck autocheck.cpp autocheck.cpp: In function 'int main(int, char**)': autocheck.cpp:64:62: warning: too many arguments for format autocheck.cpp:79:79: warning: too many arguments for format /tmp/ccOFReGf.o: In function `main': autocheck.cpp:(.text+0x244): undefined reference to `dlopen' autocheck.cpp:(.text+0x2e1): undefined reference to `dlerror' collect2: ld returned 1 exit status I searched the Internet for advise but only found a recommendation to add -ldl to the linker. dlopen linux example But this does not help here. What should I do? linux gcc linker share|improve this question edited May 31 '11 at 6:45 skaffman 277k63616654 asked May 31 '11 at 6:44 Anixx 114117 This is weird. Please try to reproduce the problem on a minimal example, i.e. a small program that only calls dlopen and does nothing else. –n.m. May 31 '11 at 7:02 Any information about what -ldl does ? –fedi Aug 1 at 22:23 add a comment| 1 Answer 1 active oldest votes up vote 10 down vote accepted Move autocheck.cpp so that it is before the libraries in your command. Libraries are only searched for things that need resolving in files that appear before them. So your command should look like this: gcc autocheck.cpp -I/usr/lib/qt3/include -I/opt/kde3/include/ -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -lqt-mt -ldl -L/usr/lib/qt3/lib64 -o autocheck share|improve this answer answered May 31 '11 at 6:48 nbt 18.6k33261 How to tell it to configure? –Anixx May 31 '11 at 6:58 @neil-butterworth In my experience, this is only true about static libraries. On my machine g++ -ldl -o foo foo.cpp works (foo.cpp does reference dlopen). –n.m. May 31 '11 at 6:58 How to tell what to configure? –nbt May 31 '11 at 6:59 @n.m I don't know whe
NAME top dlclose, dlopen, dlmopen - open and close a shared object SYNOPSIS top #include
Cmake Undefined Reference To `dlopen'
void *dlmopen (Lmid_t lmid, const char *filename, int flags); Link with -ldl. DESCRIPTION
Undefined Reference To Dlopen C
top dlopen() The function dlopen() loads the dynamic shared object (shared library) file named by the null-terminated string filename and undefined reference to __dlopen returns an opaque "handle" for the loaded object. This handle is employed with other functions in the dlopen API, such as dlsym(3), dladdr(3), dlinfo(3), and dlclose(). If filename is NULL, then the returned handle is for http://stackoverflow.com/questions/6183899/undefined-reference-to-dlopen the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the object as follows (see ld.so(8) for further details): o (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched. o If, at the time that the program http://man7.org/linux/man-pages/man3/dlopen.3.html was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of directories, then these are searched. (As a security measure, this variable is ignored for set-user-ID and set-group- ID programs.) o (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched. o The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename. o The directories /lib and /usr/lib are searched (in that order). If the object specified by filename has dependencies on other shared objects, then these are also automatically loaded by the dynamic linker using the same rules. (This process may occur recursively, if those objects in turn have dependencies, and so on.) One of the following two values must be included in flags: RTLD_LAZY Perform lazy binding. Only resolve symbols as the code that references them is executed. If the symbol is never referenced, then it is never resolved. (Lazy binding is performed only for function references; references to variables are always immediately bound when the shared object is loaded.) Since glibc 2.1.1, this flag is overridden by the effect of the LD_BIND_NOW environment variable. RTLD_NOW If this value is specified, or the environment variable LD_BIND_NOW is set to a nonempty string,
a shared library. In the host app, there are some symbols which should be used by http://www.cplusplus.com/forum/unices/25570/ the shared library. However, none of other parts of the host https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/chapter3-27/index.html app uses those symbols, so I need to prevent those symbols from stripping. // Host app // main.cpp extern "C" { #include "jpeglib.h" } void PreventLibjpegAPIsFromStripping(bool flag); void InitApp(); int main() { InitApp(); // ... void* handle = dlopen("test.so", RTLD_NOW); typedef void (*DrawJpegPtr)(const char* path); DrawJpegPtr drawJpeg undefined reference = (DrawJpegPtr)dlsym(handle, "DrawJpeg"); drawJpeg("/usr/test.jpg"); dlclose(handle); } void InitApp() { // To keep the libjpeg symbols. PreventLibjpegAPIsFromStripping(false); // initialization code block } void PreventLibjpegAPIsFromStripping(bool flag) { struct jpeg_compress_struct cinfo; struct jpeg_decompress_struct dcinfo; struct jpeg_error_mgr jerr; if(accept) { jpeg_std_error(&jerr); jpeg_stdio_src(&dcinfo, 0); jpeg_create_compress(&cinfo); jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, 0, 0); jpeg_stdio_dest(&cinfo, 0); jpeg_start_compress(&cinfo, 0); jpeg_destroy_compress(&cinfo); jpeg_write_scanlines(&cinfo, 0, 1); jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); } } // the undefined reference to shared library // test.cpp extern "C" { #include "jpeglib.h" void DrawJpeg(const char* path) { // some code which is drawing a jpeg with libjpeg. // .. } } And I compiled the shared library with below shell commands. $> g++ -c -fPIC test.cpp $> g++ -shared -o test.so test.o $> g++ main.cpp -I./libjpeg -ldl -ljpeg -L./libjpeg /////////////////////////// When I ran this test app, I got a undefined symbol runtime error. This error was "undefined symbol jpeg_write_scanlines". It didn't make sense at all. I checked if there was a symbol, "jpeg_write_scanlines" in the test app. There was "jpeg_write_scanlines" symbol. I really don't know what I'm missing. Any advice about it? Thanks in advance. Jun 30, 2010 at 1:48pm UTC Athar (4466) Make sure that the host application is linked with the flag -rdynamic. Jul 1, 2010 at 1:33am UTC erunc0 (2) I resolved this problem with linker option. There are both ways, one is to use -rdynamic(--export-dynamic). However, this way exposes all the symbols in the host app. So it'll increase its tota
Termination RoutinesProviding an Alternative to dlopen() Lazy loading can provide an alternative to dlopen(3C) and dlsym(3C) use. See Runtime Linking Programming Interface. For example, the following code from libfoo.so.1 verifies an object is loaded, and then calls interfaces provided by that object. void foo() { void *handle; if ((handle = dlopen("libbar.so.1", RTLD_LAZY)) != NULL) { int (*fptr)(); if ((fptr = (int (*)())dlsym(handle, "bar1")) != NULL) (*fptr)(arg1); if ((fptr = (int (*)())dlsym(handle, "bar2")) != NULL) (*fptr)(arg2); .... } Although very flexible, this model of using dlopen() and dlsym() is an unnatural coding style, and has some drawbacks. The object in which the symbols are expected to exit must be known. The calls through function pointers provide no means of verification by either the compiler, or lint(1). This code can be simplified if the object that supplies the required interfaces satisfies the following conditions. The object can be established as a dependency at link-edit time. The object is always available. By exploiting that a function reference can trigger lazy loading, the same deferred loading of libbar.so.1 can be achieved. In this case, the reference to the function bar1() results in lazy loading the associated dependency. This coding is far more natural, and the use of standard function calls provides for compiler, or lint(1) validation. void foo() { bar1(arg1); bar2(arg2); .... } $ cc -G -o libfoo.so.1 foo.c -L. -zdefs -zlazyload -lbar -R'$ORIGIN' However, this model fails if the object that provides the required interfaces is not always available. In this case, the ability to test for the existence of the dependency, without having to know the dependency name, is desirable. A means of testing for the availability of a dependency that satisfies a function reference is required. A robust model for testing for the exi