In CMake, can I mark a library as static after add_library'ing it?
Asked Answered
E

1

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?

Eozoic answered 22/4, 2023 at 8:25 Comment(2)
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?Feces
@local-ninja: I've never set BUILD_SHARED_LIBS, and yet somehow dynamic libs were created...Eozoic
L
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.

Lederhosen answered 22/4, 2023 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.