简体   繁体   中英

How to fix “Could not find a package configuration file …” error in CMake?

I have been working on a project which uses rplidar_sdk and in the beginning, I was facing this problem:

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

Basically, the SDK generates the library in its local directory, and in its Makefile , it does not have install rules. I mean I can run make but after that, if I run sudo make install then it gives make: *** No rule to make target 'install'. Stop. make: *** No rule to make target 'install'. Stop. error.

So, with the help of this & this answer, I was able to build my local project. So far so good.

However, the main problem is that I have to hard-code the RPLidar SDK path in CMakeLists.txt of my repo. Now, whenever someone else in my team starts working on that repo (which is quite obvious) then he/she has to update the CMakeLists.txt first. This is not a good idea/practice!

To fix this, I updated theMakefile of RPLidar SDK as follow:

.
.
.

RPLIDAR_RELEASE_LIB := $(HOME_TREE)/output/Linux/Release/librplidar_sdk.a

install: $(RPLIDAR_RELEASE_LIB)
    install -d $(DESTDIR)/usr/local/lib/rplidar/Release/
    install -m 644 $(RPLIDAR_RELEASE_LIB) $(DESTDIR)/usr/local/lib/rplidar/Release/


RPLIDAR_DEBUG_LIB := $(HOME_TREE)/output/Linux/Debug/librplidar_sdk.a

install: $(RPLIDAR_DEBUG_LIB)
    install -d $(DESTDIR)/usr/local/lib/rplidar/Debug/
    install -m 644 $(RPLIDAR_DEBUG_LIB) $(DESTDIR)/usr/local/lib/rplidar/Debug/


RPLIDAR_HEADERS := $(HOME_TREE)/sdk/include

install: $(RPLIDAR_HEADERS)
    install -d $(DESTDIR)/usr/local/include/rplidar/
    cp -r $(RPLIDAR_HEADERS)/* $(DESTDIR)/usr/local/include/rplidar/

RPLIDAR_HEADERS_HAL := $(HOME_TREE)/sdk/src/hal

install: $(RPLIDAR_HEADERS_HAL)
    install -d $(DESTDIR)/usr/local/include/rplidar/
    cp -r $(RPLIDAR_HEADERS_HAL) $(DESTDIR)/usr/local/include/rplidar/

Due to this update, now, I can run sudo make install which basically copies the header files of RPLidar SDK from the local directory to /usr/local/rplidar/ directory. It also copies the lib file to /usr/local/lib/rplidar/<Debug> or <Release>/ directory.

Now, in my local project, I updated the CMakeLists.txt to as follow:

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)

find_package(rplidar REQUIRED)
include_directories(${rplidar_INCLUDE_DIRS})
link_directories(${rplidar_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} ${rplidar_LIBRARY})

However, upon running cmake.. command, I'm getting this error:

.
.
.
CMake Error at CMakeLists.txt:12 (find_package):
  By not providing "Findrplidar.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "rplidar", but
  CMake did not find one.

  Could not find a package configuration file provided by "rplidar" with any
  of the following names:

    rplidarConfig.cmake
    rplidar-config.cmake

  Add the installation prefix of "rplidar" to CMAKE_PREFIX_PATH or set
  "rplidar_DIR" to a directory containing one of the above files.  If
  "rplidar" provides a separate development package or SDK, be sure it has
  been installed.


-- Configuring incomplete, errors occurred!

As far as I know, RPLidar SDK does not have rplidarConfig.cmake or rplidar-config.cmake file.

How can I fix this error?

Rants from my soul:

It sucks when you have to use any library foo when the author fails to provide a foo-config.cmake for you to use easily by invoking find_package(foo) . It's absolutely outrageous when a reasonably modern project still uses hand written Makefiles as its build system. I myself is stuck with a much worse constructed SDK than yours right now.


Short answer:

Since the author of the SDK fails to provide a config file to support your cmake usage, if you still insists on invoking find_package on the library (and you should,), you are required to write your own Module file to clean up their mess. (Yeah, you are doing the work for the library authors).

To truly achieve cross platform usage, you should write a Findrplidar.cmake module file to find the libraries for you.

To write a reasonable module file, you would most likely use API find_path for header files and find_library for libs. You should check out its docs and try using them, and maybe Google a few tutorials.

Here is my version of Findglog.cmake for your reference. (glog authors have updatedtheir code and supports Config mode. Unfortunately, Ubuntu build doesn't use it, so I still have to write my own file)

find_path(glog_INCLUDE_DIR glog/logging.h)
message(STATUS "glog header found at: ${glog_INCLUDE_DIR}")

find_library(glog_LIB glog)
message(STATUS "libglog found at: ${glog_LIB}")

mark_as_advanced(glog_INCLUDE_DIR glog_LIB)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(glog REQUIRED_VARS
  glog_INCLUDE_DIR
  glog_LIB
  )

if(glog_FOUND AND NOT TARGET glog::glog)
  add_library(glog::glog SHARED IMPORTED)
  set_target_properties(glog::glog PROPERTIES
    IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
    IMPORTED_LOCATION "${glog_LIB}"
    INTERFACE_INCLUDE_DIRECTORIES
      "${glog_INCLUDE_DIR}"
    )
endif()

And you can use it like this:

find_package(glog)
target_link_libraries(main PRIVATE glog::glog)

Long answer:

The history of developers using cmake is an absolute nightmare. The internet is filled with bad practice/examples of how not to use cmake in your project, including the old official cmake tutorial (which still might be there). Mostly because no one really gives a **** (If I can build my project, who cares if it's cross platform). Another valid reason is that cmake documentations are really daunting to beginners.

This is why I am writing my own answer here, lest you get misguided by Googling elsewhere.

The nightmare is no more. The wait has ended. "The Messiah" of cmake ( source ) is come. He bringeth hope to asm/C/C++/CUDA projects written in 2020 and on. Here is The Word .

The link above points to the only way how cmake projects should be written and truly achieve cross platform once and for all. Note the material is not easy at all to follow for beginners. I myself spent an entire week to fully grasp what was covered in The Word, when I had become somewhat familiar with cmake concepts at the time (but lost in my old sinful ways).

The so-called "long answer" is actually shorter. It's just a pointer to the real answer. Good luck reading the Word. Embrace the Word, for anything against that is pure heresy.


Response of comment 1-5:

Good questions. A lot of those can be obtained from the Word. But the word is better digested when you become more familiar with CMake. Let me answer them in decreasing of relevance to your problem at hand.

For the ease of discussion, I'll just use libfoo as an example.

Let's say you always wants to use libfoo like this:

find_package(foo)
target_link_libraries(your_exe ... foo::foo)

Pretend foo is installed at the following location:

- /home/dev/libfoo-dev/
  - include
    - foo
      - foo.h
      - bar.h
      - ...
  - lib
    - libfoo.so
  - share
    - foo/foo-config.cmake   # This may or may not exist. See discussion.

Q: Only one .h file. Why?

A: Because in the case of libfoo (also true for glog ), only one search of header location is necessary. Just like the example from libfoo , where foo/foo.h and foo/bar.h are at the same location. So their output of find_path would be the same: /home/dev/libfoo-dev/include .


Q: Why I'm getting NOTFOUND for my headers and libs?

A: The function find_path and find_library only search locations specify in the documentations. By default they search system locations, like /usr/include and /usr/lib respectively. Refer to the official docs for details on system locations. In the case of libfoo , however, they reside in /home/dev/libfoo-dev . So you must specify these locations in cmake variable CMAKE_PREFIX_PATH . It's a ; seperated string. One would do cmake -D CMAKE_PREFIX_PATH="/home/dev/libfoo-dev;/more/path/for/other/libs/;...;/even/more/path".... on the command line.

One very important note: unlike Unix command find , find_path will only search specific paths inside /home/dev/libfoo-dev , not all the way down: include (usually also include/{arch} where {arch} is sth like x86_64-linux-gnu for x86 Linux) for find_path ; lib variant for find_library , respectively. Unusual locations would require passing in more arguments, which is uncommon and most likely unnecessary.

For this very reason, for libfoo , calling find_path(... foo.h...) is undesired. One would want find_path(... foo/foo.h...) . Refer to the docs for more details. You can also try out yourself.

Also for this reason, it is desirable to organize libraries in the usual bin include lib share quad on Unix-like systems. I'm not familiar with Windows.


Q: Debug & Release

A: There are several options. The easiest one might be:

  • Prepare rplidar debug and release build in two different folders, /path/to/debug & /path/to/release for instance
  • Passing to Debug & Release build respectively ( cmake -D CMAKE_PREFIX_PATH="/path/to/debugORrelease".... )

There are definitely others ways, but perhaps requires special care in your Findrplidar.cmake script (maybe some if statements).


Q: Why glog::glog rather than glog ?

A: It's just modern cmake practice, with small benefits. Not important right now. Refer to the Word if you are interested.


Q: You mentioned that you are writing rplidarConfig.cmake . Instead you should rename the file to Findrplidar.cmake .

A: CMake philosophy is as such:

  • Library authors should write foo-config.cmake or fooConfig.cmake
  • When they fail to provide one, it sucks. And according to the Messiah, it should be reported as a bug.
  • In this case, you as library user, should write Findfoo.cmake by guessing how to describe the dependencies for libfoo. For simple libraries, this is not so bad. For complex ones, like Boost, this sucks!

A few side note on this topic:

  • Note how Findfoo.cmake is written by library users, from guessing.
  • This is insane. Users shouldn't do this, This is the authors' fault. to put their users in this uncomfortable situation.
  • A foo-config.cmake file is extremely easy to write for authors of libfoo, IF they follow the Word exactly.
  • Extremely easy for the authors in the sense that: cmake can take care of everything. It will generate scripts automatically for the authors to use in their foo-config.cmake file.
  • Guaranteed to be cross-platform and easy to use by the users, thanks to cmake.
  • However, the reality sucks. Now you have to write Findfoo.cmake

Q: Why only find_package & target_link_libraries ?

A: This is what the Word says. It's therefore good practice. Why the Word says so is something you have to find out yourself. It's not possible for me to explain the gist of the Word in this answer, nor would it be convincing to you. I would just say the following:

It's very easy to write spaghetti CMakeLists that are next to impossible to maintain. The spirit of the Word helps you avoid that by forcing you to carefully think about:

  • library structure: public vs private headers, for example. This makes you think about what to include in your headers and public APIs.
  • build specification: what is necessary to build a library you write (what to include; what to link)
  • usage requirement: what is necessary for others to use a library you write (what to include; what to link)
  • dependencies: what is the relationship of the library you write & its dependencies
  • Maybe more

If you think about it, these aspects are crucial to writing a cross-platform and maintainable library. include_directories , link_directories and add_definitions are all very bad practice (according to lots of sources, including the official documentations of these APIs). Bad practice tends to obscure the aspects above, and causes problems later on when everything gets integrate together as a whole. Eg include_directories will add -I to compiler for every target written in the directory of that CMakeLists.txt . Read this sentence a few times and Spock will tell you it's illogical.

Don't worry. It's okay for now to use them when you are not familiar with the Word (Why else would this be in the last section). Once you know the Word, refactor your CMakeLists when you have time. Bad practice might cause problem later on, when your project becomes more complex. (In my professional experience, 5 very small groups of people is enough to cause a nightmare. By nightmare I mean hard code everything in CMakeLists; Create a git branch for every single different platform/device/environment; Fixing a bug meaning to cherry-pick one commit to each branch. I've been there before knowing the Word.)

The practice of the Word very well utilize the philosophy of modern CMake, to encapsulate build specifications and usage requirements inside CMake targets. So when target_link_libraries is called, these properties gets propagated properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM