0

Error:

 Linking CXX executable cmakeproject
 CMakeFiles/cmakeproject.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/cmakeproject.dir/all] Error 2
 make: *** [all] Error 2

I ask how to fix this in CMake Project because there are multiple questions like this and answers but CMake is not used by them:

How to fix this if I use CMake?

FreePhoenix888
  • 4,510
  • 3
  • 13
  • 22
  • https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Jesper Juhl Nov 22 '22 at 13:12
  • @JesperJuhl I have seen that answer when I was trying to fix this error. But that answer is too much generay to help me understand how to fix `undefined reference to dlopen`. My answer gives an instruction and helps to understand how I have found the solution – FreePhoenix888 Nov 22 '22 at 13:15

1 Answers1

3

Solution

Link ${CMAKE_DL_LIBS} to your target

target_link_libraries(<target> ${CMAKE_DL_LIBS})

How you could find solution

If you google dlopen and scroll a little you possibly will be able to find CMAKE_DL_LIBS page in CMake documentation

CMAKE_DL_LIBS

Name of library containing dlopen and dlclose.

The name of the library that has dlopen and dlclose in it, usually -ldl on most UNIX machines.

FreePhoenix888
  • 4,510
  • 3
  • 13
  • 22