Undefined reference in libfreenect c++ wrapper - c++

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.

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?

What modules should be included in CMakeList.txt for Approximate Nearest Neighbor Searching?

I have compiled ANN-library and need to use it in a C++ file for segmentation evaluation
I have set the CMakeList.txt, which is using ITK and ANN libraries as follows:
PROJECT(EvaluateSegmentationResult)
cmake_minimum_required(VERSION 3.14)
set(ANN_LIB /home/user/tools/ann_1.1.2/lib/)
set(ANN_PATH /home/user/tools/ann_1.1.2/include/)
#FIND_PACKAGE(ITK)
find_package(ITK COMPONENTS
ITKBinaryMathematicalMorphology
ITKCommon
ITKIOImageBase
ITKImageFunction
ITKImageGrid
ITKImageIntensity
ITKMathematicalMorphology
ITKThresholding
ITKImageIO
)
IF(ITK_FOUND)
INCLUDE(${ITK_USE_FILE})
ELSE(ITK_FOUND)
MESSAGE(FATAL_ERROR
"ITK not found. Please set ITK_DIR.")
ENDIF(ITK_FOUND)
FIND_PATH(ANN_PATH NAMES ANN)
FIND_LIBRARY(ANN_LIB NAMES ann PATHS ${ANN_PATH})
INCLUDE_DIRECTORIES(${ANN_PATH})
ADD_EXECUTABLE( EvaluateSegmentationResult EvaluateSegmentationResult.cpp)
#TARGET_LINK_LIBRARIES( EvaluateSegmentationResult ITKIO ITKBasicFilters ITKCommon ${ITK_LIBRARIES} ${ANN_LIB})
TARGET_LINK_LIBRARIES( EvaluateSegmentationResult ${ITK_LIBRARIES} ${ANN_LIB})
However, it raises an error once I am compiling the C++ file:
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:86: undefined reference to `annAllocPts(int, int)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:95: undefined reference to `ANNkd_tree::ANNkd_tree(double**, int, int, int, ANNsplitRule)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:135: undefined reference to `annAllocPts(int, int)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:144: undefined reference to `ANNkd_tree::ANNkd_tree(double**, int, int, int, ANNsplitRule)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:175: undefined reference to `annDeallocPts(double**&)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:176: undefined reference to `annDeallocPts(double**&)'
collect2: error: ld returned 1 exit status
CMakeFiles/EvaluateSegmentationResult.dir/build.make:128: recipe for target 'EvaluateSegmentationResult' failed
make[3]: *** [EvaluateSegmentationResult] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/EvaluateSegmentationResult.dir/all' failed
make[2]: *** [CMakeFiles/EvaluateSegmentationResult.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/EvaluateSegmentationResult.dir/rule' failed
make[1]: *** [CMakeFiles/EvaluateSegmentationResult.dir/rule] Error 2
Makefile:118: recipe for target 'EvaluateSegmentationResult' failed
make: *** [EvaluateSegmentationResult] Error 2
It seems the issue is related to link libraries. should I add any specific line or module to the CMakeList.txt?
Those methods are defined in ANN.lib itself, so you haven't missed adding any libraries. The ${ANN_PATH} variable passed as the PATH argument in your find_library call points to the header include folder. You should make it point to the folder that contains the library, and do an if-check to see it is found before proceeding to the target_link_libraries call.
I could solve the issue by just adding -lANN -LlibANN to the target_link_libraries() call:
TARGET_LINK_LIBRARIES( EvaluateSegmentationResult ${ITK_LIBRARIES} -lANN -LlibANN)

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.

undefined reference to `boost::system::generic_category()' when adding boost/asio

I want to use boost.asio header in my project, but when I include it's .hpp file I got this output error on compile:
I need boost.asio for crow to route my web request.
/home/john/Downloads/clion-1.2.4/bin/cmake/bin/cmake --build /home/john/.CLion12/system/cmake/generated/a3f08900/a3f08900/Release --target rcp -- -j 8
[ 50%] Linking CXX executable /home/john/projects/rightChoiceProperty/bin/rcp
CMakeFiles/rcp.dir/main.cpp.o: In function _GLOBAL__sub_I_main':
main.cpp:(.text.startup+0x53): undefined reference toboost::system::generic_category()'
main.cpp:(.text.startup+0x58): undefined reference to boost::system::generic_category()'
main.cpp:(.text.startup+0x5d): undefined reference toboost::system::system_category()'
main.cpp:(.text.startup+0x62): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
CMakeFiles/rcp.dir/build.make:94: recipe for target '/home/john/projects/rightChoiceProperty/bin/rcp' failed
make[3]: * [/home/john/projects/rightChoiceProperty/bin/rcp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/rcp.dir/all' failed
make[2]: [CMakeFiles/rcp.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/rcp.dir/rule' failed
make[1]: [CMakeFiles/rcp.dir/rule] Error 2
Makefile:118: recipe for target 'rcp' failed
make: * [rcp] Error 2
I'm using CLion 1.2.4 as IDE
this is my main.cpp content:
#include <iostream>
#include <boost/asio.hpp>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
And this is my CMakeLists.txt file content:
cmake_minimum_required(VERSION 3.3)
project(rcp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lboost_system")
set(SOURCE_FILES main.cpp)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
include_directories("libraries/boost")
add_executable(rcp ${SOURCE_FILES})
Thank you so much
The -l option is not a compiler option, it's a linker options, so you're setting it for the wrong variable as CMAKE_CXX_FLAGS are only for the compiler.
Instead use e.g. target_link_libraries to add libraries. Like
target_link_libraries(rcp boost_system)
But what you really should do is to find the system-installed Boost libraries and use those. You do that with find_package:
find_package(Boost
REQUIRED COMPONENTS asio system)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(rcp ${Boost_LIBRARIES})

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