Building TBB using Visual Studio 2015 x64 and CMake - c++

i would like to build TBB to use it in another CMake project. I tried to build TBB from the Github source using the makefile (upgraded with VisualStudio 2015). This failed due to a mysterious error:
LINK : fatal error LNK1181: cannot open input file 'opencv_core300.lib'
Where does this error could originate from?
My second try was is to build TBB using another repository that allows a build using CMake. This build produces an tbb.lib, tbb.dll, etc. file.
Now I am stuck how to incorporate is in my other cmake files. There is no TBBConfig.cmake or similar.
My CMakeLists.txt for my project looks like this:
cmake_minimum_required(VERSION 3.10)
project(IntrafraktionelleRegistrierung)
find_package(ITK REQUIRED
COMPONENTS
ITKRegistrationCommon
ITKRegistrationMethodsv4
)
include(${ITK_USE_FILE})
set(SRC
${CMAKE_PROJECT_NAME}.cxx
)
if (DEFINED ENV{TBBROOT})
message(STATUS "TBBROOT: $ENV{TBBROOT}")
else()
message(STATUS "TBBROOT not defined!")
endif()
find_package(TBB REQUIRED)
add_executable(${CMAKE_PROJECT_NAME} ${SRC})
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
tbb
)'
TBBROOT is the build directory of tbb. The FindTBB.cmake I have available is borrowed from here and copied to the modules directory of cmake.
The latest version of the binaries of TBB have a CMake folder with TBBConfig.cmake inside. I used this to link the TBB to my project but somehow I ended up by an error stating: "tbb-NOTFOUND.obj cannot be found". (This way is still under investigation.
Has someone used this repository to configure and build a cmake project?

About CMake related questions
Basically you have two options:
Integration of pre-built TBB binaries into your project
You can use the binaries of TBB (just as you did) according to the following example. After invocation of find_package(TBB REQUIRED) you will get TBB targets in a format TBB::<component> (e.g. TBB::tbb, TBB::tbbmalloc, etc). Also TBB_IMPORTED_TARGETS variable will contain all imported TBB targets.
So, you need to slightly modify your target_link_libraries:
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
${TBB_IMPORTED_TARGETS}
)
or
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
TBB::tbb
)
Also you can update your find_package if you need only TBB::tbb component in your project: find_package(TBB REQUIRED tbb)
Integration of TBB source code into your project
You can use tbb_build (it is a CMake wrapper using GNU Make on TBB Makefiles), but to run it on Windows under Visual Studio you will need to have GNU Make in your environment.
If you'd like to integrate this TBB (with CMake support) you can use add_subdirectory(<YOUR-TBB-ROOT>) (replace <YOUR-TBB-ROOT> with actual location of TBB) instead of find_package(TBB REQUIRED).

Related

Very slow intellisense for CMake-configured Vulkan-project

I have a Vulkan project configured with the following CMakeLists.txt on Windows:
cmake_minimum_required(VERSION 3.20)
project(Custom_Vulkan)
set(MINGW_INCLUDE "C:\\msys64\\mingw64\\include")
find_package(Vulkan REQUIRED)
find_package(glfw3 REQUIRED HINTS "C:\\msys64\\mingw64\\lib\\cmake")
add_subdirectory(shaders)
add_executable(AppTest main.cpp)
target_link_libraries(AppTest PUBLIC Vulkan::Vulkan glfw)
include_directories(${MINGW_INCLUDE})
Extensions for CMake (CMake & CMake Tools), and C++ (C/C++, C/C++ Extension Pack, C/C++ Themes) are installed, and I have configured and built my executable using the CMake Tools-extension.
Intellisense can at times be unreasonably slow and freezes completely, resulting in 0 autocompletion suggestions for both constants from built-in libraries like M_PI from cmath, but also for the Vulkan/GLFW-APIs which have been located by CMake.
Where should I search in order to locate the dependency that stalls Intellisense?
Is it possible to monitor/profile the Intellisense-search in order to find the issue?
The CMake-tools project is fully able to detect the dependencies, and compiles fine.
The Intellisense-freezes were in this case caused by dependency-issues between a FetchContent repository dependency in CMake and the C# vscode extension pack which caused an OmniSharp server-handle to remain open.
This was a standard FetchContent-pull:
FetchContent_Declare(
imgui_repo
GIT_REPOSITORY "https://github.com/ocornut/imgui.git"
)
FetchContent_MakeAvailable(imgui_repo)
Right-clicking the extension followed by disable(workspace) solved the issue.

CMake enable_language(CUDA) on Linux doesn't auto-include CUDA headers: how to resolve? [duplicate]

In CMake version 3.8, native support for CUDA as a language was introduced. When a project has CUDA as one of its languages, CMake will proceed to locate CUDA (e.g. it locates the nvcc binary).
As long as you only compile CUDA code - this is enough. But what if you want to compile a C++ target in that project? The CUDA includes are not -I'ed automatically, and CMakeCache.txt does not seem to contain the CUDA include path anywhere.
Do I actually have to run something find_package(CUDA 9.0 REQUIRED) even when CMake itself has already located CUDA? Or - can I obtain the include directory some other way?
The include directories, which are used by the compiler set by CMAKE_CUDA_COMPILER, can be retrieved from the CMake variable CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES.
For getting the libraries, the best way is probably to use find_library() in combination with CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES.
Example:
cmake_minimum_required(VERSION 3.9)
project(MyProject VERSION 1.0)
enable_language(CUDA)
find_library(CUDART_LIBRARY cudart ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
add_executable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp
)
target_include_directories(
binary_linking_to_cudart
PRIVATE
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
)
target_link_libraries(
binary_linking_to_cudart
${CUDART_LIBRARY}
)
This issue is also discussed on the CMake bug tracker: Provide target libraries for cuda libraries.
Update: CMake 3.17.0 adds FindCUDAToolkit
Instead of doing find_library() manually, the best way as of CMake 3.17.0 would be to use the CUDAToolkit module.
find_package(CUDAToolkit)
add_executable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp
)
target_link_libraries(binary_linking_to_cudart PRIVATE CUDA::cudart)
For support with earlier CMake versions, you can ship the CUDATookit module file with minimal changes in your repository.
These days, with CMake 3.18 and later, you can get most of what you need by examining the targets provided by find_package(CUDAToolkit) - which you do need even if CUDA has located the CUDA compiler. But actually, you may just depend on one of those targets and avoid using the include directories directly.
PS - If you happen to use cuda-api-wrappers (e.g. via find_package(cuda-api-wrappers)), it will take care of the dependencies for you.

Obtaining the CUDA include dir in C++ targets with native-CUDA-support CMake?

In CMake version 3.8, native support for CUDA as a language was introduced. When a project has CUDA as one of its languages, CMake will proceed to locate CUDA (e.g. it locates the nvcc binary).
As long as you only compile CUDA code - this is enough. But what if you want to compile a C++ target in that project? The CUDA includes are not -I'ed automatically, and CMakeCache.txt does not seem to contain the CUDA include path anywhere.
Do I actually have to run something find_package(CUDA 9.0 REQUIRED) even when CMake itself has already located CUDA? Or - can I obtain the include directory some other way?
The include directories, which are used by the compiler set by CMAKE_CUDA_COMPILER, can be retrieved from the CMake variable CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES.
For getting the libraries, the best way is probably to use find_library() in combination with CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES.
Example:
cmake_minimum_required(VERSION 3.9)
project(MyProject VERSION 1.0)
enable_language(CUDA)
find_library(CUDART_LIBRARY cudart ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
add_executable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp
)
target_include_directories(
binary_linking_to_cudart
PRIVATE
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
)
target_link_libraries(
binary_linking_to_cudart
${CUDART_LIBRARY}
)
This issue is also discussed on the CMake bug tracker: Provide target libraries for cuda libraries.
Update: CMake 3.17.0 adds FindCUDAToolkit
Instead of doing find_library() manually, the best way as of CMake 3.17.0 would be to use the CUDAToolkit module.
find_package(CUDAToolkit)
add_executable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp
)
target_link_libraries(binary_linking_to_cudart PRIVATE CUDA::cudart)
For support with earlier CMake versions, you can ship the CUDATookit module file with minimal changes in your repository.
These days, with CMake 3.18 and later, you can get most of what you need by examining the targets provided by find_package(CUDAToolkit) - which you do need even if CUDA has located the CUDA compiler. But actually, you may just depend on one of those targets and avoid using the include directories directly.
PS - If you happen to use cuda-api-wrappers (e.g. via find_package(cuda-api-wrappers)), it will take care of the dependencies for you.

How do I use cmake to build external libraries as well as my own application?

I am trying to build a cross platform OpenGL application, which means building and including multiple libraries (glfw, glbinding, glm, etc.) Because my application is cross platform, it makes sense to use cmake to generate all the build scripts and not have to muck about with them myself. I am attempting to target Windows and Linux specifically.
A main feature that I need is that the libraries I need are not installed on the host system. Furthermore, they cannot be installed (due to administrative reasons). What I need is to build these libraries and then build my application.
I am mostly working on Windows using Visual Studio 2017, which has cmake support included. Currently, I have attempted to build these libraries myself, however I am having many issues getting find_package to do the right thing.
My directory structure looks like this:
project/
|-src/
|- my sources for my application
|-include/
|- my header files
|-external/
|-glfw-3.2.1/
|-glbinding-2.1.4/
|-glfw-build/
|-glbinidng-build/
So I am attempting to build the external libraries and use them in my application. I am also attempting to follow cmake best practices. My CMakeLists.txt currently looks like this:
cmake_minimum_required(VERSION 3.5)
project(glTestProj)
set(CMAKE_PREFIX_PATH "external/")
find_package(glfw3 3.2 REQUIRED)
find_package(glbinding REQUIRED)
add_executable(glTest src/main.cpp)
target_compile_features(glTest PRIVATE cxx_std_17)
target_compile_options(glTest PRIVATE -Wall -Wextra)
target_link_libraries(
glTest
glfw
glbinding::glbinding
)
The libraries in question (glfw and glbinding) both have instructions on including them via cmake, however I am running into this issue:
CMake Error at CMakeLists.txt:6 (find_package):
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Could not find a package configuration file provided by "glfw3" (requested
version 3.2) with any of the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.

C++ How to run programs in Clion when you need to include OpenGL libraries?

Hello I need to work with OpenGL and want to create my project in Clion. But Clion cannot compile and run my projects because of the libraries I need to include. I can create my own makefile and run the program in terminal, but I want to do it in the IDE. How can I make this happen?
First make sure you installed all libraries correctly using the compiler you configured in clion/cmake. Assuminf you have a fresh CMakeLists.txt like
cmake_minimum_required(VERSION 3.3.2)
project(MyGL CPP)
add_executable(demo-run main.cpp)
For linking your libraries you need two things. First tell the compiler where to find the include files and second which libraries to link. You could just hard code you local installation like
target_link_libraries(demo-run path/to/glfw.lib path/to/opengl.lib path/to/jpeg.lib ...)
target_include_directories(demo-run PRIVATE path/to/glfw/include path/to/opengl/include path/to/jpeg/include ...)
however this is not very portable and if you want to work with another compiler or on another machine your project file will fail. Instead you can use the package system of cmake
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_package(JPEG REQUIRED)
find_package(GLEW REQUIRED)
find_package (OpenGL REQUIRED)
find_package (GLM REQUIRED)
target_link_libraries(demo-run ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${JPEG_LIBRARIES} ${OPENGL_LIBRARIES})
target_include_directories(demo-run PRIVATE ${GLFW_INCLUDE_DIRS} ${GLEW_INCLUDE_DIR} ${JPEG_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
The glfw part is a bit tricky and works only on linux i guess see http://www.glfw.org/docs/3.0/build.html.
This code is not tested at all and you may need to specify some enviroment variables so cmake can find the packages or provide additional find scripts like https://github.com/lighttransport/nanogi/blob/master/cmake/FindGLM.cmake.
I would recommend to use the CMake build tool which does the work generating Makefiles for you and is also directly supported by clion. When you open the directory containing a CMakeLists.txt (CMake Project File) with clion, it should be automatically be loaded and compiled (if not just hit build)
A very simple example CMake project would look like this
cmake_minimum_required (VERSION 2.8.9)
project (OpenGl-Stuff)
include_directories(src)
add_executable(your-binary src/your-code.c src/your-code.h)
target_link_libraries(your-binary opengl)
# target_link_libraries will search for libopengl on standard system paths,
# maybe the library is not called libopengl, then you have to adjust the name above
this cmake project will generate the binary for you and link it against opengl