0

I'm currently porting a project to CMake which depends on a bunch of third-party libraries, none of which supports CMake. So far the project compiles well but I need to link the executable to those third-party libraries. Does CMake offer any way to create a build target that, once added as a link target, enables the project to link to all third-party libraries?

RAM
  • 2,257
  • 2
  • 19
  • 41
  • If you want to **build** those non-CMake libraries with your CMake project, then look into [ExternalProject_Add](https://cmake.org/cmake/help/v3.9/module/ExternalProject.html) command. If you already have those libraries built, as `.a` or `.so` files, and want just to **link** them, then see [that question](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library). – Tsyvarev Aug 24 '20 at 19:26
  • @Tsyvarev in this case there is no need to build anything. The only requirement is to import header files and link to a bunch of libraries. – RAM Aug 25 '20 at 10:32

2 Answers2

0

You may create INTERFACE library target and for it specify list of include directories and list of libraries to link. Whenever that target will linked, those include directories and libraries will be propagated to the consumer.

# Create the library which represents all third-party libraries
add_library(third_party_libs INTERFACE)
target_include_directories(third_party_libs INTERFACE
  <include-directory-1>
  <include-directory-2>
  ...
)
target_link_libraries(third_party_libs INTERFACE
  <external-lib-1>
  <external-lib-2>
  ...
)

# Create executable in your project
add_executable(my_exe <sources...>)

# This will propagate to the executable all include directories and libraries from
# the INTERFACE library target.
target_link_libraries(my_exe third_party_libs)
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
0

The right way of ingesting third-party libraries that don't provide their own CMake config modules is to write your own custom find modules for each package, and each find module specifies the set of libraries to be used as IMPORTED targets.

Reference:

RAM
  • 2,257
  • 2
  • 19
  • 41