0

I have a very simple problem, which I don't find a solution for:

I'm working with a company that once in a while sends me two directories, one with .h files and the second with a library in two forms, shared and static. say -

X/include/*
X/lib/libX.so
X/lib/libX.a

I would like to wrap this input with a cmake project that does this simple action - create a cmake target that I can install, alias, version, etc.

Ran Regev
  • 361
  • 3
  • 13
  • What have you tried so far? Did you spend some time with the CMake documentation and attempt a solution? Please post the CMake code you have tried already. – Kevin Aug 20 '19 at 12:01
  • yes, sure, posting in stackoverflow is the last option. Take a look at https://cmake.org/cmake/help/latest/command/add_library.html - all requires source files, and the imported one does not allow adding files. – Ran Regev Aug 20 '19 at 12:39
  • also, couldn't find a way to naturally install or add dependencies for custom-target . – Ran Regev Aug 20 '19 at 12:40
  • Did you look closely at [`IMPORTED`](https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries) libraries? [This](https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/#sex-drugs-and-imported-targets) is a good introduction. – Botje Aug 20 '19 at 12:43
  • @Botje - I read it few times. it is good, but very hard to use as a reference. I'll reread it again, looking for answers – Ran Regev Aug 20 '19 at 13:13
  • Note, that CMake doesn't allow to install IMPORTED libraries. Just use `install(FILES)` for install your files. See that question for more info: https://stackoverflow.com/questions/41175354/can-i-install-shared-imported-library/41179630 – Tsyvarev Aug 20 '19 at 14:11
  • @Tsyvarev sure I can install the files, but then I loose the benefit of "target-oriented" cmake. I cannot EXPORT it as a target, I cannot ALIAS it, etc. – Ran Regev Aug 20 '19 at 14:52

1 Answers1

0

You may want to look here as it's explain pretty well how to import targets.

Something like this should work :

# Shared library
add_library(LibX-Shared SHARED IMPORTED)
set_target_properties(LibX-Shared PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "X/include"
  IMPORTED_LOCATION "X/lib/libX.so"
)

# Static library
add_library(LibX-Static STATIC IMPORTED)
set_target_properties(LibX-Static PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "X/include"
  IMPORTED_LOCATION "X/lib/libX.a"
)

Then you should be able to use these targets in CMake :

target_link_libraries(myexe LibX-Shared)
Blabdouze
  • 1,041
  • 6
  • 12