how to generate ITK_INCLUDE_DIRS when packaging myself module?

I am packaging myself module by cmake.

My CMakeLists.txt is:


install( TARGETS pA pB
	EXPORT SustaoBasic
	RUNTIME DESTINATION bin
	LIBRARY DESTINATION lib
	ARCHIVE DESTINATION lib
)

install(DIRECTORY package/ DESTINATION include
	FILES_MATCHING PATTERN "*.h"
)

install(EXPORT SustaoBasic
	FILE SustaoBasic.cmake
	NAMESPACE SustaoBasic::
	DESTINATION lib/cmake
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
	SustaoBasicConfigVersion.cmake
	VERSION V1.0
	COMPATIBILITY AnyNewerVersion  # 表示该函数库向下兼容
)

configure_package_config_file(
	SustaoBasicConfig.cmake.in ${PROJECT_BINARY_DIR}/SustaoBasicConfig.cmake
	INSTALL_DESTINATION lib/cmake
)

install(FILES "${PROJECT_BINARY_DIR}/SustaoBasicConfig.cmake"
              "${PROJECT_BINARY_DIR}/SustaoBasicConfigVersion.cmake"
        DESTINATION lib/cmake)

Then, I can package it in installation path.

In additional project, the find_package(SustaoBasic) is OK.

When I #include "pA.h", vs reports bug:

“pA.h”: No such file or directory

But, I am sure pA.h is included in install\include, and my install\include is:

install
    include 
        pA
            pA.h
        pB
            pB.h

Then, I want to find solution from ITK project. I find ITK generates a ITK_INCLUDED_DIRS variable. How can I generate the similar variable in my project?

Any suggestion is appreciated~~~

This is historical. There is no need for such a variable anymore. Instead, target_include_directories(pA PUBLIC "$<INSTALL_INTERFACE:include/pA>") is probably what you want.

Thank you for you kindly suggestion.

But I feel some confuzed about how to use target_include_directories(pA PUBLIC "$<INSTALL_INTERFACE:include/pA>").

In following description, packagePro is the project I need to generate package. 'packageTestis the project where I want to usepackagePro` package.

I have tried two experiments:

  1. I add target_include_directories(pA PUBLIC "$<INSTALL_INTERFACE:include/pA>") in the CMakeLists.txt of packagePro, which is:
...
install(DIRECTORY package/ DESTINATION include
	FILES_MATCHING PATTERN "*.h"
)

install(EXPORT SustaoBasic
	FILE SustaoBasic.cmake
	NAMESPACE SustaoBasic::
	DESTINATION lib/cmake
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
	SustaoBasicConfigVersion.cmake
	VERSION V1.0
	COMPATIBILITY AnyNewerVersion  # 表示该函数库向下兼容
)

configure_package_config_file(
	SustaoBasicConfig.cmake.in ${PROJECT_BINARY_DIR}/SustaoBasicConfig.cmake
	INSTALL_DESTINATION lib/cmake
)

install(FILES "${PROJECT_BINARY_DIR}/SustaoBasicConfig.cmake"
              "${PROJECT_BINARY_DIR}/SustaoBasicConfigVersion.cmake"
        DESTINATION lib/cmake)

# add -------------------------------------------------------------------------------------
target_include_directories(pA PUBLIC "$<INSTALL_INTERFACE:include/pA>")

The CMakeLists.txt of packageTest is:

project(PackageTest)
find_package(SustaoBasic)
add_executable(PackageTest main.cpp)

After building, the vs still reports “pA.h”: No such file or directory.

  1. I add target_include_directories(pA PUBLIC "$<INSTALL_INTERFACE:include/pA>") in the CMakeLists.txt of packageTest:
project(PackageTest)

find_package(SustaoBasic)
add_executable(PackageTest main.cpp)

target_include_directories(PackageTest PUBLIC "$<INSTALL_INTERFACE:include/pA>")

But, it still report “pA.h”: No such file or directory.

Where should I add target_include_directories(pA PUBLIC "$<INSTALL_INTERFACE:include/pA>")?
In addition, what path is INSTALL_INTERFACE? Is the SustaoBasic_DIR set in CMake?

You need to link to the pA target.

1 Like

@ben.boeckel Thank you for you kindly reply, and I finally solve my problem.

The solution is:

  1. add target_include_directories(PackageTest PUBLIC "$<INSTALL_INTERFACE:include/pA>") in CMakeLists.txt of packagePro.
  2. link to pA target in CMakeLists.txt of packageTest:
project(PackageTest)

find_package(SustaoBasic)

add_executable(PackageTest main.cpp)
target_link_libraries(PackageTest SustaoBasic::pA)

@ben.boeckel

target_include_directories(pA PUBLIC "$<INSTALL_INTERFACE:include/pA>") can solve current problem.

The include folder is:

Package
  pA
  pB

Is it possible to include Package directory when find_package() or target_link_libraries(PackageTest SustaoBasic)?

Currently, I must target_link_libraries(PackageTest SustaoBasic::pA) to specify the pA. Can I link all libraries, just link target_link_libraries(Pro ${ITK_LIBRARIES})?

I find it better to link to the targets directly rather than through some variable. Fewer moving parts.