Building with libfreenect2

2019-07-29 05:00发布

Would anyone be able to post a simple example of how to compile code which uses libfreenect2? After installing the library, the following structure is created in my home directory:

→ tree freenect2 
freenect2
├── include
│   └── libfreenect2
│       ├── config.h
│       ├── export.h
│       ├── frame_listener.hpp
│       ├── frame_listener_impl.h
│       ├── libfreenect2.hpp
│       ├── logger.h
│       ├── packet_pipeline.h
│       └── registration.h
└── lib
    ├── cmake
    │   └── freenect2
    │       └── freenect2Config.cmake
    ├── libfreenect2.so -> libfreenect2.so.0.2
    ├── libfreenect2.so.0.2 -> libfreenect2.so.0.2.0
    ├── libfreenect2.so.0.2.0
    └── pkgconfig
        └── freenect2.pc

I attempted to compile with the .pc file using a line similar to this found on the pkg-config wikipedia page:

gcc -o test test.c $(pkg-config --libs --cflags libpng)

But came with up with this error:

./test: error while loading shared libraries: libfreenect2.so.0.2: cannot open shared object file: No such file or directory

Obviously, I messed up the compilation process somewhere, but I'm not sure where to look since this is error occurs on runtime and not at compile time. There's also a .cmake file created with the library install, which I'm sure would lead to a more robust and proper solution, but I'm not entirely sure how to use that and haven't been able to find a simple guide showing how to do so. Any links to beginner-friendly documentation are also appreciated. In the documentation for libfreenect2, it says to use this line when compiling cmake -Dfreenect2_DIR=$HOME/freenect2/lib/cmake/freenect2 -- is this something that I'd have to use when making the library or when making my application?

Another tangentially related question, would it be better to move the /include and /lib directories to /usr/local/include and /usr/local/lib respectively? I believe that would "install" the library system-wide, but I imagine there's some reason that libfreenect2 doesn't do it automatically and I'm not sure what that is.

标签: c++ build cmake
1条回答
倾城 Initia
2楼-- · 2019-07-29 05:34

Well, I just use cmake with a CMakeLists.txt file that I create. Do like this:

Create a CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project("My Project")

set(CMAKE_CXX_FLAGS "-std=c++11")

find_package(freenect2 REQUIRED)

include_directories("/usr/include/libusb-1.0/")

INCLUDE_DIRECTORIES(
  ${freenect2_INCLUDE_DIR}
)

add_executable(main ./main.cpp)

target_link_libraries(main ${freenect2_LIBRARIES})

In this file, I assume we want to compile the main.cpp file that uses libfreenect2. So, in your local directory create a build folder, using the terminal:

mkdir build && cd build

Then, run the command in the terminal:

cmake -Dfreenect2_DIR=$HOME/freenect2/lib/cmake/freenect2 .. && make

this should create main executable in the build folder. Please, note that this cmake command specifies the freenect2 directory. In this case I assume it was placed in the /home directory.

However, I understand that having to type that long cmake command or search for it on the terminal history may be boring for some people. So, it is possible to embed the command like this:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project("My Project")

set(CMAKE_CXX_FLAGS "-std=c++11")
# Set cmake prefix path to enable cmake to find freenect2
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} $ENV{HOME}/freenect2/lib/cmake/freenect2)
find_package(freenect2 REQUIRED)

include_directories("/usr/include/libusb-1.0/")

INCLUDE_DIRECTORIES(
  ${freenect2_INCLUDE_DIR}
)

add_executable(main ./main.cpp)

target_link_libraries(main ${freenect2_LIBRARIES})

After, just run this in the terminal: mkdir build && cd build && cmake .. & make

This answer was my source for this second way of compiling the code.

Hope this helps!

查看更多
登录 后发表回答