Unable to compile example from OpenCL C++ Bindings Documentation (cl2.hpp) - c++

I am trying to learn OpenCL2 with C++.
I'm using kubuntu 16.04.1 LTS and beignet from repository.
My laptop is a lenovo with a intel i5-5200U without nvidia or similar.
The command clinfo recognize the platform.
The first bug i found in the example is the variable output2 that isn't declared i have tried to comment it, but still get many link error...
The example is this http://github.khronos.org/OpenCL-CLHPP/index.html#example
This is a minimal example that reproduce a part of link error:
main.cpp:
// Defines the target OpenCL runtime version to build the header against.
// Defaults to 200, representing OpenCL 2.0.
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include <CL/cl2.hpp>
int main()
{
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 11) # C++11...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11
project(exampleopencl2)
add_executable(exampleopencl2 main.cpp)
install(TARGETS exampleopencl2 RUNTIME DESTINATION bin)
I use KDevelop and i get this output when i try to compile this code:
/home/deglans/ExampleOpenCL2/build> make -j2 mytests
Scanning dependencies of target mytests
[ 50%] Building CXX object CMakeFiles/mytests.dir/main2.cpp.o
[100%] Linking CXX executable mytests
CMakeFiles/mytests.dir/main2.cpp.o: In function `cl::detail::ReferenceHandler<_cl_device_id*>::release(_cl_device_id*)':
/usr/include/CL/cl2.hpp:1438: undefined reference to `clReleaseDevice'
CMakeFiles/mytests.dir/main2.cpp.o: In function `cl::detail::ReferenceHandler<_cl_context*>::release(_cl_context*)':
/usr/include/CL/cl2.hpp:1473: undefined reference to `clReleaseContext'
CMakeFiles/mytests.dir/main2.cpp.o: In function `cl::detail::ReferenceHandler<_cl_command_queue*>::release(_cl_command_queue*)':
/usr/include/CL/cl2.hpp:1482: undefined reference to `clReleaseCommandQueue'
collect2: error: ld returned 1 exit status
CMakeFiles/mytests.dir/build.make:94: recipe for target 'mytests' failed
make[3]: *** [mytests] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/mytests.dir/all' failed
make[2]: *** [CMakeFiles/mytests.dir/all] Error 2
CMakeFiles/Makefile2:116: recipe for target 'CMakeFiles/mytests.dir/rule' failed
make[1]: *** [CMakeFiles/mytests.dir/rule] Error 2
Makefile:175: recipe for target 'mytests' failed
make: *** [mytests] Error 2
*** Errore: Codice di uscita 2 ***

I have solved the problem by adding target_link_libraries(exampleopencl2 OpenCL) in the CMakeLists.txt file.

Related

Undefined Reference error with RGBD.hpp file from opencv

I've got a problem at the moment...
I've got a cmake file that includes the local computer files.
cmake_minimum_required(VERSION 3.10)
project(testproject)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread -Wall -fPIC -Ofast -fopenmp")
link_directories(/usr/local/lib/cmake)
set(CMAKE_BUILD_TYPE "Debug")
find_package(OpenCV REQUIRED)
set(LIBRARY_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Libraries/include)
set(LIBRARY_USR_INCLUDE_DIR /usr/local/include)
add_executable(testproject
"base.cpp"
"Libraries/include/base64/base64.h"
"Libraries/include/base64/base64.cpp"
)
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${LIBRARY_USR_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS} ${LIBRARIES} ${LIBRARY_INCLUDE_DIR})
target_link_libraries(${CMAKE_PROJECT_NAME} "${OpenCV_LIBS}" stdc++fs)
The include file within CMAKE works because there are other files with opencv included and those are found..
Only the weird problem is that the rgbd.hpp (File is under /usr/local/include/) is found and also the function but when i'm making the the file with.
Terminal Process:
make -j7
ERROR RESULT:
[ 15%] Linking CXX executable count
CMakeFiles/count.dir/base.cpp.o: In function `main':
/home/testpc/Documents/Test/base.cpp:10: undefined reference to `cv::rgbd::DepthCleaner::DepthCleaner(int, int, int)'
collect2: error: ld returned 1 exit status
CMakeFiles/count.dir/build.make:374: recipe for target 'count' failed
make[2]: *** [count] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/count.dir/all' failed
make[1]: *** [CMakeFiles/count.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
make: *** [all] Error 2
That this happens when all the other files are already have been compiled.
Problem class: Base.cpp
#include "opencv2/rgbd.hpp"
int main()
{
rgbd::DepthCleaner * depthc = new rgbd::DepthCleaner(CV_16U, 7, rgbd::DepthCleaner::DEPTH_CLEANER_NIL);
}
Possible Tried Solutions:
Still having the same result, When i remove the content within the main method and replace it with cout << "Test" << endl; It builds and runs as it suppose to be. Also works when I create a new raw Material blanko with standard Opencv core included.
It only does not build with the RGBD Include and instance creation.
(RGDB.hpp does not stand under /usr/local/inlclude) but under /usr/include. So i removed the local part but still responded as the same current issue. Any other possible solution?

Cmake Fails to Link DL Library

Below is the CMakeLists I currently have however, I am having issues trying to get it to build and link with the dl library. The target_link_libraries statement did not seem to be effective in doing that (it got rid of the undefined reference errors). I then tried adding the linker flags however, it seems to error out with the following:
[build] g++-9: error: ;-Wl,--no-as-needed: No such file or directory
[build] make[2]: *** [CMakeFiles/FIFO_Queue_Testbench.dir/build.make:86: FIFO_Queue_Testbench] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:141: CMakeFiles/FIFO_Queue_Testbench.dir/all] Error 2
[build] make: *** [Makefile:84: all] Error 2
[build] Build finished with exit code 2
I am pretty new to using meta build systems like CMake so if you could breakdown what was incorrect and why, I would appreciate the understanding. Thank you for any help in advance!
If it's of any importance I am on Ubuntu 20.04 using vscode.
cmake_minimum_required(VERSION "3.16")
project("My_CPP_Code" VERSION "0.0")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(MY_LINKER_FLAGS "-Wl,--no-as-needed -ldl")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}" "${MY_LINKER_FLAGS}")
set(XILINX_XSI_INCLUDE_DIR
"/tools/Xilinx/Vivado/2020.2/data/xsim/include"
)
set(XILINX_HEADER_FILES
"${XILINX_XSI_INCLUDE_DIR}/xsi.h"
)
include_directories("${XILINX_XSI_INCLUDE_DIR}")
add_subdirectory("Data_Structures")
add_subdirectory("Hardware_Models")
add_subdirectory("Xilinx")
set (XILINX
${XILINX_HEADER_FILES}
${XILINX_LOCAL_LIB}
)
add_executable("Main_App" "Applications/Main.cpp" ${XILINX})
add_executable("FIFO_Queue_Testbench" "Hardware_Models/FIFO_Queue_Testbench.cpp" ${XILINX})
target_link_libraries("Main_App"
INTERFACE "Data_Structures")
target_link_libraries("FIFO_Queue_Testbench"
${CMAKE_DL_LIBS}
"XILINX_LOCAL"
"Hardware_Models")

Undefined reference in libfreenect c++ wrapper

I want to print the number of connected devices with libfreenect in c++. As described in https://openkinect.org/wiki/C%2B%2B_Wrapper
i include the libfreenect.hpp header file in my TestKinectConnection.cpp.
My TestKinectConnection.cpp:
#include <iostream>
#include "libfreenect.hpp"
using namespace std;
int main(void) {
Freenect::Freenect nect;
freenect_context *f_ctx;
cout << nect.deviceCount() << endl;
return(0);
}
When i build with cmake --build build -- -j3 the terminal shows
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::Freenect()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0x40): undefined reference to `freenect_init'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0x90): undefined reference to `freenect_select_subdevices'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0xb0): undefined reference to `pthread_create'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::~Freenect()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectD2Ev[_ZN8Freenect8FreenectD5Ev]+0xa5): undefined reference to `pthread_join'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectD2Ev[_ZN8Freenect8FreenectD5Ev]+0xb4): undefined reference to `freenect_shutdown'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::deviceCount()':
TestKinectConnection.cpp:(.text._ZN8Freenect8Freenect11deviceCountEv[_ZN8Freenect8Freenect11deviceCountEv]+0x17): undefined reference to `freenect_num_devices'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::operator()()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectclEv[_ZN8Freenect8FreenectclEv]+0x4f): undefined reference to `freenect_process_events_timeout'
collect2: error: ld returned 1 exit status
CMakeFiles/projektinf.dir/build.make:95: recipe for target '../bin/projektinf' failed
make[2]: *** [../bin/projektinf] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/projektinf.dir/all' failed
make[1]: *** [CMakeFiles/projektinf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
My CMakeLists.txt:
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.10)
# Project's name
project(projektinf)
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
set(PROJECT_SOURCE_DIR ${CMAKE_SOURCE_DIR})
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_SOURCE_DIR}/lib)
# The following folder will be included
include_directories("${PROJECT_SOURCE_DIR}/src/include")
add_executable(projektinf ${PROJECT_SOURCE_DIR}/src/main/TestKinectConnection.cpp)
add_library(libfreenect ${PROJECT_SOURCE_DIR}/src/include/libfreenect.hpp)
target_link_libraries(projektinf PUBLIC libfreenect)
set_target_properties(libfreenect PROPERTIES LINKER_LANGUAGE CXX)
https://github.com/OpenKinect/libfreenect/blob/master/wrappers/cpp/libfreenect.hpp
https://github.com/OpenKinect/libfreenect/blob/master/include/libfreenect.h
In short summary, libfreenect.hpp tries to include libfreenect.h und completely fails.
From your question it is unclear whether you installed libfreenect globally or simply bundled the library with your code.
If you installed it globally, you forgot to tell CMake that the libfreenect target needs to link with libfreenect.so using the -lfreenect linker flag.
Adding the following should fix that:
set_property(TARGET libfreenect PROPERTY INTERFACE_LINK_LIBRARIES -lfreenect)
The proper approach is to make libfreenect an IMPORTED target, as documented in "It's time to do CMake right".
If you bundled the .cpp with your code, you need to add the .cpp file to the add_library statement that defines the libfreenect target.

How do I configure the way a project is built in CLion?

The problem is as follows.
I have installed libarmadillo on my Ubuntu distributive via apt utility having liblapack and libblas previously installed. And I'm trying to use it in my CLion project. What my intentions result in is that I'm receiving some build errors. One of them was solved by adding #define ARMA_DONT_USE_WRAPPER.
I have found the way to build my project in this topic -
Armadillo + BLAS + LAPACK: Linking error?. Though, I can build it only with terminal. I assume that the issue with CLion is CMake configuration.
What is the way for me to alter CMake script so that I refine its built behavior and make it compile my project?
In simple words, how do I make it compile my program with g++ main.cpp -o lab2 -O1 -llapack -lblas.
Code sample:
#define ARMA_DONT_USE_WRAPPER
#define ARMA_USE_BLAS
#define ARMA_USE_LAPACK
#include <armadillo>
#include <iostream>
using namespace arma;
using namespace std;
int main() {
mat A(4, 5);
A.load("matrix.txt");
mat B = resize(A, 4, 4);
cout << norm(A, 2);
cout << B;
return 0;
}
The errors I'm issued:
CMakeFiles/lab2.dir/main.cpp.o: In function `double
arma::blas::asum<double>(unsigned long long, double const*)':
/usr/include/armadillo_bits/wrapper_blas.hpp:241: undefined reference
to `dasum_'
CMakeFiles/lab2.dir/main.cpp.o: In function `double
arma::blas::nrm2<double>(unsigned long long, double const*)':
/usr/include/armadillo_bits/wrapper_blas.hpp:273: undefined reference
to `dnrm2_'
collect2: error: ld returned 1 exit status
CMakeFiles/lab2.dir/build.make:94: recipe for target 'lab2' failed
make[3]: *** [lab2] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/lab2.dir/all'
failed
make[2]: *** [CMakeFiles/lab2.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/lab2.dir/rule'
failed
make[1]: *** [CMakeFiles/lab2.dir/rule] Error 2
Makefile:118: recipe for target 'lab2' failed
make: *** [lab2] Error 2
CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(lab2)
set(CMAKE_CXX_STANDARD 11)
add_executable(lab2 main.cpp)
You first need CMake functions to find the libraries liblapack and libblas.
For both there are already functions in the standard cmake distribution so
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
find_package(Armadillo REQUIRED)
See FindBLAS.cmake and FindLAPACK.cmake in your cmake modules directory for documentation what variables they define.
then add them to your targets, e.g.:
target_link_libraries(lab2 ${LAPACK_LIBRARIES} ${BLAS_LIBARIES} ${ARMADILLO_LIBRARIES})
As #Richard Hodges said in a comment, in the Settings you can set the variables fed to CMake as well as define environment variables.
Something also I like to do is to include a custom CMake script to easily tweak a configuration; for example add in your CMakeLists.txt:
include(local.cmake OPTIONAL)
That way, you can put any CMake configuration you want in local.cmake in your source directory, and it will be parsed. Nothing happens if the file does not exist.

Error linking to Boost filesystem using cmake on cygwin

I'm using cmake 2.8.9, g++ 3.4.4, and Boost 1.50. in Cygwin on Windows 8 64 bit.
Here is the error message I get.
Linking CXX executable RayTracer.exe
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x89c):
undefined reference to boost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8a6):
undefined reference toboost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8b0):
undefined reference to boost::system::system_category()'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld:
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o: bad reloc address 0xb in
section
.text$_ZN5boost6system14error_categoryD1Ev[boost::system::error_category::~error_category()]'
collect2: ld returned 1 exit status
CMakeFiles/RayTracer.dir/build.make:94: recipe for target
RayTracer.exe' failed make[2]: *** [RayTracer.exe] Error 1
CMakeFiles/Makefile2:64: recipe for target
CMakeFiles/RayTracer.dir/all' failed make[1]: *
[CMakeFiles/RayTracer.dir/all] Error 2 Makefile:75: recipe for target
`all' failed make: * [all] Error 2
From what I've seen, the usual problem is failing to link the boost system library, but I made sure to do that. Here is the relevant portion of my CMakeLists.txt file:
#Edit: cmake can't find the static libraries on cygwin, so I'm setting this to false for now.
SET(Boost_USE_STATIC_LIBS FALSE)
FIND_PACKAGE(Boost 1.50 REQUIRED date_time program_options thread filesystem system unit_test_framework)
IF(${Boost_FOUND})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ENDIF()
add_executable(RayTracer
Ray_Tracer.cpp
)
target_link_libraries(RayTracer ${Boost_PROGRAM_OPTIONS_LIBRARIES})
And here's the line in my .cpp file that triggers the error:
#include <boost/filesystem.hpp>
Any idea what I'm doing wrong?
You need to tell the linker to link Boost.Filesystem and Boost.System libraries.
You can do:
target_link_libraries(RayTracer
${Boost_PROGRAM_OPTIONS_LIBRARIES}
${Boost_FILESYSTEM_LIBRARIES}
${Boost_SYSTEM_LIBRARIES}
)
or if you just want to link all the libs specified in your find_package(Boost...) call, you can do:
target_link_libraries(RayTracer ${Boost_LIBRARIES})
For further details on the FindBoost CMake module, see the docs or run:
cmake --help-module FindBoost