Create & link interface library with cmake

hello,
is it possible to compile interface library only and target it in another "project" to make compilable by interface rules but without linking library physically?

since windows swift cannot link static library, there is no need to link libraries defining protocol / base class / ...

what i have discovered so far is the only option to build library:
add_library(BLA SHARED librarySource.swift) -> dll, lib, swiftmodule (interface i quess)

and consumer links dynamic only this way:
add_library(BaseClassInLibSwift SHARED IMPORTED)
set_target_properties(BaseClassInLibSwift PROPERTIES IMPORTED_IMPLIB pathTo.lib)
set_target_properties(BaseClassInLibSwift PROPERTIES IMPORTED_LOCATION pathTo.dll)
set_target_properties(BaseClassInLibSwift PROPERTIES INTERFACE_INCLUDE_DIRECTORIES pathToFolderWhere.swiftmodule exists)

target_link_libraries(MyProject PUBLIC BaseClassInLibSwift)

only using PROPERTIES INTERFACE_INCLUDE_DIRECTORIES allows me to declare "import MYMODULE_REPRESENTING_LIBRARY" in my target swift file

  1. static library only does not work
  2. shared library requires IMPORTED_IMPLIB path to lib file too
  3. .swiftmodule must be present

is it possible to generate INTERFACE library (compiler does not generate anything when calling add_library(A INTERACE B) - i would expected at least .swiftmodule file
AND
link only this interface using something like
set_target_properties(BaseClassInLibSwift PROPERTIES INTERFACE_INCLUDE_DIRECTORIES pathToFolderWhere.swiftmodule exists

there is no need to have physically dll or lib when dll is shared and external and allready compiled

thank you very much

The import library is always required for linking (if the target is a shared library). The DLL is never needed - it is a runtime only component meant for distribution (that is when you want to run the code).

The swift interface is always required for compiling.

Effectively the following is all you should need:

add_library(Project::Library SHARED IMPORTED)
set_target_properties(Project::Library PROPERTIES
  IMPORTED_IMPLIB project/out/lib/library.lib
  INTERFACE_INCLUDE_DIRECTORIES project/out/swift)
...
target_link_libraries(library PUBLIC
  Project::Library)

At some point, it will be possible to do static linking, in which case you will need the static library and the swift module/interface.

This isn't the only way to consume it of course - you can use CMake packages. You can also do variant imports or build and install tree configurations. The CMake documentation does cover all the various ways to use it and it can be quite useful to reference.

thank you, works

due my previous experimenting i was able to compile with both IMPORTED_IMPLIB & IMPORTED_LOCATION (path to dll) present only
so the error was caused by something else

Terms of Service

Privacy Policy

Cookie Policy