-2

I have a problem with following C++ code built with CMake:

#include <iostream> #include <dlfcn.h> int main() { void* vulkanLibrary = dlopen( "libvulkan.so.1", RTLD_NOW ); if(vulkanLibrary) return 0; else return 1; } 

And here the CMakeLists:

cmake_minimum_required(VERSION 3.14) project(VulkanTest) set(CMAKE_CXX_STANDARD 17) add_link_options(-ldl) add_executable(VulkanTest main.cpp) 

Build Output:

/opt/JetBrains/CLion-2019.2/bin/cmake/linux/bin/cmake --build /home/eriksimon/CLionProjects/VulkanTest/cmake-build-debug --target VulkanTest -- -j 4 -- Configuring done -- Generating done -- Build files have been written to: /home/eriksimon/CLionProjects/VulkanTest/cmake-build-debug [ 50%] Linking CXX executable VulkanTest /usr/bin/ld: CMakeFiles/VulkanTest.dir/main.cpp.o: in function `main': /home/eriksimon/CLionProjects/VulkanTest/main.cpp:5: undefined reference to `dlopen' collect2: error: ld returned 1 exit status make[3]: *** [CMakeFiles/VulkanTest.dir/build.make:84: VulkanTest] Error 1 make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/VulkanTest.dir/all] Error 2 make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/VulkanTest.dir/rule] Error 2 make: *** [Makefile:118: VulkanTest] Error 2 

I get the linker error on dlopen reported as undefined reference. When googling this, I only found Posts telling to link with "-ldl" linker flag. Looks like I am setting it in my CMake file, though. What is the reason for it not being used by the linker?

7
  • Please provide the compiler/linker output for when you try to build. If running with make, run make VERBOSE=1 and provide the output for that. This will give us a lot more information about what is going on... Commented Aug 16, 2019 at 18:08
  • Oh great now anybody can just edit my Post if it doesn't please him ... ? Commented Aug 16, 2019 at 18:09
  • @squareskittles done :) Commented Aug 16, 2019 at 18:10
  • Can't reproduce. Works fine after edits. Please run make VERBOSE=1 in the build directory and provide the output. Commented Aug 16, 2019 at 18:11
  • make: *** No targets specified and no makefile found. Stop. Commented Aug 16, 2019 at 18:14

2 Answers 2

1

You have not linked with the dl library correctly. You are looking for the target_link_libraries command, using the pre-defined CMake variable CMAKE_DL_LIBS:

cmake_minimum_required(VERSION 3.14) project(VulkanTest) set(CMAKE_CXX_STANDARD 17) add_executable(VulkanTest main.cpp) target_link_libraries(VulkanTest ${CMAKE_DL_LIBS}) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked :)
0

Looks like you are not using CMake correctly. add_link_options is defined as

Add options to the link step for executable, shared library or module library targets in the current directory and below that are added after this command is invoked.

Emphasis is mine. You need to put add_link_options before you define your executable.

5 Comments

doesn't change anything :(
@ErikSimon you should provide MCVE. Edit your question and provide copy-paste code of C++ file and CMakeLists.txt
Sorry, but what is MCVE? Also there is all my code in the answer.
@ErikSimon You might want to read about minimal reproducible example. Also, you are telling me that you tried my suggestion, but it didn't work. Your code as posted right now can't possibly work, if you tried my suggestion and it still didn't work you need to post the version which didn't work.
Well now it is updated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.