0

I have a third-party static library that I would like to add to a project, and as there are ongoing updates to the library, I would like to stick to a single CMakeLists.txt in root (if possible) - or at least a setup that requires the least amount of customization each time the library is updated.

Let's say my file structure is like this:

├── include
│   └── my_api
│       ├── some
│       │   └── long
│       │       └── path
│       │           └── include
│       │               ├── header1.h
│       │               └── header2.h
│       └── other
│           └── very
│               └── long
│                   └── path
│                       └── include
│                           ├── header3.h
│                           └── header4.h
├── lib
│   └── libmy_api.a
├── CMakeLists.txt
└── main.cpp

I have followed a myriad of tutorials, but either CMake or Make does not build or I get "No such file or directory" when I include one of the headers in my main.cpp.

I'm by no means a CMake super user and I'm not even sure if it's possible just having a single CMakeLists.txt in root, handling everything, or whether it is good practice.

And to complicate things even further, header1.h depends on header3.h using this path:

#include "other/very/long/path/include/header3.h"

All help is appreciated - thanks!

AGE_DK
  • 11
  • 2

1 Answers1

0

Include directories are added by the include_directories(...) or target_include_directories(...) commands. You need to add the paths to the include directories that contain the relative paths you use in your headers.

You can add the library using something like add_library(name STATIC IMPORTED). You then add the location via set_target_properties(name PROPERTIES IMPORTED LOCATION some/path/lib.a). Then you can link it to your targets as usual.

For reference:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • Thanks! Now it builds. However, I still get an error when **#include other/very/long/path/include/header3.h** from within header1.h. Any idea how to fix this? – AGE_DK Sep 29 '21 at 08:58
  • Depends on the actual error message. Maybe you didn't add the right path to the target where it is required. – moooeeeep Sep 29 '21 at 09:22
  • It's a simple "No such file or directory". The paths are correct and I'm able to include all headers from main.cpp. But I can't include eg header3 inside header1. – AGE_DK Sep 30 '21 at 04:57
  • That means you either specify the wrong path to the file or you didn't add the right include directories. Unless you show how you actually do it, only you can find out. Good luck! – moooeeeep Sep 30 '21 at 09:57