Skip to content

Instantly share code, notes, and snippets.

@kwk

kwk/CMakeLists.txt

Created Sep 2, 2012
Embed
What would you like to do?
Fix for "undefined reference to dlopen" in CMake projects

Suppose you get this error with the main.cpp file below:

 Linking CXX executable testlink
 CMakeFiles/testlink.dir/main.cpp.o: In function `main':
 main.cpp:(.text+0x1a): undefined reference to `dlopen'
 main.cpp:(.text+0x2a): undefined reference to `dlclose'
 collect2: ld returned 1 exit status
 make[2]: *** [testlink] Error 1
 make[1]: *** [CMakeFiles/testlink.dir/all] Error 2
 make: *** [all] Error 2

All you need to do is add ${CMAKE_DL_LIBS} to the target_link_libraries() call:

 target_link_libraries(testlink ${CMAKE_DL_LIBS})
project(testlink)
add_executable(testlink main.cpp)
target_link_libraries(testlink)
#include <dlfcn.h>
int main(int argc, char* argv[]) {
void *handle = dlopen("/usr/lib/libmagic.so.1", 0);
dlclose(handle);
return 0;
}
@paulsapps

This comment has been minimized.

Copy link

@paulsapps paulsapps commented Mar 26, 2017

${CMAKE_DL_LIBS} is old but gold!

@kishorekanala

This comment has been minimized.

Copy link

@kishorekanala kishorekanala commented Apr 12, 2017

I faced same problem while building cmake. This problem was during linking of cmcurl.
I included /usr/lib/x86_64-linux-gnu/libdl.so in Utilities/cmcurl/CMakeFiles/LIBCURL.dir/link.txt
gcc picked this up while linking and cmake is built

@tungduonghgg123

This comment has been minimized.

Copy link

@tungduonghgg123 tungduonghgg123 commented Sep 17, 2018

You saved my day!

@mlevkov

This comment has been minimized.

Copy link

@mlevkov mlevkov commented Mar 5, 2019

You saved my day!!!!

@liuli9203

This comment has been minimized.

Copy link

@liuli9203 liuli9203 commented Mar 7, 2019

Take my knee. Thanks, Buddy

@Fegoist

This comment has been minimized.

Copy link

@Fegoist Fegoist commented Mar 12, 2019

Definitely saved my day! THX.

@jking777

This comment has been minimized.

Copy link

@jking777 jking777 commented Apr 26, 2019

Excellent.

@dktapps

This comment has been minimized.

Copy link

@dktapps dktapps commented May 3, 2019

Thank you sir. 💯

@marisancans

This comment has been minimized.

Copy link

@marisancans marisancans commented Jul 2, 2019

Saved my day!

@rqds12

This comment has been minimized.

Copy link

@rqds12 rqds12 commented Jul 6, 2019

save the day again

@emabello42

This comment has been minimized.

Copy link

@emabello42 emabello42 commented Aug 5, 2019

Thank you!! You saved my day too :)

@kwk

This comment has been minimized.

Copy link
Owner Author

@kwk kwk commented Aug 8, 2019

Haha, I'm glad this snippet has helped so many people already. I mean, it is seven years old already :)

@gyzdmgqy

This comment has been minimized.

Copy link

@gyzdmgqy gyzdmgqy commented Sep 18, 2019

Thank you so much this really worked for me.

@ruipacheco

This comment has been minimized.

Copy link

@ruipacheco ruipacheco commented Sep 19, 2019

Take my knee. Thanks, Buddy

what?

@kamranrad1993

This comment has been minimized.

Copy link

@kamranrad1993 kamranrad1993 commented Dec 4, 2020

great.
its a shortcut for :
find_library(dl NAMES dl PATH /usr/lib/x86_64-linux-gnu/libdl.so)
target_link_libraries(${PROJECT_NAME} ${dl})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment