0

In CMake, we can write:

add_library(mylib mylib.c)

for a dynamically-linked (shared) library, and

add_library(mylib STATIC mylib.c)

for a static one. But - can we start with the first syntax, then later mark the mylib target as static?

starball
  • 20,030
  • 7
  • 43
  • 238
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • You first snippet does not create a dynamic libary, only if `BUILD_SHARED_LIBS` is set. Maybe this is exactly what you want to achieve? – local-ninja Apr 22 '23 at 22:42
  • @local-ninja: I've never set BUILD_SHARED_LIBS, and yet somehow dynamic libs were created... – einpoklum Apr 23 '23 at 04:34

1 Answers1

0

My educated guess (I feel pretty confident right now, but I don't want to come off as too confident) is that this is not possible.

For one thing, you can trace how the type argument of the add_library command gets parsed and eventually used to set data in internal CMake structures. Then you can try to see where else that data member is modified/modifiable and then look for "vectors" (in the "attack vector" sense of the word) into that execution path. I found none. Here's the call hierarchy for what I just mentioned:

  • Source/cmAddLibraryCommand.cxx cmAddLibraryCommand

  • Source/cmMakefile.cxx cmMakefile::AddLibrary

  • Source/cmMakefile.cxx cmMakefile::AddNewTarget

  • Source/cmMakefile.cxx cmMakefile::CreateNewTarget

  • Source/cmTarget.cxx cmTarget::cmTarget, which does this->impl->TargetType = type;

  • If you search for the regex "\bTargetType\s*=[^=]" in the rest of source code, you'll only see it in that previously mentioned cmTarget::cmTarget constructor.

And there's the TYPE target property, but it's read-only. You'll get an error if you attempt to modify it.

starball
  • 20,030
  • 7
  • 43
  • 238