# Modifying cmake file to create packageConfig.cmake and friends

**URL:** <https://discourse.cmake.org/t/modifying-cmake-file-to-create-packageconfig-cmake-and-friends/14270>\
**Category:** Code\
**Created:** [June 20, 2025, 1:54pm UTC](https://discourse.cmake.org/t/modifying-cmake-file-to-create-packageconfig-cmake-and-friends/14270 "2025-06-20T13:54:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![perdrix](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@perdrix](https://discourse.cmake.org/u/perdrix)\
**Post date:** [June 20, 2025, 1:54pm UTC](https://discourse.cmake.org/t/modifying-cmake-file-to-create-packageconfig-cmake-and-friends/14270/1 "2025-06-20T13:54:16Z")

</div>

I am working to support the use of find\_package with a particular project.

I added a file called SmtpMimeConfig.cmake which reads:

```auto
include(CMakeFindDependencyMacro)
# find_dependency(xxx 2.0)
include(${CMAKE_CURRENT_LIST_DIR}/SmtpMimeTargets.cmake)

```

The end of the main cmake file used to read:

```auto
include(GNUInstallDirs)
install(TARGETS ${LIBRARY_TARGET_NAME}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/smtpmime
)

```

I’ve changed that to read:

```auto
include(GNUInstallDirs)
install(TARGETS ${LIBRARY_TARGET_NAME}
	EXPORT SmtpMimeTargets
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/smtpmime
)
	
install(EXPORT SmtpMimeTargets
	NAMESPACE SmtpMime::
	DESTINATION lib/cmake/smtpmime 
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    "SmtpMimeConfigVersion.cmake"
    VERSION ${smtpmime_VERSION}
    COMPATIBILITY AnyNewerVersion)
	
install(FILES "SmtpMimeConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/SmtpMimeConfigVersion.cmake"
    DESTINATION lib/cmake/smtpmime)	

```

which appears to work, but could I have done that better/more tidily?

Thanks  
David

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [June 20, 2025, 2:16pm UTC](https://discourse.cmake.org/t/modifying-cmake-file-to-create-packageconfig-cmake-and-friends/14270/2 "2025-06-20T14:16:39Z")

</div>

You could have also generated the CMake config with [configure\_package\_config\_file()](https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html) instead of writing it manually.

As for the being tidy, my [installation module](https://github.com/retifrav/cmake-target-link-libraries-example/blob/759f3c68af73738724a5ce0ac91bfd3ca66b079e/dpndnc/cmake/Installing.cmake) is even longer, but it doesn’t bother me at all. If that what it takes - so be it, I wouldn’t be considerably happier if I could do all that with less lines.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 20, 2025, 2:47pm UTC](https://discourse.cmake.org/t/modifying-cmake-file-to-create-packageconfig-cmake-and-friends/14270/3 "2025-06-20T14:47:31Z")

</div>

> [@retif](#):
>
> As for the being tidy, my [installation module](https://github.com/retifrav/cmake-target-link-libraries-example/blob/759f3c68af73738724a5ce0ac91bfd3ca66b079e/dpndnc/cmake/Installing.cmake) is even longer, but it doesn’t bother me at all. If that what it takes - so be it, I wouldn’t be considerably happier if I could do all that with less lines.

yes, you are right!

But be prepared to install `FILE_SET HEADER` and `CXX_MODULES` too.

---

<div class="post-metadata">

**Author:** ![perdrix](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@perdrix](https://discourse.cmake.org/u/perdrix)\
**Post date:** [June 20, 2025, 4:43pm UTC](https://discourse.cmake.org/t/modifying-cmake-file-to-create-packageconfig-cmake-and-friends/14270/4 "2025-06-20T16:43:39Z")

</div>

Could you give a specific example targeted to this case please? I much prefer simple solutions 🙂

Please include the packageConfig.cmake.in in your example …

David

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [June 20, 2025, 4:46pm UTC](https://discourse.cmake.org/t/modifying-cmake-file-to-create-packageconfig-cmake-and-friends/14270/5 "2025-06-20T16:46:18Z")

</div>

It is all part of the linked repository, is it not.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 20, 2025, 4:57pm UTC](https://discourse.cmake.org/t/modifying-cmake-file-to-create-packageconfig-cmake-and-friends/14270/6 "2025-06-20T16:57:01Z")

</div>

> [@perdrix](#):
>
> I much prefer simple solutions

```cmake
if(ALGO_USE_MODULES)
  ##################################################
  add_library(Algo SHARED)
  add_library(my_package::Algo ALIAS Algo)
  target_sources(Algo PRIVATE algo-impl.cpp PUBLIC FILE_SET CXX_MODULES FILES algo-interface.cppm)

  # CMake requires the language standard to be specified as compile feature
  # when a target provides C++23 modules and the target will be installed
  target_compile_features(Algo PUBLIC cxx_std_23)

  if(CMAKE_CXX_MODULE_STD)
    target_compile_definitions(Algo PUBLIC HAS_IMPORT_STD)
  endif()

  # XXX target_link_libraries(Algo PUBLIC $<BUILD_INTERFACE:a4z::commonCompilerWarnings>)

  include(GenerateExportHeader)
  generate_export_header(Algo CUSTOM_CONTENT_FROM_VARIABLE)
  target_sources(
    Algo PUBLIC FILE_SET HEADERS BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR} FILES
                ${CMAKE_CURRENT_BINARY_DIR}/algo_export.h
  )

  if(NOT CMAKE_SKIP_INSTALL_RULES)
    include(CPack)
    include(GNUInstallDirs)
    include(CMakePackageConfigHelpers)

    write_basic_package_version_file(
      "my_package-config-version.cmake" COMPATIBILITY SameMajorVersion
    )

    install(
      TARGETS Algo
      EXPORT my_package-targets
      # ... a few details omitted, see the "Deep CMake For Library Authors" talk
      FILE_SET CXX_MODULES
      # There's currently no convention for this location, see discussion below
      DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/my_package/src
      CXX_MODULES_BMI
      DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/my_package/bmi-${CMAKE_CXX_COMPILER_ID}_$<CONFIG>
      FILE_SET HEADERS
      DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # Same as default, could be omitted
      INCLUDES
      DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    )
    install(EXPORT my_package-targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/my_package
            CXX_MODULES_DIRECTORY . NAMESPACE my_package::
    )
    install(FILES "${PROJECT_BINARY_DIR}/my_package-config-version.cmake"
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/my_package
    )
    # The following file includes the my_package-targets.cmake file
    install(FILES cmake/my_package-config.cmake
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/my_package
    )
  endif()

  if(PROJECT_IS_TOP_LEVEL)
    enable_testing()
    add_subdirectory(example)
  endif()

  ##################################################
endif()

```

`cmake/my_package-config`:

```cmake
# include(CMakeFindDependencyMacro)
# only if runtime dependency needed: i.e. find_dependency(fmt)

include("${CMAKE_CURRENT_LIST_DIR}/my_package-targets.cmake")

```
