How to create findable imported libraries

Hi, I am working in a project where we have some autogenerated libraries that are created outside of cmake.
I want to make them available within CMake , with some cmake find for the rest of the project.
I also want the import creation to be automatic so I don’t have to manually hack the files.
I tried creating some .cmake containing add_library(lib STATIC IMPORTED)
So I can link my project but I want it to be a real findable package:
For normal targets you can do install(TARGETS lib EXPORT ,)
But lib is not accepted, all I want is to create the nice Package files so it’s possible to use find on it for other CMakeList.txt to refer to it.
Is there some info on how to create this, preferable a function in CMake?
Either a function or some info on what a Package should contain, most examples are on CMake targets and not imported ones…

Hello, Patrik, you may try to do something like:

# FindMyLib.cmake - helper Find-module for MyLib.


Finds the MyLib.

Input variables
MyLib_DIR - the hint directory in which the MyLib may be found.

Imported Targets

This module provides the following imported targets, if found:

  The MyLib library.

Result Variables

This will define the following variables:

  True, if the system has the MyLib library.
  The path for founded library.

find_library(MyLib_LIBRARY MyLib HINTS MyLib_DIR)

# helper for reporting the `find_package(MyLib)`
# resulting status.

  # create the IMPORTED target
  add_library(MyLib UNKNOWN IMPORTED)
  set_target_properties(MyLib PROPERTIES
  add_library(MyLib::MyLib ALIAS MyLib)

This is the FindModule file which will be used by CMake for find_package().
It must be in directory with another modules, and this directory must be inside CMAKE_MODULE_PATH for succesful find.

See the cmake-developer(7) for CMake module example (the previous code was based on that example), also consider the “Using dependencies Guide”, section “Imported Targets” and set_target_properties().

Hope that helps.

Please write if it has helped you or if there were some errors.