CMake:如何构建共享和静态库,而不需要重新编译源两次 [英] CMake: How can I build a shared and a static library without recompiling the sources twice

查看:651
本文介绍了CMake:如何构建共享和静态库,而不需要重新编译源两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要构建一个静态和共享版本的同一个库,如下所述是否可以让CMake构建同一个库的静态和共享版本?

I want to build both a static and shared version of the same library as described Is it possible to get CMake to build both a static and shared version of the same library?

但是,源代码被编译两次,每个版本不一样。有没有办法避免这种情况?

However, the sources are compiled twice, one for each version which is not necessary. Is there any way to avoid this?

目前我有:

add_library(${LIB} SHARED ${${LIB}_srcs})

add_library(${LIB}_static STATIC ${${LIB}_srcs})

我需要改变什么才能只需要编译一次? FYI。

What do I need to change in order to only need to compile once? FYI. I have the same compiler flags and defines.

推荐答案

由于CMake 2.8.8可以使用对象库:注意:将为一个库构建的对象文件重用到另一个lib目标

Since CMake 2.8.8 you can use Object Library: CMake: reuse object files built for a lib into another lib target.

另请参阅 http:// www.cmake.org/Wiki/CMake/Tutorials/Object_Library

这篇关于CMake:如何构建共享和静态库,而不需要重新编译源两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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