Same source, all that, just want a static and shared version both. Easy to do?
相关问题
- What are the recommended GNU linker options to spe
- Avoid cmake to add the flags -search_paths_first a
- confusion over System.Web.HttpContext.Current
- CMakeList file to generate LLVM bitcode file from
- How to fix NDK build error for HelloCardboard samp
相关文章
- Private static variables in php class
- Passing static array in attribute
- Java “static import” vs. “import static” in Java 8
- Target requires the language dialect “CXX17” (with
- Why does Java's List have “List.toArray()”, bu
- How to apply Static Weaving Ant Task with Eclipse-
- How do I tell cmake not to create a console window
- “Access of shared member, constant member, enum me
It is indeed possible. As @Christopher Bruns said in his answer, you need to add two versions of the library:
Then, as described here, you need to specify that both targets should use the same output name and not overwrite each other's files:
This way, you will get both libmylib.a and libmylib.so (on Linux) or mylib.lib and mylib.dll (on Windows).
Since CMake version 2.8.8, you can use "object libraries" to avoid the duplicated compilation of the object files. Using Christopher Bruns' example of a library with two source files:
From the CMake docs:
Simply put, the
add_library(objlib OBJECT ${libsrc})
command instructs CMake to compile the source files to*.o
object files. This collection of*.o
files is then referred to as$<TARGET_OBJECT:objlib>
in the twoadd_library(...)
commands that invoke the appropriate library creation commands that build the shared and static libraries from the same set of object files. If you have lots of source files, then compiling the*.o
files can take quite long; with object libraries you compile them only once.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. Furthermore, it is easier to distribute statically linked executables.
Yes, it's moderately easy. Just use two "add_library" commands:
Even if you have many source files, you would place the list of sources in a cmake variable, so it's still easy to do.
On Windows you should probably give each library a different name, since there is a ".lib" file for both shared and static. But on Linux and Mac you can even give both libraries the same name (e.g.
libMyLib.a
andlibMyLib.so
):But I don't recommend giving both the static and dynamic versions of the library the same name. I prefer to use different names because that makes it easier to choose static vs. dynamic linkage on the compile line for tools that link to the library. Usually I choose names like
libMyLib.so
(shared) andlibMyLib_static.a
(static). (Those would be the names on linux.)There is generally no need to duplicate
ADD_LIBRARY
calls for your purpose. Just make use ofwhile building, first (in one out-of-source directory) with
-DBUILD_SHARED_LIBS:BOOL=ON
, and withOFF
in the other.