gpt4 book ai didi

c++ - libfreenect c++ 包装器中的 undefined reference

转载 作者:行者123 更新时间:2023-11-28 01:15:31 26 4
gpt4 key购买 nike

我想在 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目标需要与 libfreenect.so 链接使用 -lfreenect链接器标志。添加以下内容应该可以解决这个问题:

set_property(TARGET libfreenect PROPERTY INTERFACE_LINK_LIBRARIES -lfreenect)

正确的方法是制作libfreenect一个 IMPORTED 目标,如 "It's time to do CMake right" 中所述.

如果您捆绑了 .cpp使用您的代码,您需要添加 .cpp文件到 add_library定义 libfreenect 的语句目标。

关于c++ - libfreenect c++ 包装器中的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58857067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com