0

I would like to

  • build a binary that is relying on a shared library at runtime.
  • the library does not exist while compiling the binary.
  • the headers for the library are available

I would like to create a CMake project under those circumstances. Therefore I tried the following, but it will complain about the missing lib for sure:

cmake_minimum_required (VERSION 3.8)

project (example)

add_executable(${PROJECT_NAME}
    main.c
)

target_link_libraries(${PROJECT_NAME}
    PRIVATE
    # system libs
        lib_that_does_not_yet_exist
)

When removing the link instruction to lib_that_does_not_yet_exist, then I will get a undefined reference error.

Question: Is there any way to accomplish what I need?

Background: I am crosscompiling the binary, the lib is therefore having the wrong architecture to install it on the host. I know there may be some ways around that issue, but I am mainly interested in the above question.

ataraxis
  • 1,257
  • 1
  • 15
  • 30
  • https://stackoverflow.com/questions/5555632/can-gcc-not-complain-about-undefined-references but can't you just copy the library and link with it? – KamilCuk Nov 11 '20 at 19:33
  • There is definitely a way to accomplish what you need. What you describe is exactly what vulkan/dx12/etc do. I just have never had to do it myself. If you are interested take a look at the vulkansdk (it's opensource) and see what it does. –  Nov 11 '20 at 19:40
  • It is possible to use [`dlopen`, ect.](https://linux.die.net/man/3/dlopen) to dynamically load & access a shared library at runtime, but you will loose performance this way and make the code for accessing the functions more complex. It's probably best to get your hands the shared libs for the target system simply linking them on the build system. It may be a bit harder to tell the linker where to find those libs, but you'll end up with a simpler program that performs better in the end. – fabian Nov 13 '20 at 21:17

0 Answers0