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)
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!
Trying to package an application with Microsoft Visual Studio 2022 and qt6 libraries I do not no were to start from. I've done a bit of research most people used qt5 and when I replace the text with qt6 it doesn't seem to work. I used dependency walker to find the necessary libraries and I seem to have achieved a way to add the Visual Studio libraries but not qt and VTK.
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
foreach(p
CMP0071 # 3.10: Let AUTOMOC and AUTOUIC process GENERATED files
)
if(POLICY ${p})
cmake_policy(SET ${p} NEW)
endif()
endforeach()
PROJECT( QtVTKExample )
#set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
if (MSVC_VERSION EQUAL 1930 AND MSVC_TOOLSET_VERSION EQUAL 142)
cmake_host_system_information(RESULT VS_DIR QUERY VS_17_DIR)
file(GLOB MSVC_REDIST_LIBRARIES "${VS_DIR}/VC/Redist/MSVC/*/${MSVC_C_ARCHITECTURE_ID}/Microsoft.VC143.CRT/*.dll")
list(APPEND CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS "${MSVC_REDIST_LIBRARIES}")
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS TRUE)
endif()
include(InstallRequiredSystemLibraries)
# Add to top of file CPACK stuff
include(CTest)
if(WIN32)
set(CPACK_GENERATOR "NSIS")
else()
set(CPACK_GENERATOR "ZIP")
endif()
include(CPack)
find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonCore
vtkCommonDataModel
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2
QUIET
)
# The CMake build process might generate some new files in the current
# directory. This makes sure they can be found.
set( CMAKE_INCLUDE_CURRENT_DIR ON )
# This allows CMake to run one of Qt's build tools called moc
# if it is needed. moc.exe can be found in Qt's bin directory.
# We'll look at what moc does later.
set( CMAKE_AUTOMOC ON )
set( CMAKE_AUTOUIC ON )
# Find the Qt widgets package. This locates the relevant include and
# lib directories, and the necessary static libraries for linking.
find_package( Qt6Widgets )
set( UIS mainwindow.ui )
qt6_wrap_ui( UI_Srcs ${UIS} )
set( ICONS Icons/icons.qrc )
qt6_add_resources( QRC_Srcs ${ICONS} )
# Also link to VTK
find_package( VTK REQUIRED )
include( ${VTK_USE_FILE} )
# Define the executable output and its sources
add_executable( QtVTKExample MACOSX_BUNDLE
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
tabcontent.cpp
tabcontent.h
tabcontent.ui
currentstl.cpp
currentstl.h
recentstl.cpp
recentstl.h
${UI_Srcs}
${QRC_Srcs}
)
# Tell CMake that the executable depends on the Qt::Widget libraries.
target_link_libraries( QtVTKExample Qt6::Widgets )
# Tell CMake that the executable depends on the VTK libraries
target_link_libraries( QtVTKExample ${VTK_LIBRARIES} )
# /calc_cmake/CMakeLists.txt
install(TARGETS QtVTKExample
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)
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.
I wanted to use boost::filesystem for my C++ code so I decided to add boost libraries into my CMakeLists.txt but I keep getting this message and it's not working right.
ipo: warning #11012: unable to find #loader_path/libboost_system-mt.dylib
Here is my CMakeLists.txt file.
cmake_minimum_required ( VERSION 3.10 )
set ( CMAKE_CXX_COMPILER icpc )
set ( CMAKE_CXX_STANDARD 17 )
set ( CMAKE_CXX_FLAGS "-fast -qopenmp -Wall" )
project( FLattice CXX )
# Add include files (-I option)
include_directories ( ${PROJECT_SOURCE_DIR}/include )
include_directories ( /opt/fftw/include )
# Specify the Library directory (-L option)
link_directories ( /opt/fftw/lib )
# Add executing files
file ( GLOB lib_codes ${PROJECT_SOURCE_DIR}/lib/*.cpp )
add_executable ( ${PROJECT_NAME} main.cpp ${lib_codes} )
# Boost
set(boost_min_ver 1.69.0)
set(boost_libs system filesystem)
find_package(Boost ${boost_min_ver})
if(Boost_FOUND)
find_package(Boost ${boost_min_ver} COMPONENTS ${boost_libs})
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
endif()
# Link external libraries
target_link_libraries ( ${PROJECT_NAME} fftw3 )
# Link project "library" when compile FLattice
# target_link_libraries ( FLattice library_code )
# Add sub-directory
# add_subdirectory ( lib )
I installed Boost by brew install boost and I simply just added the #Boost part to my original CMakeLists.txt file.
What am I doing wrong? Any thoughts?
update Seems like the program is working fine (I thought it wasn't working, but it was). However, I still get the same warning message.
ipo: warning #11012: unable to find #loader_path/libboost_system-mt.dylib
I started to think that this warning is an Intel issue and not a boost issue as in https://software.intel.com/en-us/forums/intel-c-compiler/topic/518493.
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.