Cmake file for C++/CUDA project - c++

I'm having trouble compiling my application using cmake and make. The source files of the project are organized as follows:
SOURCE/
CMakeLists.txt
myApp.cc
include/
classA.hh
classB.hh
src/
classA.cc
classB.cc
classB.cu
My CMakeLists.txt file is as follows:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(myApp)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
FIND_PACKAGE(GDCM REQUIRED)
IF(GDCM_FOUND)
INCLUDE(${GDCM_USE_FILE})
SET(GDCM_LIBRARIES gdcmCommon vtkgdcm)
ELSE(GDCM_FOUND)
MESSAGE(FATAL_ERROR "Cannot find GDCM, did you set GDCM_DIR?")
ENDIF(GDCM_FOUND)
SET(CUDA_TOOLKIT_ROOT_DIR="/Developer/NVIDIA/CUDA-7.5/")
FIND_PACKAGE(CUDA REQUIRED)
SET(CUDA_PROPAGATE_HOST_FLAGS ON)
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -gencode arch=compute_30,code=sm_30)
SET(LIB_TYPE SHARED)
SET(CUDA_SEPARABLE_COMPILATION ON)
LINK_DIRECTORIES(/lib/FFTW/INSTALL/lib)
INCLUDE_DIRECTORIES(/lib/FFTW/INSTALL/include)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
FILE(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)
FILE(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
FILE(GLOB cudafile ${PROJECT_SOURCE_DIR}/src/*.cu)
CUDA_ADD_EXECUTABLE(myApp myApp ${headers} ${sources} ${cudafile})
TARGET_LINK_LIBRARIES(myApp ${VTK_LIBRARIES} ${GDCM_LIBRARIES} fftw3)
When I try to compile the project using make (after successfully running cmake) I get:
nvcc fatal : A single input file is required for a non-link phase when an outputfile is specified
CMake Error at myApp_generated_classB.cu.o.cmake:207 (message):
Error generating
../BUILD/CMakeFiles/myApp.dir/src/./myApp_generated_classB.cu.o
Is breaking up the source file of a class into .cc and .cu files problematic?

This is not a comprehensive explanation of what exactly causes the problem stated in the question; nevertheless it solves the problem in a fairly satisfactory way.
First, apparently there is a conflict between using FIND_PACKAGE(VTK) (and hence FIND_PACKAGE(GDCM) which seems to require VTK CMake files for vtkgdcm) and nvcc. This has been recently reported on Mantis. To avoid this conflict, I use:
LINK_DIRECTORIES( {VTK_Directory}/INSTALL/lib)
INCLUDE_DIRECTORIES({VTK_Directory}/INSTALL/include/vtk-6.2)
LINK_DIRECTORIES( {GDCM_Directory}/INSTALL/lib)
INCLUDE_DIRECTORIES({GDCM_Directory}/INSTALL/include/gdcm-2.4)
instead of,
FIND_PACKAGE(VTK REQUIRED)
FIND_PACKAGE(GDCM REQUIRED)
Second, as for the CUDA part of the project, I put everything into a .cu file and use CUDA_COMPILE(cuda_o myCUDAstudff.cu) to create an object file. Then I use the native C++ compiler to create an executable as usual using ADD_EXECUTABLE( ... ${cuda_o}). Since I am using the native C++ compiler as opposed to nvcc, I need to include the following header files in my kernel (myCUDAstudff.cu) file:
#include <cuda.h>
#include <cuda_runtime.h>
and also link to libcudart in TARGET_LINK_LIBRARIES(), for which I used the shared library. I couldn't figure out a way to do the same with libcudart_static.a though.
Alternatively, one can use CUDA_ADD_EXECUTABLE(... myCUDAstudff.cu) instead of all the above steps (i.e., CUDA_COMPILE(), ADD_EXECUTABLE(),...).

Related

pybind11 and another libs in one project [duplicate]

I want to be able to call my C++ code as a python package. To do this I am using pybind11 with CMakelists (following this example https://github.com/pybind/cmake_example). My problem is that I have to include GSL libraries in the compilation of the code, and these need an explicit linker -lgsl .
If I were just to compile and run the C++ without wrapping it with python, the following Cmakelists.txt file does the job
cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
project(myProject)
add_executable(
myexecutable
main.cpp
function1.cpp
)
find_package(GSL REQUIRED)
target_link_libraries(myexecutable GSL::gsl GSL::gslcblas)
but when using pybind11 the template I found doesn't allow the add_executable therefore target_link_libraries doesn't work.
I have trie this
project(myProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES) # See below (1)
# Set source directory
set(SOURCE_DIR "project")
# Tell CMake that headers are also in SOURCE_DIR
include_directories(${SOURCE_DIR})
set(SOURCES "${SOURCE_DIR}/functions.cpp")
# Generate Python module
add_subdirectory(lib/pybind11)
pybind11_add_module(namr ${SOURCES} "${SOURCE_DIR}/bindings.cpp")
FIND_PACKAGE(GSL REQUIRED)
target_link_libraries(GSL::gsl GSL::gslcblas)
but this produces errors in the building.
Any idea ?
Function pybind11_add_module creates a library target, which can be used for link added module with other libraries:
pybind11_add_module(namr ${SOURCES} "${SOURCE_DIR}/bindings.cpp")
target_link_libraries(namr PUBLIC GSL::gsl GSL::gslcblas)
This is explicitely stated in documentation:
This function behaves very much like CMake’s builtin add_library (in fact, it’s a wrapper function around that command). It will add a library target called <name> to be built from the listed source files. In addition, it will take care of all the Python-specific compiler and linker flags as well as the OS- and Python-version-specific file extension. The produced target <name> can be further manipulated with regular CMake commands.

gtest setup cmake to get it run

I want to include gtest to my C++ project. I am using Clion as IDE, which should work. Some tests are already working, but I cannot use any functions from B_RocChoice.h. It says that the function is not declared in this scope.
Can somebody tell me what I am doing wrong? How I must change my CMakeLists.txt files that it recogizes my methods?
This is my basic_tests.cpp, where my testcases will be written.
This is my Directory.
Here, the most outer CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(cli)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(SOURCE_FILES
include/A_WowbaggerChoice.h
include/AbstractChoice.h
include/B_RocChoice.h
include/C_CnnChoice.h
include/D_DetectorChoice.h
include/E_LearningChoice.h
include/Help.h
include/MyException.h
include/StartScreen.h
include/tinyxml.h
include/types.h
src/A_WowbaggerChoice.cpp
src/AbstractChoice.cpp
src/B_RocChoice.cpp
src/C_CnnChoice.cpp
src/D_DetectorChoice.cpp
src/E_LearningChoice.cpp
src/Help.cpp
src/main.cpp
src/MyException.cpp
src/StartScreen.cpp
tinyxml/tinystr.cpp
tinyxml/tinystr.h
tinyxml/tinyxml.cpp
tinyxml/tinyxml.h)
add_subdirectory(googletest)
add_executable(cli ${SOURCE_FILES})
target_link_libraries( cli ${OpenCV_LIBS} )
CMakeLists.txt for gtest.
cmake_minimum_required(VERSION 2.6.2)
project( googletest-distribution )
enable_testing()
option(BUILD_GTEST "Builds the googletest subproject" ON)
#Note that googlemock target already builds googletest
option(BUILD_GMOCK "Builds the googlemock subproject" OFF)
if(BUILD_GMOCK)
add_subdirectory( googlemock )
elseif(BUILD_GTEST)
add_subdirectory( googletest )
endif()
add_subdirectory(basic_tests)
CMakeLists.txt for basic_tests
include_directories($(gtest_SOURCE_DIR}/include
${getest_SOURCE_DIR}))
#include_directories(../../src/)
include_directories(../../include/)
add_executable(runBasicCli
basic_check.cpp)
target_link_libraries(runBasicCli gtest gtest_main)
#target_link_libraries(cli)
I'm assuming your compiler is complaining it can't find the B_RocChoices.h header? Your question seems to imply the compiler error is about not finding a function, but B_RocChoices is a header and not a function in your basic_tests.cpp file.
Assuming your problem is that the compiler isn't finding the B_RocChoices.h header, I expect that when you include_directories(../../include) you are wanting to make the directory where B_RocChoices.h resides part of the header search path. This is a relative path, so it depends where the compiler is being run from as to what path it means (it wouldn't work if you were doing out of source builds, for example). Try using either CMAKE_SOURCE_DIR or CMAKE_CURRENT_SOURCE_DIR to define the path unambiguously. For example:
include_directories(${CMAKE_SOURCE_DIR}/include)
If you are using CMake 2.8.11 or later, I'd recommend you consider using target_include_directories() instead and probably also read up on target_link_libraries(). Together, these allow you to make the header search paths and linked libraries specific to a target rather than global to all targets. Lastly, if you prefer to download GoogleTest as part of your build rather than embedding it directly in your project sources, you may find the following link useful:
https://crascit.com/2015/07/25/cmake-gtest/

How to include library headers in CMakeLists

I'm having trouble getting CMake and Make to find external header files I need to compile my code.
My code has is trying to use a header provided by the external aruco library
#include <aruco.h>
And when I try to compile I get
/home/ncr/ncr_ws/src/aruco_ros/src/aruco_ros_node.cpp:21:19: fatal error: aruco.h: No such file or directory
#include <aruco.h>
^
compilation terminated.
My CMakeLists.txt has:
cmake_minimum_required(VERSION 2.8.3)
project(aruco_ros)
set(CMAKE_MODULE_PATH /usr/local/lib/cmake)
message( STATUS "CMAKE_MODULE_PATH: " ${CMAKE_MODULE_PATH})
find_package(OpenCV REQUIRED)
find_package(aruco REQUIRED)
...
include_directories(
${OpenCV_INCLUDE_DIRS}
${aruco_INCLUDE_DIRS}
)
message(STATUS "OpenCV_INCLUDE_DIRS: " ${OpenCV_INCLUDE_DIRS})
message(STATUS "aruco_INCLUDE_DIRS: " ${aruco_INCLUDE_DIRS})
The Findaruco.cmake file is in /usr/local/lib/cmake/Findaruco.cmake
The Findaruco.cmake file sets the aruco_INCLUDE_DIRS variable as /usr/local/include and the include file aruco.h is in /usr/local/include/aruco. Does make not search subdirectories? Most of the opencv headers are in /usr/local/include/opencv2 but the OpenCV_INCLUDE_DIRS only declares /usr/local/include and yet all opencv the headers are found without any problem.
As James Adkison said, I needed to properly reference the header files. Looking at the other opencv include files, I start them all off with #include <opencv2/[blah]>, which explains why those work as well.

Include Eigen library for Xcode project via CMake/CMakeLists.txt

I've got the following CMakeLists.txt (in my "project" folder) file for my project.
# define new project
PROJECT(SETUPMARKERTEST)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR)
if(UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif(UNIX)
# Set static build for GLFW
SET(BUILD_SHED_LIBS OFF)
ADD_SUBDIRECTORY(ext/glfw-3.1.1)
# Set shared lib build for the rest
SET(BUILD_SHARED_LIBS ON)
# Find dependencies
SET(EIGEN_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/ext/Eigen-3.1.2")
FIND_PACKAGE(OpenCV REQUIRED)
# Set header and source files
SET(MAR_Test_SOURCES
src/main.cpp
src/MarkerTracker.h src/MarkerTracker.cpp
src/PoseEstimation.h src/PoseEstimation.cpp
)
# define executable
ADD_EXECUTABLE(${PROJECT_NAME} ${MAR_Test_SOURCES})
# define additional include directories and linking targets
INCLUDE_DIRECTORIES("ext/glfw-3.1.1/include" ${EIGEN_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS} glfw ${OPENGL_glu_LIBRARY} ${GLFW_LIBRARIES})
And my Eigen folder is in "project/ext/Eigen/3.1.2/Eigen/".
Somehow when I created my project for Xcode (in "project/buildXcode/" with Cmake .. -G "Xcode") and run it, Xcode throws me the error:
So I guess there is some error in my CMakeLists.txt, unfortunately I received that file and I'm new to CMake and thus didn't write it on my own nor am I very skilled with CMake.
Do you know what causes the error and can you fix the CMakeLists.txt that my project runs with the Eigen library?
Unfortunately it looks like windows is having no problem with this, whereas mac is bleating.
You just have to use
#include <Eigen/Dense>
instead of
#include <Eigen\Dense>
...pretty stupid error.

Can I make a library from multiple targets?

I'm trying to learn cmake and have started converting an old make project over to cmake. Here is a simplified version of the directory structure I now have:
CMakeLists.txt
src/
CMakeLists.txt
main.cpp
core/
CMakeLists.txt
/sourcecode, other cmakes, etc.
test/
CMakeLists.txt
someTest.cpp
Currently, in my root CMakeLists.txt file I simply have this:
cmake_minimum_required(VERSION 2.8)
project(all)
add_subdirectory(src)
add_subdirectory(test)
What I wanted to do, was have a library created by core/CMakeLists.txt that can be used by both src/CMakeLists.txt to build the main executable, but also loaded by test/CMakeLists to build the unit tests.
So my src/core/CMakeLists.txt file currently looks sort of like this:
cmake_minimum_required(VERSION 2.8)
project(core)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wpedantic -Wreorder -DBOOST_TEST_DYN_LINK -DBOOST_LOG_DYN_LINK ")
#some other directories in my core code:
add_subdirectory(display)
add_subdirectory(training)
add_subdirectory(utility)
#some packages I use...
find_package(Boost 1.55.0
COMPONENTS
log
program_options
serialization
thread
system
filesystem
REQUIRED)
find_package(GLUT REQUIRED)
find_package(OpenGL REQUIRED)
find_package(Eigen3 REQUIRED)
include_directories(
${PROJECT_SOURCE_DIR}
${EIGEN3_INCLUDE_DIR})
target_link_libraries(core
display
training
utility
${Boost_LIBRARIES}
${OPENGL_LIBRARIES}
${GLUT_LIBRARY}
${OpenMP_LIBRARIES})
So the idea is that I now have a core target I can simply link against to run my tests, and everything should work. However, when I try to build main, for example, I get:
Cannot specify link libraries for target "core" which is not built by this
project.
I thought this might be because core doesn't have a add_library command, but if I add add_library(core) I get this error:
You have called ADD_LIBRARY for library core without any source files. This typically indicates a problem with your CMakeLists.txt file
But I don't want to add any source files; I just want this target to link the targets in the core directory and produce a target I can link against from test.
Clearly I'm missing some core knowledge here, either with cmake or the toolchain itself. Help is appreciated :)
If you only want to create a core target without source files, you need to declare it like an INTERFACE target. So, try to add the following code to your src/core/CMakeLists.txt:
cmake_minimum_required(VERSION 3.0) # REQUIRED 3.x.x version
project(core)
...
# Here declare your core_interface target
add_library(core_interface INTERFACE)
target_link_libraries(core_interface INTERFACE
display
training
utility
${Boost_LIBRARIES}
${OPENGL_LIBRARIES}
${GLUT_LIBRARY}
${OpenMP_LIBRARIES})
As you can see, if you make this, you'll need to upgrade your CMake installed version.
Then, you'll build your tests or any executable, linking with this interface target directly:
add_executable(main main.cpp)
target_link_libraries(main core_interface)