Use external libraries that are already linked to a subproject


Let’s suppose I have a project with two subfolders and subPrj1 links libhdf5_D.lib:

Now I want to link subPrj1 to subPrj2 and use the same libhdf5_D.lib in subPrj2.
What is the “cleanest” way to do that? Do I have to manually link subPrj1 and libhdf5_D.lib to subPrj2 or it is enough to simply link subPrj1 to subPrj2?

subPrj1 has the HDF5 information on its PUBLIC interface, so it will be given to consumers of subPrj1 automatically. You should be able to do target_link_libraries(subPrj2 PRIVATE subPrj1) (feel free to use PUBLIC if needed here) and have HDF available in subPrj2.

1 Like

And do I also have to include HDF5 headers to subPrj2 via target_include_directories(subPrj2 PRIVATE ${H5_DIR}/include) ? or it is enough target_include_directories(subPrj2 PRIVATE subPrj1/include)?

The usage requirements will come along with subPrj1, so HDF5’s PUBLIC include directory will come along. I recommend adding $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> to subPrj1 so that target_link_libraries is completely sufficient.

1 Like

Thank you a lot! that works