0

I want to use the following library https://github.com/gmeuli/caterpillar

It's documentation says that it's a header-only library, and that I should "directly integrate it into my source files with #include <caterpillar/caterpillar.h>." It also depends on a few other libraries, one of which I need to use directly as well.

So far I have done the following:

  1. create cmake project to make an 'executable' (with the vscode extension)
  2. created a 'lib' folder, inside which I did
git clone https://github.com/gmeuli/caterpillar
  1. Then, I did include_directories(lib) in my cmake file.
  2. But #include <caterpillar/caterpillar.h> doesn't quite work in my singular main.cpp file.

I played around with various CMake functions, and it either gave the error "No such file or directory" regarding caterpillar/caterpillar.h itself, or it gave "cannot open source file... dependent of caterpillar/caterpillar.h" depending on how I messed with the cmake file.

For reference:

cat ~/project/main.cpp

#include <caterpillar/caterpillar.hpp>
#include <lorina/lorina.hpp> //how do I include this ? it's in the lib folder of caterpillar itself, or do I need to have a copy of it in my lib folder too

int main()
{
    // stuff in lorina:: namespace
    // stuff in caterpillar:: namespace
    return 0;
}

cat ~/project/CMakeLists.txt

include_directories(lib)
//... rest is stuff like CXX standard, etc etc

tree ~/project

main.cpp
lib/
    caterpillar/
build/
    cmake generated stuff
CMakeLists.txt
  • Taking a wild guess change `#include ` to `#include ` and make the necessary adjustments to CMake for that code to work. – john Jul 05 '22 at 08:09
  • Of course what you should really do is consult the documentation for that library and see how they expect you to include their header file. – john Jul 05 '22 at 08:13
  • Tried that, didn't work. But maybe I'm doing the 'necessary adjustments to CMake' wrong. – porridgewithraisins Jul 05 '22 at 08:13
  • @john, their documentation says to `#include ` . edited question with this info – porridgewithraisins Jul 05 '22 at 08:14
  • OK, well ignore what I said. – john Jul 05 '22 at 08:14
  • So what I would do is focus your question on your best attempt at getting this to work, saying I tried various things is not very helpful. Show the Cmake file for that attempt Show the errors for that attempt. Show how the various header files are trying to include each other. Show where the various header files are located. And finally say what compiler you are using (this is a compiler specific area). – john Jul 05 '22 at 08:16
  • There more than one way to get this to work, after all it's really is just a matter of telling your compiler where some header files are located. But obviously you want to do this in a straightforward manner as possible. – john Jul 05 '22 at 08:19
  • Regarding one of your questions. It's generally not a good idea to move files around to try and solve these problems. If a compiler is not finding a certain header file, then tell the compiler where to look rather than move the header file. – john Jul 05 '22 at 08:20

1 Answers1

0

Firstly, modern cmake recommends target_include_directories() instead of old include_directories() for better scope management.

Actually <caterpillar/caterpillar.hpp> is not in $PROJECT_SOURCE_DIR/lib directory. That's why your code not works.

CMakeLists example:

cmake_minimum_required(VERSION 3.22)
project(myproject)

set(CMAKE_CXX_STANDARD 17)

add_executable(my_project main.cpp)

target_include_directories(my_project PRIVATE ${PROJECT_SOURCE_DIR}/lib/caterpillar/include)

# project_src_dir/lib/catepillar/include/ is the directory where you find the headers like <catepillar/catepillar.hpp>

target_include_directories(my_project PRIVATE ${PROJECT_SOURCE_DIR}/lib/caterpillar/lib/lorina)

caterpillar's document describes how to include their headers in a traditional style, assuming the readers could understand this and decide where to put the headers themselves. (which means you don't need the whole git repo but only the "include" dir.)


For this specific problem, the library has provided a detailed CMakeLists.txt for users to include:

cmake_minimum_required(VERSION 3.22)
project(my_project)

set(CMAKE_CXX_STANDARD 17)

add_subdirectory(lib/caterpillar)
# this works because "project_src_dir/lib/catepillar/CMakeLists.txt" exists.

add_executable(my_project main.cpp)
target_link_libraries(my_project PRIVATE caterpillar)
# you need to tell cmake to add all catepillar settings into your project
halfelf
  • 9,737
  • 13
  • 54
  • 63
  • Thank you! It seems to nearly work. However, I now get the second error I mentioned. `[build] /home/sandy/revmd5/lib/caterpillar/include/caterpillar/details/utils.hpp:6:10: fatal error: tweedledum/gates/gate_set.hpp: No such file or directory [build] 6 | #include ` – porridgewithraisins Jul 05 '22 at 08:51