CPACK third party libraries

I have worked out how to make my cmake compile, but now deploying with libraries is confusing me greatly.

As you can see below “target_link_libraries(my_project ${libavcodec_LIBRARIES})” allows me to use this code, but how do I package that library with “cpack” so that I don’t need a user to install it? I have been looking everywhere to understand this…

Thank you so much! Any tutorial or something? How do I even find the necessary files if I just want to manually add them to the bundle?

cmake_minimum_required(VERSION 2.8.12)
project(my_project)


# if (CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_STANDARD 17)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    set(CMAKE_CXX_FLAGS "-I /usr/local/include -L /usr/local/lib -l tensorflow -Wall -Os -g ${CMAKE_CXX_FLAGS}")
    # -I /usr/local/include -L /usr/local/lib -l tensorflow -std=c++17 
    message("CMAKE_COMPILER_IS_GNUCXX is True")
    message("option is: ${CMAKE_CXX_FLAGS}")
endif() #(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# endif (CMAKE_COMPILER_IS_GNUCXX)


find_package(PkgConfig)

pkg_check_modules(libavcodec REQUIRED libavcodec)
pkg_check_modules(libavformat REQUIRED libavformat)
pkg_check_modules(libavdevice REQUIRED libavdevice)
pkg_check_modules(libavutil REQUIRED libavutil)
pkg_check_modules(libswscale REQUIRED libswscale)
pkg_check_modules(sdl REQUIRED sdl)

add_compile_definitions(${libavcodec_CFLAGS_OTHER})
add_compile_definitions(${libavformat_CFLAGS_OTHER})
add_compile_definitions(${libavdevice_CFLAGS_OTHER})
add_compile_definitions(${libavutil_CFLAGS_OTHER})

find_package( OpenCV REQUIRED )
add_compile_definitions(${OpenCV_CFLAGS_OTHER})

add_compile_definitions(${libswscale_CFLAGS_OTHER})



add_executable(my_project my_project.cpp)

target_link_libraries(my_project "-framework Cocoa")
target_link_libraries(my_project "-framework IOKit")
target_link_libraries(my_project ${libavcodec_LIBRARIES})
target_link_libraries(my_project ${libavformat_LIBRARIES})
target_link_libraries(my_project ${libavdevice_LIBRARIES})
target_link_libraries(my_project ${libavutil_LIBRARIES})
target_link_libraries(my_project ${OpenCV_LIBRARIES})
target_link_libraries(my_project ${libswscale_LIBRARIES})
# target_link_libraries(my_project ${sdl_LIBRARIES})

SET(dlib_ROOT "~/Documents/Code/my_project/build/dlib-19.24/")
target_link_libraries(my_project  dlib)

install(
    TARGETS my_project
    COMPONENT Application
    )

set(CPACK_COMPONENTS_ALL Application Libraries)
set(CPACK_COMPONENT_APPLICATION_DISPLAY_NAME "Analysis")
set(CPACK_COMPONENT_APPLICATION_LIBRARY_NAME "Video Libaries")



set(CPACK_GENERATOR "TGZ")
if (WIN32)
    list(APPEND CPACK_GENERATOR "NSIS")
elseif (APPLE)
    list(APPEND CPACK_GENERATOR "Bundle")
endif(WIN32)
set(CPACK_SOURCE_GENERATOR "ZIP;TGZ")

include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${my_project_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${my_project_VERSION_MINOR}")
include(CPack)

You’ll want to look at file(GET_RUNTIME_DEPENDENCIES) for that.

Thanks! That was perfect. And quick question how do I link those libraries (for example in a “lib” folder) so that the output executable (unix file) will look for the libraries there?

Currently I get an error: “dyld: Library not found: @rpath/libtensorflow.2.dylib”, where libtensorflow.2.dylib is in the lib folder.

Thanks for any help!

For future people searching:

file(GET_RUNTIME_DEPENDENCIES
    EXECUTABLES my_project
    RESOLVED_DEPENDENCIES_VAR RESOLVED_DEPS
    UNRESOLVED_DEPENDENCIES_VAR UNRESOLVED_DEPS
  )
  foreach(FILE ${RESOLVED_DEPS})
    #Do whatever you need with these libraries
  endforeach()
  foreach(FILE ${UNRESOLVED_DEPS})
    message(STATUS "Unresolved: ${FILE}")
  endforeach()

For that, you’ll need to add an RPATH entry (CMAKE_{INSTALL,BUILD}_RPATH) to the appropriate location. Probably something like @executable_path/../lib or the like for the install and the containing path for the build.

For what it’s worth, I (and many programs I’ve seen) just dump the DLLs in the same bin folder as my exe.

Even when the libraries have their dlls spread across a bunch of directories, I put them directly in bin/ so my exe can see them.

Also in regards to actually finding your DLLs for installation:

Some Find modules make this process easy because they provide a LIB_BIN_DIR or similar that you can pass right to file(GET_RUNTIME_DEPENDENCIES or install(RUNTIME_DEPENDENCY_SET but a lot of times I have to manually add the search directory (usually ends up being something like LIB_INCLUDE_DIR/../bin)