libfreenect c++ 包装器中的未定义引用

Undefined reference in libfreenect c++ wrapper

我想在 C++ 中使用 libfreenect 打印已连接设备的数量。如 https://openkinect.org/wiki/C%2B%2B_Wrapper 中所述 我在 TestKinectConnection.cpp.

中包含 libfreenect.hpp 头文件

我的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);
}

当我使用 cmake --build build -- -j3 构建时,终端显示

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

我的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

简而言之,libfreenect.hpp 尝试包含 libfreenect.h 但完全失败了。

从你的问题来看,不清楚你是全局安装了 libfreenect 还是只是将库与你的代码捆绑在一起。

如果你全局安装它,你忘了告诉 CMake libfreenect 目标需要 link 和 libfreenect.so 使用 -lfreenect linker旗帜。 添加以下内容应该可以解决这个问题:

set_property(TARGET libfreenect PROPERTY INTERFACE_LINK_LIBRARIES -lfreenect)

正确的方法是使 libfreenect 成为一个 IMPORTED 目标,如 "It's time to do CMake right" 中所述。

如果您将 .cpp 与您的代码捆绑在一起,则需要将 .cpp 文件添加到定义 libfreenect 目标的 add_library 语句中。