I am trying to build a dbgsym package with cmake and cannot find any help anywhere. I've seen CPACK_DEBIAN_DEBUGINFO_PACKAGE but cannot use it in CMakeLists.txt since the version of CMake I am using does not have that.
I do have set(CMAKE_BUILD_TYPE Debug).
I want to be able to include debug symbols with the package.
The following CMakeLists.txt creates a debug symbol package. I didn't check if it's usable in gdb, though.
cmake_minimum_required(VERSION 3.13)
message( STATUS "Build type is " ${CMAKE_BUILD_TYPE} )
project( demonstrator
DESCRIPTION "Demonstrator"
VERSION 1.0.0
LANGUAGES CXX )
add_library( my_library SHARED )
target_sources( my_library
PRIVATE my_code.cpp )
target_include_directories( my_library
PUBLIC include )
set_target_properties ( my_library
PROPERTIES
PUBLIC_HEADER "include/my_code.hpp" )
install(
TARGETS my_library
ARCHIVE
DESTINATION lib
COMPONENT runtime
LIBRARY
DESTINATION lib
COMPONENT runtime
PUBLIC_HEADER
DESTINATION include
COMPONENT development
)
set( CPACK_PACKAGE_NAME ${PROJECT_NAME} )
set( CPACK_PACKAGE_CONTACT "Contact name" )
set( CPACK_PACKAGE_VERSION ${PROJECT_VERSION} )
set( CPACK_PACKAGE_DESCRIPTION_SUMMARY "Description" )
set( CPACK_GENERATOR DEB )
set( CPACK_DEB_COMPONENT_INSTALL ON )
set( CPACK_DEBIAN_PACKAGE_DEPENDS "" )
set( CPACK_DEBIAN_RUNTIME_DEBUGINFO_PACKAGE ON )
include( CPack )
You might be interested in this ticket regarding a bug in CMake regarding debug symbol creation.
Related
I have the following problem. I have a project (classic c++ library) called arsenalgear-cpp and want to compile it and let it be distributable for other projects.
My CMakeLists.txt is the following:
# CMake project settings
cmake_minimum_required( VERSION 3.15 )
project( arsenalgear-cpp
VERSION 1.0
DESCRIPTION "Build system for arsenalgear-cpp."
LANGUAGES CXX
)
# Error if building out of a build directory
file( TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH )
if( EXISTS "${LOC_PATH}" )
message( FATAL_ERROR "You cannot build in a source directory (or any directory with "
"CMakeLists.txt file). Please make a build subdirectory. Feel free to "
"remove CMakeCache.txt and CMakeFiles." )
endif()
# Other settings for paths
include_directories( . )
# Compile tests
add_subdirectory( test )
# Create static library
file( GLOB source src/*cpp )
add_library( arsenalgear STATIC ${source} )
# Installing headers
if( UNIX )
set( INSTALLATION_DIR_INCLUDE /usr/include )
elseif( APPLE )
set( INSTALLATION_DIR_INCLUDE /usr/local/include )
elseif( WIN32 )
set( WIN_INSTALLATION_DIR_INCLUDE "" CACHE STRING "Installation directory for Windows OSs." )
set( INSTALLATION_DIR_INCLUDE ${WIN_INSTALLATION_DIR_INCLUDE} )
endif()
INSTALL(
FILES include/containers.hpp include/math.hpp include/operators.hpp include/system.hpp include/stream.hpp include/type.hpp include/constants.hpp include/utils.hpp
DESTINATION ${INSTALLATION_DIR_INCLUDE}/arsenalgear
)
# Creating static libraries path
if( UNIX )
set( INSTALLATION_DIR_LIB /usr/lib )
elseif( APPLE )
set( INSTALLATION_DIR_LIB /usr/local/lib )
elseif( WIN32 )
set( WIN_INSTALLATION_DIR_LIB "" CACHE STRING "Installation directory for Windows OSs." )
set( INSTALLATION_DIR_LIB ${WIN_INSTALLATION_DIR_LIB} )
endif()
# Installing cmake package configuration files and libraries
INSTALL(
FILES build/libarsenalgear.a
DESTINATION ${INSTALLATION_DIR_LIB}
)
install(
FILES scripts/cmake/arsenalgearConfig.cmake
DESTINATION ${INSTALLATION_DIR_LIB}/cmake/arsenalgear
)
I will not post the content of the CMakeLists.txt of the directory test, since it is trivial.
I will post instead the content of scripts/cmake/arsenalgearConfig.cmake which is important:
# Headers path
set( arsenalgear_INCLUDE_DIRS ${PREFIX}/include/arsenalgear)
# Libraries path
set( arsenalgear_LIBRARIES ${PREFIX}/lib/libarsenalgear.a)
The library is correctly compiled and if I want to use it with an external project (once I have installed it) I should do:
find_package( arsenalgear )
target_link_libraries( ${TARGET_NAME} PRIVATE arsenalgear )
And that's fine. However I would be able to link the library using the CMake namespace, in this way:
find_package( arsenalgear )
target_link_libraries( ${TARGET_NAME} PRIVATE arsenalgear::arsenalgear )
I searched all around the web and the StackOverflow community for similar questions and found many of them, but none solved my problem.
How should I modify my CMake files in order to satisfy my requirements? Thanks.
PS: if you want to suggest also general improvements to my CMake files you are more than welcome!
I want to include the pytorch C++ API to the large C++ software I am working on.
For legacy reasons, I must use find_package and the associated find_path and find_library functions, instead of the proposed target_link_libraries.
Here's my FindTORCH.cmake :
include( FindPackageHandleStandardArgs )
find_path( TORCH_INCLUDE_DIR torch/torch.h
PATHS
/path/to/libtorch/include/torch/csrc/api/include/
NO_DEFAULT_PATH )
find_library( TORCH_LIBRARIES libtorch.so
PATHS
/path/to/libtorch/lib/
NO_DEFAULT_PATH )
FIND_PACKAGE_HANDLE_STANDARD_ARGS( TORCH REQUIRED_VARS TORCH_INCLUDE_DIR TORCH_LIBRARIES )
if ( TORCH_FOUND )
message( STATUS "Torch found" )
endif( TORCH_FOUND )
mark_as_advanced( TORCH_LIBRARIES TORCH_INCLUDE_DIR )
At the compilation, the torch files are found and I can include <torch/torch.h> in a random .cxx in the project.
However, if I add to the .cxx :
torch::Tensor tensor = torch::rand({2, 3});
cout << tensor << std::endl;
Then I cannot compile anymore and I get the following error :
/path/to/libtorch/include/torch/csrc/api/include/torch/utils.h:4:10: fatal error: ATen/record_function.h: No such file or directory
#include <ATen/record_function.h>
^~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
I'm working Ubuntu 18, C++ 14, and the cmake version is 3.10.2 .
Thanks in advance
Torch exposes its own targets. To use them effectively, simply remove FindTORCH.cmake from your project, and add /path/to/libtorch/ to your prefix path:
cmake_minimum_required(VERSION 3.19) # or whatever version you use
project(your-project CXX)
list(APPEND CMAKE_PREFIX_PATH "/path/to/libtorch/")
find_package(Torch REQUIRED CONFIG) # this ensure it find the file provided by pytorch
add_executable(your-executable main.cpp)
target_link_libraries(your-executable PUBLIC torch::Tensor)
If you really insist to use your own FindTorch.cmake instead of the correct one, you can modify it to create an imported target that you will then link:
You can change your find module very slightly to gain a modern CMake interface from it:
include( FindPackageHandleStandardArgs )
find_path( TORCH_INCLUDE_DIR torch/torch.h
PATHS
/path/to/libtorch/include/torch/csrc/api/include/
NO_DEFAULT_PATH )
find_library( TORCH_LIBRARIES libtorch.so
PATHS
/path/to/libtorch/lib/
NO_DEFAULT_PATH )
FIND_PACKAGE_HANDLE_STANDARD_ARGS( TORCH REQUIRED_VARS TORCH_INCLUDE_DIR TORCH_LIBRARIES )
if ( TORCH_FOUND )
message( STATUS "Torch found" )
add_library(torch::Tensor SHARED IMPORTED) # mimic the names from pytorch maintainers
set_target_properties(torch::Tensor
PROPERTIES
IMPORTED_LOCATION "${TORCH_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${TORCH_INCLUDE_DIR}"
# on windows, set IMPORTED_IMPLIB to the .lib
)
endif( TORCH_FOUND )
mark_as_advanced( TORCH_LIBRARIES TORCH_INCLUDE_DIR )
Then, in your main CMake file, you can use the imported target like any other targets:
find_package(Torch REQUIRED)
add_executable(your-executable main.cpp)
target_link_libraries(your-executable PUBLIC torch::Tensor)
I have a cmake project which is a static library that i have to share with other teams in my company but just the .lib file and not the source code.
This library depends on Boost.
Now i build the library and transfer the install folder onto another pc (exe_pc) and use it in a exe project (this exe also depends on boost).
Now at compile time i get a linking error saying that E:/Development/vcpkg/installed/x64-windows/lib/boost_system-vc140-mt.lib cannot be opened.
so i open and check the generated LibTargets.cmake and it contains absolute paths of boost library for pc on which the library was built.
# The installation prefix configured by this project.
set(_IMPORT_PREFIX "C:/Program Files/cpp_licensing")
# Create imported target Licensing::liblicensing
add_library(Licensing::liblicensing STATIC IMPORTED)
set_target_properties(Licensing::liblicensing PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "_LICENSING_DEBUG=0"
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "\$<\$<NOT:\$<CONFIG:DEBUG>>:E:/Development/vcpkg/installed/x64-windows/lib/boost_system-vc140-mt.lib>;\$<\$<CONFIG:DEBUG>:E:/Development/vcpkg/installed/x64-windows/debug/lib/boost_system-vc140-mt-gd.lib>;\$<\$<NOT:\$<CONFIG:DEBUG>>:E:/Development/vcpkg/installed/x64-windows/lib/boost_filesystem-vc140-mt.lib>;\$<\$<CONFIG:DEBUG>:E:/Development/vcpkg/installed/x64-windows/debug/lib/boost_filesystem-vc140-mt-gd.lib>;cryptopp-static;wbemuuid"
)
LibLicensingTargets-release.cmake
et_target_properties(Licensing::liblicensing PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
IMPORTED_LOCATION_RELEASE "C:/Program Files/cpp_licensing/lib/licensing.lib"
)
list(APPEND _IMPORT_CHECK_TARGETS Licensing::liblicensing )
list(APPEND _IMPORT_CHECK_FILES_FOR_Licensing::liblicensing "C:/Program Files/cpp_licensing/lib/licensing.lib" )
The user is supposed to have boost installed, the only problem i have is that because absolute paths are used in targets file user has to manually change them on his pc in the targets file.
what changes do i make to cmake so that the libraries built folder can be shared. How do i distribute a library built using cmake ??
Here is the cmake for the library project
cmake_minimum_required(VERSION 3.10)
project(liblicensing)
message("~~ Project: " ${PROJECT_NAME})
set(LIB_NAME "${PROJECT_NAME}")
set(PROJECT_VERSION 1.0)
include_directories(
${PROJECT_SOURCE_DIR}/include
)
if (WIN32)
find_package(Boost REQUIRED system filesystem)
find_package(cryptopp REQUIRED)
find_package(unofficial-date REQUIRED)
include_directories(
${Boost_INCLUDE_DIR}
)
else()
find_package(date REQUIRED)
endif ()
file(GLOB_RECURSE HEADER_FILES include/*.h include/*.hpp)
file(GLOB_RECURSE SOURCES src/*.cpp)
add_library(${LIB_NAME} STATIC ${SOURCES} ${HEADER_FILES} )
set_target_properties(
${LIB_NAME}
PROPERTIES
OUTPUT_NAME "licensing"
POSITION_INDEPENDENT_CODE ON
CXX_STANDARD 14
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
LINKER_LANGUAGE CXX
)
if (MSVC)
set_target_properties(${LIB_NAME} PROPERTIES DEBUG_POSTFIX "_d")
endif()
add_library(Licensing::liblicensing ALIAS ${LIB_NAME})
set(INSTALLATION_DIR "${CMAKE_INSTALL_PREFIX}")
set(CMAKE_INSTALL_LIBDIR "${INSTALLATION_DIR}/lib")
set(CMAKE_INSTALL_BINDIR "${INSTALLATION_DIR}/bin")
set(CMAKE_INSTALL_INCLUDEDIR "include")
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME})
target_include_directories(${LIB_NAME}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
if (WIN32)
target_link_libraries(
${LIB_NAME}
${Boost_LIBRARIES}
cryptopp-static
wbemuuid
)
else()
target_link_libraries(
${LIB_NAME}
cryptopp
boost_system
boost_filesystem
)
endif ()
if(CMAKE_BUILD_TYPE MATCHES Debug)
target_compile_definitions(${LIB_NAME} PUBLIC _LICENSING_DEBUG=1)
elseif(CMAKE_BUILD_TYPE MATCHES Release)
target_compile_definitions(${LIB_NAME} PUBLIC _LICENSING_DEBUG=0)
endif()
# Expose Projects's public includes to other subprojects through cache variable.
set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
###############
# Installation
##
install(TARGETS ${LIB_NAME}
EXPORT LibLicensingTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
# INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${LIB_NAME}
FILES_MATCHING PATTERN "*.h*")
install(EXPORT LibLicensingTargets
FILE LibLicensingTargets.cmake
NAMESPACE Licensing::
DESTINATION ${INSTALL_CONFIGDIR}
)
#####################
# ConfigVersion file
##
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/LibLicensingConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
configure_package_config_file(
${CMAKE_CURRENT_LIST_DIR}/CMake/LibLicensingConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/LibLicensingConfig.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)
## Install all the helper files
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/LibLicensingConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/LibLicensingConfigVersion.cmake
DESTINATION ${INSTALL_CONFIGDIR}
)
Not an entire answer but too much for a comment:
If you inspect your file LibLicensingTargets.cmake closely, you see that cryptopp-static and wbemuuid are not resolved down to the library level.
Looking at how you reference Boost you use the oldstyle version (${Boost_LIBRARIES}) that resolves down to the library level.
Try instead to use the targets provided by FindBoost.cmake module as follows:
if (WIN32)
target_link_libraries(
${LIB_NAME}
Boost::system
Boost::filesystem
cryptopp-static
wbemuuid
)
else()
target_link_libraries(
${LIB_NAME}
cryptopp
Boost::system
Boost::filesystem
)
endif()
Another thing that puzzles me is why you added the ALIAS:
add_library(Licensing::liblicensing ALIAS ${LIB_NAME})
This ALIAS is properly created in the LibLicensingTargets.cmake file, so you do not need to create it on your own.
Another thing is, that you are giving your INSTALL command absolute paths. To make the package entirely relocatable you need to use relative paths (relative to the CMAKE_INSTALL_PREFIX, the installation directory).
The following commands should therefore be changed to:
set(CMAKE_INSTALL_LIBDIR "lib")
set(CMAKE_INSTALL_BINDIR "bin")
set(CMAKE_INSTALL_INCLUDEDIR "include")
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME})
If you further add the line:
export(TARGETS liblicensing NAMESPACE LibLicensing:: FILE LibLicensingTargets.cmake)
to your CMakeLists.txt, outside projects are able to import target from your build tree folder as of liblicensing was installed therein. See the CMake documentation of the export command.
I'm using CLion with CMake. I have my own static library "libxxx.a".
I'm trying to link it this way in CMakeLists.txt:
target_link_libraries(myProject ./lib/libxxx.a)
And this way I'm including library to my main.cpp.
#include "xxx.h".
But I have error fatal error: xxx.h: No such file or directory.
What should I do?
CMake include a prebuilt static library
As a sketch you need your project to look something like this
project( myProject )
set( SOURCE_FILES main.cpp )
add_library( myLibrary STATIC IMPORTED )
set_property( TARGET myLibrary PROPERTY IMPORTED_LOCATION /path/to/lib/libxxx.a )
include_directories( /path/to/headers/ )
add_executable( myProject ${SOURCE_FILES} )
target_link_libraries( myProject myLibrary )
You might be able to replace include_directories with:
set_property( TARGET myLibrary PROPERTY INCLUDE_DIRECTORIES /path/to/headers/ )
A better way to go is to compile the static library from source and then use
target_include_directories( myLibrary PUBLIC /path/to/headers/ )
And then they get handled automatically.
I'm using cmake to build an executable to run on an Intel Galileo board.
My question is how do I include the external mraa library in the build process.
Mraa library
When I download the library from git (1) do I need to build it as described here
Mraa compiling instructions
What do I need to put in my CMakeLists.txt file to pick up the library?
This is what I have thus far in my CMakeLists.txt file but I believe it is incorrect.
### MRAA ###
add_subdirectory(mraa-master/src)
file(GLOB mraa_SRC
"mraa-master/src/*.c"
)
include_directories( "${PROJECT_SOURCE_DIR}/mraa-master/include" )
add_library( ${MRAA_LIBRARY_NAME} SHARED ${mraa_SRC} )
Thank you
cmake_minimum_required(VERSION 2.8)
MESSAGE( STATUS "Starting build process")
SET( CMAKE_VERBOSE_MAKEFILE on )
if (CMAKE_BUILD_TYPE EQUAL "Debug")
MESSAGE(STATUS "Building in debug mode")
elseif (CMAKE_BUILD_TYPE EQUAL "Release")
MESSAGE(STATUS "Building in release mode")
endif()
SET(PROJECT_NAME "TestProject")
SET(APPLICATION_NAME "TestApplication")
SET(SAFE_STRING_LIBRARY "SafeString")
SET(APPLICATION_LIBRARY "Applibrary")
PROJECT( ${PROJECT_NAME} )
MESSAGE( STATUS "PROJECT: " ${PROJECT_NAME} )
SET(WRSDK_PATH "$ENV{WINDRIVER_SDK_DIR}")
IF (WRSDK_PATH)
else()
SET(WRSDK_PATH /opt/windriver/wrlinux/5.0-intel-quark/)
endif()
SET(APPLIBRARY_NAME ${APPLICATION_LIBRARY} )
SET(SAFE_STRING_LIBRARY_NAME ${SAFE_STRING_LIBRARY} )
### SAFE STRING ###
add_subdirectory(SafeStringStaticLibrary/safeclib)
file(GLOB safestring_SRC
"SafeStringStaticLibrary/safeclib/*.c"
)
include_directories( "${PROJECT_SOURCE_DIR}/SafeStringStaticLibrary /safeclib" )
add_library( ${SAFE_STRING_LIBRARY_NAME} SHARED ${safestring_SRC} )
include(ExternalProject)
ExternalProject_Add(mraa
GIT_REPOSITORY https://github.com/intel-iot-devkit/mraa.git
GIT_TAG v0.8.0
UPDATE_COMMAND ""
INSTALL_COMMAND "" )
file(GLOB app_SRC
"classes/*.cpp"
"Logger.cpp"
"sqlite3.c"
"shell.c"
)
add_library( ${APPLIBRARY_NAME} SHARED ${app_SRC})
include_directories( "${CMAKE_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/SafeStringStaticLibrary/include" )
add_executable( ${APPLICATION_NAME} ${APPLICATION_NAME}.cpp
include_directories( "${CMAKE_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/SafeStringStaticLibrary/include" "${CMAKE_SOURCE_DIR}/classes")
TARGET_LINK_LIBRARIES( ${APPLICATION_NAME} ${APPLIBRARY_NAME} ${SAFE_STRING_LIBRARY_NAME} -lrt -lpthread -lgcov -ldl)
The simplest way for build your project alongside with 3d party project is add_subdirectory() that subproject. Approach below implies that you (manually) download(git clone) sources of mraa project into mraa-lib subdirectory of your project's sources.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
MESSAGE( STATUS "Starting build process")
# Configure subproject before definition of variables for main project.
#
# Subproject defines *mraa* library target.
add_subdirectory(mraa-lib)
# Before installation, public mraa headers are contained under *api* subdirectory.
include_directories(mraa-lib/api)
... # create executable ${APPLICATION_NAME}
# Linking with mraa library is straightforward.
target_link_libraries(${APPLICATION_NAME} mraa)
That way mraa subproject will be configured, built and installed alongside with your project. So, if you cross-compile your project(using toolchain file, or inside IDE), the subproject will also be cross-compiled.