Support building both static and shared libs
nh2 opened this issue · 7 comments
Hi, thanks for the library!
in
Lines 82 to 89 in f657d47
Would it be possible to allow building both (I believe cmake supports that by default)?
This would make life for distribution packagers easier.
Thanks!
it makes building static and shared libs mutually exclusive.
As far as I'm aware, cmake doesn't offer a way to build both at once - one would have to implement that explicitly. (Unless I'm misunderstanding your request?)
(See add_library docs.)
If you know of a way to do it that I'm missing, please submit a PR and I'll take a look!
I think the standard way is using CMake's "object libraries" feature (scrolling down a bit further in your link), full example here:
I've never seen that as a "standard" way - and it creates a problem because you then need to use position-independent code for both. It's mentioned at the end of that response:
The price you pay is that the object files must be built as position-independent code because shared libraries need this (static libs don't care). Note that position-independent code may be less efficient, so if you aim for maximal performance then you'd go for static libraries.
Effectively, turning on -fPIC
for static builds turns off some optimizations.
To go back to the beginning - what is the actual problem you are trying to solve?
If you need both, can you not just build it twice - once as a static lib, once as a dynamic lib?
(What platform are you dealing with BTW?)
(What platform are you dealing with BTW?)
I'm packaging for NixOS.
If you need both, can you not just build it twice - once as a static lib, once as a dynamic lib?
Yes, avoiding to build twice and having to copy files together into the package is the main motivation.
In autoconf, for example, you can enable/disable static/shared lib output independent from each other, and if you enable both, you get both .a
and .so
files.
In autoconf...
cmake doesn't work anything like autoconf though. There is no way (again AFAIK!) to do what your asking in a clean way with cmake.
avoiding to build twice
It will always need to compile the files twice regardless because the compiler flags are different between static and shared builds. (It's 18 files and takes less than a minute to compile...)
and having to copy files together into the package
You shouldn't need to copy anything - it will install them to CMAKE_INSTALL_PREFIX
:
mkdir libE57Format-build-static
cd libE57Format-build-static
cmake -DCMAKE_INSTALL_PREFIX=/path/to/install/libE57 ../libE57Format
cmake --build .
cmake --install .
cd ..
mkdir libE57Format-build-shared
cd libE57Format-build-shared
cmake -DE57_BUILD_SHARED=ON -DCMAKE_INSTALL_PREFIX=/path/to/install/libE57 ../libE57Format
cmake --build .
cmake --install .
Now both static and shared libs are in /path/to/install/libE57.
If you find a clean way to do what you want to do (maybe cmake will introduce a way to do it sometime?), please feel free to submit a pull request for it.