[CMake] How to build and reference dlopen-able libraries?

Nils Gladitz nilsgladitz at gmail.com
Fri Sep 5 19:16:10 EDT 2014


On 06.09.2014 00:44, Ted Middleton wrote:
> Just wondering if there's a nice way of getting a library target's 
> includes but not linking to it, so I can use dlopen() on it instead. 
> If I want to build a shared library that gets implicitly loaded by an 
> executable, I would do this:
>
>   mylib/CMakeLists.txt:
>   add_library( mylib SHARED src/mylibmain.c src/mylibsomething.c )
>   target_include_directories( mylib PRIVATE src PUBLIC inc )
>   target_link_libraries( mylib png )
>
>   myexe/CMakeLists.txt:
>   add_executable( myexe src/main.c src/stuff.c )
>   target_include_directories( myexe PRIVATE src )
>   target_link_libraries( myexe PRIVATE mylib )
>
> What I really want, though, is for mylib's public include dir to be 
> accessible to myexe, but I don't want myexe to link to mylib - I'm 
> going to load it with dlopen()/LoadLibrary(). Do I have to do 
> something like this:
>
>   myexe/CMakeLists.txt:
>   get_property( mylibincs TARGET mylib PROPERTY 
> INTERFACE_INCLUDE_DIRECTORIES )
>   add_executable( myexe src/main.c src/stuff.c )
>   target_include_directories( myexe PRIVATE src ${mylibincs} )
>   #target_link_libraries not necessary
>
>
> This must be really simple, but I can't seem to find anything about it 
> on the cmake wikis or through google. There's a very old post from 
> 2006 or so (pre target properties, it uses include_directories()).

Try target_include_directories(myexe PRIVATE 
$<TARGET_PROPERTY:mylib,INTERFACE_INCLUDE_DIRECTORIES>)

Nils


More information about the CMake mailing list