是否可以让 CMake 同时构建静态库和共享库? [英] Is it possible to get CMake to build both a static and shared library at the same time?

查看:53
本文介绍了是否可以让 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)

即使你有很多源文件,你也可以将源列表放在一个 Cmake 变量中,所以还是很容易做到的.

Even if you have many source files, you can place the list of sources in a Cmake variable, so it's still easy to do.

在 Windows 上,您可能应该给每个库一个不同的名称,因为有一个.lib".共享和静态文件.但是在 Linux 和 Mac 上,您甚至可以为两个库指定相同的名称(例如 libMyLib.alibMyLib.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(共享)和 libMyLib_static.a(静态)这样的名称.(这些将是 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天全站免登陆