Use existing CMake library in a new project including its headers - c++

I'm working on a project where I have an existing CMake library (https://github.com/OpenEtherCATsociety/SOEM) stored in project/lib/SOEM/.
The source for my application lives in project/src/
I have a root CMakeLists.txt which adds both subdirectories.
Now my question is how do I link the SOEM library to my executable and also use its headers without relative paths?
SOEM contains its own CMakeLists.txt which I would preferably not touch at all if that's possible. It builds a library and installs it together with its headers, however, I don't know how to make use of the latter.
cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules")
project(SOEM C)
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
# Default to installing in SOEM source directory
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install)
endif()
set(SOEM_INCLUDE_INSTALL_DIR include/soem)
if(WIN32)
set(OS "win32")
include_directories(oshw/win32/wpcap/Include)
link_directories(${CMAKE_SOURCE_DIR}/oshw/win32/wpcap/Lib)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_NO_WARNINGS")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
set(OS_LIBS wpcap.lib Packet.lib Ws2_32.lib Winmm.lib)
elseif(UNIX)
set(OS "linux")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
set(OS_LIBS pthread rt)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "rt-kernel")
set(OS "rtk")
message("ARCH is ${ARCH}")
message("BSP is ${BSP}")
include_directories(oshw/${OS}/${ARCH})
file(GLOB OSHW_EXTRA_SOURCES oshw/${OS}/${ARCH}/*.c)
set(OSHW_SOURCES "${OS_HW_SOURCES} ${OSHW_ARCHSOURCES}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-but-set-variable")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format")
set(OS_LIBS "-Wl,--start-group -l${BSP} -l${ARCH} -lkern -ldev -lsio -lblock -lfs -lusb -llwip -leth -li2c -lrtc -lcan -lnand -lspi -lnor -lpwm -ladc -ltrace -lc -lm -Wl,--end-group")
endif()
message("OS is ${OS}")
file(GLOB SOEM_SOURCES soem/*.c)
file(GLOB OSAL_SOURCES osal/${OS}/*.c)
file(GLOB OSHW_SOURCES oshw/${OS}/*.c)
file(GLOB SOEM_HEADERS soem/*.h)
file(GLOB OSAL_HEADERS osal/osal.h osal/${OS}/*.h)
file(GLOB OSHW_HEADERS oshw/${OS}/*.h)
include_directories(soem)
include_directories(osal)
include_directories(osal/${OS})
include_directories(oshw/${OS})
add_library(soem STATIC
${SOEM_SOURCES}
${OSAL_SOURCES}
${OSHW_SOURCES}
${OSHW_EXTRA_SOURCES})
target_link_libraries(soem ${OS_LIBS})
install(TARGETS soem DESTINATION lib)
install(FILES
${SOEM_HEADERS}
${OSAL_HEADERS}
${OSHW_HEADERS}
DESTINATION ${SOEM_INCLUDE_INSTALL_DIR})
add_subdirectory(test/linux/slaveinfo)
add_subdirectory(test/linux/eepromtool)
add_subdirectory(test/linux/simple_test)

Related

How to use intel opencl sdk in Clion in windows 10?

I have installed Intel opencl sdk for windows and opencl variable are added to the environment variables. I want to use this sdk with my Clion ide which I am unable to include it in my current project since it was CL/cl.hpp not found. How can add it to my project in Clion?
Cl/cl.hpp is located atC:\Program Files(x86)\IntelSWTools\OpenCL\sdk\include\CL
following is my CMakeLists.txt
project(tpch_framework)
# enable c++11
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_BUILD_TYPE "Release")
find_package(OpenMP REQUIRED)
find_package(OpenCL REQUIRED)
if (OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
# Configure required Boost libraries
set(BOOST_ROOT "" CACHE PATH "Boost build root (useful on Windows)")
option(Boost_USE_STATIC_LIBS
"Search for static boost libs" OFF)
option(Boost_USE_MULTITHREADED
"Search for multithreaded boost libs" ON)
option(Boost_USE_STATIC_RUNTIME
"Search for boost libs linked against static C++ runtime" OFF)
find_package(Boost 1.47.0 REQUIRED filesystem system)
# ensure that dependant libraries not explicitly specified here
# are found by the linker:
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
set(LIBS ${LIBS} ${Boost_LIBRARIES})
#Bring the headers into the project
include_directories(include)
FILE(GLOB_RECURSE INC_ALL "include/*.hpp")
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
add_library(tpch_framework ${SOURCES})
add_executable(framework main.cpp ${INC_ALL})
target_link_libraries(framework tpch_framework)
#target_link_libraries(framework stdc++fs)
target_link_libraries(framework ${LIBS})
You need to provide information about include directory for OpenCL headers like you provided for Boost headers. Also, you need to link OpenCL libraries with your target.
In your CMakeLists...
For include and link directories:
link_directories(${Boost_LIBRARY_DIRS} ${OpenCL_LIBRARY})
include_directories(${Boost_INCLUDE_DIRS} ${OpenCL_INCLUDE_DIRS})
For linking libraries:
set(LIBS ${LIBS} ${Boost_LIBRARIES} ${OpenCL_LIBRARY})

How to modify CMakelists for Qt5 from Windows to Linux?

(problem already solved)
It will compile if just $qtcreator openglwindow/openglwindow.pro for Qt5-OpenGL-example(http://doc.qt.io/qt-5/qtgui-openglwindow-example.html)
My CMakelist can be build on Win10, but on linux fail, the command is:
[user]$ mkdir build && cd build && cmake .. && make
it outputs error as follow:
Scanning dependencies of target QtGL_test_automoc
[ 20%] Automatic mocfor target QtGL_test
Generating moc source QtGL_test_automoc.dir/moc_openglwindow_LRW7M26ARLGWDK.cpp
Generating moc compilation QtGL_test_automoc.cpp
[ 20%] Built target
QtGL_test_automoc Scanning dependencies of target QtGL_test
[ 40%] Building CXX object CMakeFiles/QtGL_test.dir/main.cpp.o
c++: fatal error: no input files compilation terminated.
/bin/sh:-DQT_CORE_LIB: command not found
make[2]: *** [CMakeFiles/QtGL_test.dir/build.make:63: MakeFiles/QtGL_test.dir/main.cpp.o] Error 127
make[1]: ***[CMakeFiles/Makefile2:68: CMakeFiles/QtGL_test.dir/all] Error 2 make:
*** [Makefile:84: all] Error 2
I'd tried to figure out how to modify a cross-platform version for linux.
Here is my CMakeLists.txt:
cmake_minimum_required (VERSION 3.5)
set(PRROJ_NAME QtGL_test)
project (${PRROJ_NAME})
set(HEADERS ${HEADERS} openglwindow.h )
set(SOURCES ${SOURCES} main.cpp ${HEADERS})
set(CMAKE_AUTOMOC ON)
add_executable(${PRROJ_NAME} ${SOURCES})
set(PROJ_INCLUDE_DIRS ${PROJ_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR})
add_library(openglwindow openglwindow.cpp)
set(PROJ_LIBRARIES ${PROJ_LIBRARIES} openglwindow)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
if(WIN32)
find_path(CMAKE_PREFIX_PATH NAMES lib/cmake/Qt5/Qt5Config.cmake HINTS
"$ENV{Qt5_ROOT}"
)
message("Qt5_ROOT is $ENV{Qt5_ROOT}")
endif()
message("CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}")
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
set(NEED_QT_COMPONENTS QTCORE QTGUI)
message("finding ${NEED_QT_COMPONENTS}")
foreach(COMPONENT Gui Core)
message("find_package(Qt5${COMPONENT})")
find_package("Qt5${COMPONENT}")
message("Qt5${COMPONENT}_FOUND is ${Qt5${COMPONENT}_FOUND}")
if("${Qt5${COMPONENT}_FOUND}")
set(PROJ_INCLUDE_DIRS ${PROJ_INCLUDE_DIRS} "${Qt5${COMPONENT}_INCLUDE_DIRS}")
set(PROJ_LIBRARIES ${PROJ_LIBRARIES} "Qt5::${COMPONENT}")
add_definitions("${Qt5${COMPONENT}_DEFINITIONS}")
qt5_use_modules(${PRROJ_NAME} ${COMPONENT})
endif()
endforeach(COMPONENT )
message("include_directories(${PROJ_INCLUDE_DIRS})")
include_directories(${PROJ_INCLUDE_DIRS})
message("target_link_libraries(${PRROJ_NAME} ${PROJ_LIBRARIES})")
target_link_libraries(${PRROJ_NAME} ${PROJ_LIBRARIES})
set(CMAKE_CXX_STANDARD 14)
if(UNIX)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/local/include -Wall")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib")
endif()
finally solved version:
cmake_minimum_required (VERSION 3.5)
set(PRROJ_NAME QtGL_test)
project (${PRROJ_NAME})
set(HEADERS ${HEADERS} openglwindow.h )
set(SOURCES ${SOURCES} main.cpp ${HEADERS})
set(CMAKE_AUTOMOC ON)
add_executable(${PRROJ_NAME} ${SOURCES})
set(PROJ_INCLUDE_DIRS ${PROJ_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR})
add_library(openglwindow openglwindow.cpp)
set(PROJ_LIBRARIES ${PROJ_LIBRARIES} openglwindow)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
if(WIN32)
find_path(CMAKE_PREFIX_PATH NAMES lib/cmake/Qt5/Qt5Config.cmake HINTS
"$ENV{Qt5_ROOT}"
)
message("Qt5_ROOT is $ENV{Qt5_ROOT}")
endif()
message("CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}")
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
set(QT_FLAGS "-fPIC")
set(NEED_QT_COMPONENTS QTCORE QTGUI)
message("finding ${NEED_QT_COMPONENTS}")
foreach(COMPONENT Gui Core)
message("find_package(Qt5${COMPONENT})")
find_package("Qt5${COMPONENT}")
message("Qt5${COMPONENT}_FOUND is ${Qt5${COMPONENT}_FOUND}")
if("${Qt5${COMPONENT}_FOUND}")
set(PROJ_INCLUDE_DIRS ${PROJ_INCLUDE_DIRS} "${Qt5${COMPONENT}_INCLUDE_DIRS}")
set(PROJ_LIBRARIES ${PROJ_LIBRARIES} "Qt5::${COMPONENT}")
#Open add_definitions will error
#add_definitions("${Qt5${COMPONENT}_DEFINITIONS}")
qt5_use_modules(${PRROJ_NAME} ${COMPONENT})
endif()
endforeach(COMPONENT )
message("include_directories(${PROJ_INCLUDE_DIRS})")
include_directories(${PROJ_INCLUDE_DIRS})
message("target_link_libraries(${PRROJ_NAME} ${PROJ_LIBRARIES})")
target_link_libraries(${PRROJ_NAME} ${PROJ_LIBRARIES})
set(CMAKE_CXX_STANDARD 14)
if(UNIX)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${QT_FLAGS} -I/usr/local/include -Wall")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib")
endif()

Cmake shared library - include doesn't exist

I am trying to write a cmake file to create a shared library and use it in the executable file in my project. In the main I include heades as <pattern_follower/header_file_name.h>, but when I run make install it says pattern_follower/header_file_name.h does not exist.
cmake_minimum_required(VERSION 2.8)
project(pattern_follower)
add_definitions(-std=c++14)
file(GLOB SOURCES src/*)
file(GLOB HEADERS include/*)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_RELEASE} -Wall -O4 -march=native -Wfatal-errors")
find_package(OpenCV REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(${PROJECT_NAME} SHARED ${SOURCES})
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS})
add_executable(${PROJECT_NAME}-main main.cpp)
set_target_properties(${PROJECT_NAME}-main PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
target_link_libraries( ${PROJECT_NAME}-main ${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
install(TARGETS ${PROJECT_NAME}-main DESTINATION bin)
install(DIRECTORY include DESTINATION include/${PROJECT_NAME})

issue related to cmake and openmp

I am trying to compile a program on QT creator with cmake. The code(its not my code, I am using it to test something) uses openMP. in my cmake file I have:
`enter code here`project(dbscan_try)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
SET(CMAKE_CXX_FLAGS "{CMAKE_CXX_FLAGS} -std=c++11 -Wall -fPIC -pedantic -fopenmp -mtune=corei7-avx")
set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) add_definitions(-DBOOST_UBLAS_NDEBUG) add_definitions("-std=c++11") add_definitions("-fopenmp")
add_definitions("-lgomp")
OPTION (USE_OpenMP "Use OpenMP" ON)
find_package(OpenMP)
if (OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp")
find_package(PCL REQUIRED COMPONENTS) include_directories(${PCL_INCLUDE_DIRS} ) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS})
include_directories({/home/raman/Work/dbscan_try})
set(project_SOURCES
/home/raman/Work/cplusplus_projects/dbscan_try/main.cpp
/home/raman/Work/cplusplus_projects/dbscan_try/dbscan.cpp )
set(project_HEADERS
/home/raman/Work/cplusplus_projects/dbscan_try/dbscan.h)
set(OpenCV_INCLUDE_DIRS "${OpenCV_INSTALL_PATH}/include/opencv;${OpenCV_INSTALL_PATH}/include")
find_package(OpenCV 3.1.0 REQUIRED COMPONENTS) include_directories(-L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_saliency -lboost_system -lopencv_contrib) set( CMAKE_CXX_FLAGS "-g -Wall")
add_executable(${PROJECT_NAME} ${project_SOURCES})
target_link_libraries(dbscan_try ${PCL_LIBRARIES} ${OpenCV_LIBS} )
However, I am getting errors such as:
/home/raman/Work/cplusplus_projects/dbscan_try/dbscan.cpp:88: error: undefined reference to omp_set_num_threads'
/home/raman/Work/cplusplus_projects/dbscan_try/dbscan.cpp:89: error: undefined reference toGOMP_parallel_start'
etc.
I am new to C++ and cmake so I am not sure if I am doing somethign wrong here. I seem to have followed what other suggestions here mentioned and it worked for them.

Building dependencies with Cmake

I'm new to CMake and I'm trying to figure out how to build dependencies. My project folders are organized like this:
Scrubber
-- FileIO
-- CDEs
-- Utilities
-- Scrubber
FileIO, CDEs, and Utilities are static libraries that are used by the executable in Scrubber.
I want to be able to execute a single make command from the top dir that will build everything. If I build each library independently, then it all comes together fine when I execute the top make. But if I don't do that it ahead of time then it won't build the dependencies and, not surprisingly, complains that the libraries weren't found.
So very simple question: how do I cause the system to build the libraries?
TOP LEVEL CMAKELISTS IN SCRUBBER
cmake_minimum_required(VERSION 3.2)
project(Scrubber)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_subdirectory(FileIO)
add_subdirectory(CDEs)
add_subdirectory(Utilities)
add_subdirectory(Scrubber)
FILEIO CMAKELISTS IN SCRUBBER/FILEIO
cmake_minimum_required(VERSION 3.2)
project(FileIO)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
file(GLOB SOURCE_FILES *.cpp)
add_library(FileIO STATIC ${SOURCE_FILES})
#Don't prepend with "lib"
set_target_properties(FileIO PROPERTIES PREFIX "")
CDES CMAKELISTS IN SCRUBBER/CDES
cmake_minimum_required(VERSION 3.2)
project(CDEs)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_INCLUDE_DIR "/usr/lib/boost_1_60_0")
include_directories( ${BOOST_INCLUDE_DIR} )
file(GLOB SOURCE_FILES *.cpp)
add_library(CDEs STATIC ${SOURCE_FILES})
#Don't prepend with "lib"
set_target_properties(CDEs PROPERTIES PREFIX "")
UTILITIES CMAKELIST IN SCRUBBER/UTILITIES
cmake_minimum_required(VERSION 3.2)
project(Utilities)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_INCLUDE_DIR "/usr/lib/boost_1_60_0")
include_directories( ${BOOST_INCLUDE_DIR} )
file(GLOB SOURCE_FILES *.cpp)
add_library(Utilities STATIC ${SOURCE_FILES})
#Don't prepend with "lib"
set_target_properties(Utilities PROPERTIES PREFIX "")
SCRUBBER CMAKELIST IN SCRUBBER/SCRUBBER
cmake_minimum_required(VERSION 3.2)
project(Scrubber)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_INCLUDE_DIR "/usr/lib/boost_1_60_0")
set(PROJECT_INCLUDE_DIR "..")
include_directories(${BOOST_INCLUDE_DIR} ${PROJECT_INCLUDE_DIR})
find_library(FILEIO_LIB FileIO.a HINTS ../FileIO/)
find_library(CDES_LIB CDEs.a HINTS ../CDEs/)
find_library(UTILITIES_LIB Utilities.a HINTS ../Utilities/)
file(GLOB SOURCE_FILES *.cpp)
add_executable(Scrubber ${SOURCE_FILES})
target_link_libraries(Scrubber ${FILEIO_LIB} ${CDES_LIB} ${UTILITIES_LIB})
This is wrong:
target_link_libraries(Scrubber ${FILEIO_LIB} ${CDES_LIB} ${UTILITIES_LIB})
if you have library add_library(FileIO STATIC ${SOURCE_FILES}), then you should
write:
target_link_libraries(Scrubber FileIO )
cmake find out that this is also target, and create make file with proper dependencies, and you not have to point any dependencies with add_dependency,
cmake do it for automatically. At least current for me cmake version 3.5
CMake includes the add_dependency command that does exactly what you need.
Make a top-level depend on other top-level targets to ensure that they build before does.