Milan
Milan

Reputation: 2017

How can I link locally installed SDK's static library in my C++ project?

I have been working on a C++ project which depends on RPLidar SDK. I have already installed the SDK as per the instructions in the README. (FYI: I couldn't install it in the /usr/local/ directory using sudo make install command). As per the SDK documentation:

When developing via RPLIDAR SDK, developers only need to include SDK’s external header files (under sdk\include) into their own source code and link the application with SDK’s static library (rplidar_driver.lib or rplidar_driver.a).

So, in my CMakeLists.txt, I have already added:

include_directories(/home/milan/rplidar_sdk/sdk/sdk/include/)

However, I couldn't find rplidar_driver.lib or rplidar_driver.h. Here, is the directory structure of the SDK:

.
├── docs
│   ├── ReleaseNote.v1.10.0.md
│   |       ...
│   └── ReleaseNote.v1.9.1.md
├── LICENSE
├── README.md
├── sdk
│   ├── app
│   │   ├── frame_grabber
│   │   │   ├── AboutDlg.cpp
│   │   │   ├── AboutDlg.h
│   │   │   ├── drvlogic
│   │   │   │   ├── common.h
│   │   │   │   ├── lidarmgr.cpp
│   │   │   │   └── lidarmgr.h
│   │   │   ├── framegrabber.cpp
│   │   │   ├── framegrabber.h
│   │   │   ├── framegrabber.rc
│   │   │   ├── FreqSetDlg.cpp
│   │   │   ├── FreqSetDlg.h
│   │   │   ├── MainFrm.cpp
│   │   │   ├── MainFrm.h
│   │   │   ├── ref
│   │   │   │   └── wtl
│   │   │   │       ├── atlapp.h
│   │   │   │       ├── atlcrack.h
│   │   │   │       ├── atlctrls.h
│   │   │   │       ├── atlctrlw.h
│   │   │   │       ├── atlctrlx.h
│   │   │   │       ├── atlddx.h
│   │   │   │       ├── atldlgs.h
│   │   │   │       ├── atldwm.h
│   │   │   │       ├── atlfind.h
│   │   │   │       ├── atlframe.h
│   │   │   │       ├── atlgdi.h
│   │   │   │       ├── atlmisc.h
│   │   │   │       ├── atlprint.h
│   │   │   │       ├── atlresce.h
│   │   │   │       ├── atlres.h
│   │   │   │       ├── atlribbon.h
│   │   │   │       ├── atlscrl.h
│   │   │   │       ├── atlsplit.h
│   │   │   │       ├── atltheme.h
│   │   │   │       ├── atluser.h
│   │   │   │       ├── atlwince.h
│   │   │   │       └── atlwinx.h
│   │   │   ├── res
│   │   │   │   ├── rplidar.ico
│   │   │   │   └── Toolbar.bmp
│   │   │   ├── resource.h
│   │   │   ├── scanView.cpp
│   │   │   ├── scanView.h
│   │   │   ├── SerialSelDlg.cpp
│   │   │   ├── SerialSelDlg.h
│   │   │   ├── stdafx.cpp
│   │   │   ├── stdafx.h
│   │   │   ├── TcpChannelSelDlg.cpp
│   │   │   └── TcpChannelSelDlg.h
│   │   ├── Makefile
│   │   ├── simple_grabber
│   │   │   ├── main.cpp
│   │   │   └── Makefile
│   │   └── ultra_simple
│   │       ├── main.cpp
│   │       └── Makefile
│   ├── cross_compile.sh
│   ├── mak_common.inc
│   ├── mak_def.inc
│   ├── Makefile
│   ├── obj
│   │   └── Linux
│   │       └── Release
│   │           ├── sdk
│   │           │   └── src
│   │           │       ├── arch
│   │           │       │   └── linux
│   │           │       │       ├── net_serial.o
│   │           │       │       ├── net_socket.o
│   │           │       │       └── timer.o
│   │           │       ├── hal
│   │           │       │   └── thread.o
│   │           │       └── rplidar_driver.o
│   │           ├── simple_grabber
│   │           │   └── main.o
│   │           └── ultra_simple
│   │               └── main.o
│   ├── output
│   │   └── Linux
│   │       └── Release
│   │           ├── librplidar_sdk.a
│   │           ├── simple_grabber
│   │           └── ultra_simple
│   ├── sdk
│   │   ├── include
│   │   │   ├── rplidar_cmd.h
│   │   │   ├── rplidar_driver.h
│   │   │   ├── rplidar.h
│   │   │   ├── rplidar_protocol.h
│   │   │   └── rptypes.h
│   │   ├── Makefile
│   │   └── src
│   │       ├── arch
│   │       │   ├── linux
│   │       │   │   ├── arch_linux.h
│   │       │   │   ├── net_serial.cpp
│   │       │   │   ├── net_serial.h
│   │       │   │   ├── net_socket.cpp
│   │       │   │   ├── thread.hpp
│   │       │   │   ├── timer.cpp
│   │       │   │   └── timer.h
│   │       │   ├── macOS
│   │       │   │   ├── arch_macOS.h
│   │       │   │   ├── net_serial.cpp
│   │       │   │   ├── net_serial.h
│   │       │   │   ├── net_socket.cpp
│   │       │   │   ├── thread.hpp
│   │       │   │   ├── timer.cpp
│   │       │   │   └── timer.h
│   │       │   └── win32
│   │       │       ├── arch_win32.h
│   │       │       ├── net_serial.cpp
│   │       │       ├── net_serial.h
│   │       │       ├── net_socket.cpp
│   │       │       ├── timer.cpp
│   │       │       ├── timer.h
│   │       │       └── winthread.hpp
│   │       ├── hal
│   │       │   ├── abs_rxtx.h
│   │       │   ├── assert.h
│   │       │   ├── byteops.h
│   │       │   ├── event.h
│   │       │   ├── locker.h
│   │       │   ├── socket.h
│   │       │   ├── thread.cpp
│   │       │   ├── thread.h
│   │       │   ├── types.h
│   │       │   └── util.h
│   │       ├── rplidar_driver.cpp
│   │       ├── rplidar_driver_impl.h
│   │       ├── rplidar_driver_serial.h
│   │       ├── rplidar_driver_TCP.h
│   │       └── sdkcommon.h
│   └── workspaces
│       ├── vc10
│       │   ├── frame_grabber
│       │   │   ├── frame_grabber.vcxproj
│       │   │   └── frame_grabber.vcxproj.filters
│       │   ├── rplidar_driver
│       │   │   ├── rplidar_driver.vcxproj
│       │   │   └── rplidar_driver.vcxproj.filters
│       │   ├── sdk_and_demo.sln
│       │   ├── simple_grabber
│       │   │   ├── simple_grabber.vcxproj
│       │   │   └── simple_grabber.vcxproj.filters
│       │   └── ultra_simple
│       │       ├── ultra_simple.vcxproj
│       │       └── ultra_simple.vcxproj.filters
│       └── vc14
│           ├── frame_grabber
│           │   ├── frame_grabber.vcxproj
│           │   └── frame_grabber.vcxproj.filters
│           ├── rplidar_driver
│           │   ├── rplidar_driver.vcxproj
│           │   └── rplidar_driver.vcxproj.filters
│           ├── sdk_and_demo.sln
│           ├── simple_grabber
│           │   ├── simple_grabber.vcxproj
│           │   └── simple_grabber.vcxproj.filters
│           └── ultra_simple
│               ├── ultra_simple.vcxproj
│               └── ultra_simple.vcxproj.filters
└── tools
    └── cp2102_driver
        └── CP210x_Windows_Drivers.zip

Now, in my main.cpp, I have this line: #include <rplidar.h>. I can successfully run cmake .. command in the build directory of my project. However, upon running make command, it's giving me this error:

In file included from /home/milan/<project>/src/main.cpp:3:0:
/home/milan/rplidar_sdk/sdk/sdk/include/rplidar.h:38:10: fatal error: hal/types.h: No such file or directory
 #include "hal/types.h"
          ^~~~~~~~~~~~~
compilation terminated.
CMakeFiles/<project>.dir/build.make:81: recipe for target 'CMakeFiles/<project>.dir/src/main.cpp.o' failed
make[2]: *** [CMakeFiles/<project>.dir/src/main.cpp.o] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/<project>.dir/all' failed
make[1]: *** [CMakeFiles/<project>.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
make: *** [all] Error 2

Here is my complete CMakeLists.txt:

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)

project(<project>)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

include_directories(include)
include_directories(/home/milan/rplidar_sdk/sdk/sdk/include/)

add_executable(${PROJECT_NAME} src/main.cpp)

How can I fix this?

Upvotes: 0

Views: 850

Answers (3)

In

target_include_directories(rplidar INTERFACE
  ${RPLIDAR_SDK_PATH}/sdk/sdk/include/
  ${RPLIDAR_SDK_PATH}/sdk/sdk/src/)

you probably have inserted double "sdk" folder.

Upvotes: 0

Milan
Milan

Reputation: 2017

If someone is directly looking for the updated CMakeLists.txt then here it is (special thanks to Botje's answer):

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)

project(<project_name>)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
SET(CMAKE_CXX_FLAGS -pthread)

include_directories(include)
add_executable(${PROJECT_NAME} src/main.cpp src/another_src_file.cpp)

## RPLidar
# Update the following path variable as per the RPLidar SDK installation in your system!
set(RPLIDAR_SDK_PATH "/home/milan/rplidar_sdk/")

add_library(rplidar STATIC IMPORTED)

set_property(TARGET rplidar
  PROPERTY IMPORTED_LOCATION ${RPLIDAR_SDK_PATH}/sdk/output/Linux/Release/librplidar_sdk.a)

target_include_directories(rplidar INTERFACE
  ${RPLIDAR_SDK_PATH}/sdk/sdk/include/
  ${RPLIDAR_SDK_PATH}/sdk/sdk/src/)

target_link_libraries(${PROJECT_NAME} rplidar)

Note: First, you have to build rplidar_sdk

Upvotes: 1

Botje
Botje

Reputation: 30907

Since you already compiled the library, create an IMPORTED target that references the sdk/include and sdk/src directories as include path:

add_library(rplidar STATIC IMPORTED)
set_property(TARGET rplidar
  PROPERTY IMPORTED_LOCATION /home/milan/rplidar_sdk/output/Linux/Release/librplidar_sdk.a)
target_include_directories(rplidar INTERFACE
  /home/milan/rplidar_sdk/sdk/sdk/include/
  /home/milan/rplidar_sdk/sdk/sdk/src/)

You may even need to add the sdk/arch/linux directory to the include path.

And now you can link that library to your main file by doing:

target_link_libraries(${PROJECT_NAME} rplidar)

Upvotes: 2

Related Questions