是否有可能获得CMake来构建同一个库的静态和共享版本? [英] Is it possible to get CMake to build both a static and shared version of the same library?

查看:194
本文介绍了是否有可能获得CMake来构建同一个库的静态和共享版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相同的来源,所有,只是想要一个静态和共享版本。很容易做?

Same source, all that, just want a static and shared version both. Easy to do?

推荐答案

是的,它很容易。只需使用两个add_library命令:

Yes, it's moderately easy. Just use two "add_library" commands:

add_library(MyLib SHARED source1.c source2.c)
add_library(MyLibStatic STATIC source1.c source2.c)

即使您有许多源文件,

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.

在Windows上,你应该给每个库一个不同的名称,因为有一个.lib文件用于共享和静态。但在Linux和Mac上,你甚至可以给这两个库相同的名称(例如libMyLib.a和libMyLib.so):

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 and libMyLib.so):

set_target_properties(MyLibStatic PROPERTIES OUTPUT_NAME MyLib)

但我不建议同时给出静态和动态版本库同名。我喜欢使用不同的名称,因为这使得更容易在链接到库的工具的编译行上选择静态与动态链接。通常我选择像libMyLib.so(shared)和libMyLib_static.a(static)。 (这些将是linux上的名称。)

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) and libMyLib_static.a (static). (Those would be the names on linux.)

这篇关于是否有可能获得CMake来构建同一个库的静态和共享版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆