TheLartians/ModernCppStarter

Adding 3rd party libs to target_include_directories

mr-eyes opened this issue · 10 comments

How to properly add other local third-party libraries inside the main package? the main package is not a single header libarary.

issue_project
├── include
│   ├── main_lib1
│   │   └── mainlib.h
│   ├── main_lib2
│   │   └── mainlib2.h
│   └── main_project.h
└── thirdparty
    ├── lib1
    │   └── lib1.h
    └── lib2
        └── lib2.h

I tried this

target_include_directories(
  Greeter PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include;${THIRD_PARTY_LIBRARY}">
                 $<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

But with an error that the header file not found. However, if I tried to put "include_directories(${THIRD_PARTY_LIBRARY})" it will build the main Cmake but the test's Cmake will fail due to the following error:

#include <xx/x.h> No such file or directory

If I was not clear enough, I will create a simplified project to reproduce the issue.

Thanks,

Well, it seems to be fixed by this edit.

target_include_directories(
        Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include$<SEMICOLON>${Lib1_DIR}$<SEMICOLON>${Lib2_DIR}>
        $<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

IMHO You should not mix concepts.

use CPM.cmake to add your third party sw.

that works fine, see #80

That seems neat!
But, will CPM.cmake work with non-Cmake projects?

it is possible, see asio package at CPM.cmake

Thanks, @ClausKlein the mixed concepts worked fine except for the installation. I will try the CPM.cmake and update this issue.

That seems neat!
But, will CPM.cmake work with non-Cmake projects?

Yes, CPM may configured to try find_package() as first.

An possible solutions for the problem to find a cmake import config package of a non cmake project
is here https://github.com/ClausKlein/modern-cmake-sample/blob/develop/libjsonutils/CMakeLists.txt

The doku is here https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/

I have been working around and mimicking those solutions for about 4 hours and still failing to get it worked.

The issue can be reproduced here In this commit. I will be glad if I can get some help getting this working.

Another question: Does the third party libraries in the package installation stored by defauls in /usr/local/include/<thirdParty_lib> and not in /usr/local/include/Greeter-1.0/thirdParty_lib ?

After adding IMPORTED the build succeeded but testing the installed library failed.

https://github.com/mr-eyes/issue_86/blob/87e1e895397732198e64bfec4c7f74a6f2896ea7/CMakeLists.txt#L52

The error:

CMake Error at /usr/share/cmake-3.16/Modules/CMakeFindDependencyMacro.cmake:47 (find_package):
  By not providing "Findkseq.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "kseq", but
  CMake did not find one.

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

    kseqConfig.cmake
    kseq-config.cmake

  Add the installation prefix of "kseq" to CMAKE_PREFIX_PATH or set
  "kseq_DIR" to a directory containing one of the above files.  If "kseq"
  provides a separate development package or SDK, be sure it has been
  installed.
Call Stack (most recent call first):
  /usr/local/lib/cmake/Greeter/GreeterConfig.cmake:33 (find_dependency)
  CMakeLists.txt:25 (find_package)

You should close the this issue, it works as it should.

see https://github.com/ClausKlein/issue_86/blob/feature/show-the-problem/CMakeLists.txt

Thanks so much @ClausKlein !