为cmake重用静态库的自定义makefile [英] Reusing custom makefile for static library with cmake

查看:1543
本文介绍了为cmake重用静态库的自定义makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这将是一个通用的问题,包括现有的makefiles在cmake中的库;但这里是我的上下文 -



我试图在另一个CMake项目中包括 scintilla 问题:



在Linux上,scintilla有一个makefile(例如) $ {CMAKE_CURRENT_SOURCE_DIR} / scintilla / gtk 目录;如果你在该目录下运行 make (像往常一样),你会得到一个 $ {CMAKE_CURRENT_SOURCE_DIR} /scintilla/bin/scintilla.a file - which(我猜)是静态库。



现在,如果我尝试使用cmake的 ADD_LIBRARY ,我必须手动指定闪烁在cmake - 我不想打扰,因为我已经有一个makefile。所以,我宁愿调用普通的闪烁 make - 然后指示CMAKE以某种方式引用结果 scintilla.a 。 (我想这将确保跨平台兼容性 - 但请注意,目前的跨平台不是我的问题;我只是想建立闪烁作为这一部分





因此, ve尝试了一下:

  ADD_CUSTOM_COMMAND(
OUTPUT scintilla.a
COMMAND $ {CMAKE_MAKE_PROGRAM}
WORKING_DIRECTORY $ {CMAKE_CURRENT_SOURCE_DIR} / scintilla / gtk
COMMENT原始scintilla makefile目标)


$ b b

...但add_custom_command会添加 target with no output ;所以我尝试几种方法来建立,所有的失败(错误给出作为评论):

  ADD_CUSTOM_TARGET STATIC DEPENDS scintilla.a)#类型为UTILITY的目标scintilla可能不会链接到另一个目标。 

ADD_LIBRARY(scintilla STATIC DEPENDS scintilla.a)#找不到源文件DEPENDS。

ADD_LIBRARY(scintilla STATIC)#对于没有任何源文件的库scintilla,您调用了ADD_LIBRARY。
ADD_DEPENDENCIES(scintilla scintilla.a)



我显然用cmake引用了一个noob,所以,有可能有 cmake 预先存在的makefile,以及捕获其输出库文件,以便cmake项目的其他组件可以链接到它?



非常感谢任何答案,

干杯!





编辑:可能重复: CMake:我如何依赖自定义目标的输出? - Stack Overflow - 但是,这里的破坏似乎是由于需要专门有一个,其余的cmake项目会识别...




解决方案

p>您还可以使用导入的目标和一个自定义目标,如下所示:

 #设置输出目标
set SCINTILLA_LIBRARY $ {CMAKE_CURRENT_SOURCE_DIR} /scintilla/gtk/scintilla.a)
#创建一个名为build_scintilla的自定义目标,它是所有
#的一部分,并在每次输入make时运行
add_custom_target(build_scintilla ALL
COMMAND $ {CMAKE_MAKE_PROGRAM}
WORKING_DIRECTORY $ {CMAKE_CURRENT_SOURCE_DIR} / scintilla / gtk
注释原始scintilla makefile目标)

#现在创建一个导入的静态目标
add_library(scintilla STATIC IMPORTED)
#为配置导入目标scintilla
set_property(TARGET scintilla APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG)
set_target_properties(scintilla PROPERTIES
IMPORTED_LOCATION_NOCONFIG $ {SCINTILLA_LIBRARY})

#现在你可以使用scintilla作为项目中的常规cmake构建目标
add_dependencies(scintilla build_scintilla)

add_executable(foo foo.c)
target_link_libraries(foo scintilla)

#注意,这只能在linux / unix平台上工作,它还在源代码树中构建
#这也是一种不好的风格,并保持源的
#构建从工作。


I guess this would be a generic question on including libraries with existing makefiles within cmake; but here's my context -

I'm trying to include scintilla in another CMake project, and I have the following problem:

On Linux, scintilla has a makefile in (say) the ${CMAKE_CURRENT_SOURCE_DIR}/scintilla/gtk directory; if you run make in that directory (as usual), you get a ${CMAKE_CURRENT_SOURCE_DIR}/scintilla/bin/scintilla.a file - which (I guess) is the static library.

Now, if I'd try to use cmake's ADD_LIBRARY, I'd have to manually specify the sources of scintilla within cmake - and I'd rather not mess with that, given I already have a makefile. So, I'd rather call the usual scintilla make - and then instruct CMAKE to somehow refer to the resulting scintilla.a. (I guess that this then would not ensure cross-platform compatibility - but note that currently cross-platform is not an issue for me; I'd just like to build scintilla as part of this project that already uses cmake, only within Linux)

 

So, I've tried a bit with this:

ADD_CUSTOM_COMMAND(
  OUTPUT scintilla.a
  COMMAND ${CMAKE_MAKE_PROGRAM}
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scintilla/gtk
  COMMENT "Original scintilla makefile target" )

... but then, add_custom_command adds a "target with no output"; so I'm trying several approach to build upon that, all of which fail (errors given as comment):

ADD_CUSTOM_TARGET(scintilla STATIC DEPENDS scintilla.a) # Target "scintilla" of type UTILITY may not be linked into another target.

ADD_LIBRARY(scintilla STATIC DEPENDS scintilla.a) # Cannot find source file "DEPENDS".

ADD_LIBRARY(scintilla STATIC) # You have called ADD_LIBRARY for library scintilla without any source files.
ADD_DEPENDENCIES(scintilla scintilla.a)

 

I'm obviously quote a noob with cmake - so, is it possible at all to have cmake run a pre-existing makefile, and "capture" its output library file, such that other components of the cmake project can link against it?

Many thanks for any answers,
Cheers!

 

EDIT: possible duplicate: CMake: how do i depend on output from a custom target? - Stack Overflow - however, here the breakage seems to be due to the need to specifically have a library that the rest of the cmake project would recognize...

解决方案

You could also use imported targets and a custom target like this:

# set the output destination
set(SCINTILLA_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/scintilla/gtk/scintilla.a)
# create a custom target called build_scintilla that is part of ALL
# and will run each time you type make 
add_custom_target(build_scintilla ALL 
                   COMMAND ${CMAKE_MAKE_PROGRAM}
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scintilla/gtk
                   COMMENT "Original scintilla makefile target")

# now create an imported static target
add_library(scintilla STATIC IMPORTED)
# Import target "scintilla" for configuration ""
set_property(TARGET scintilla APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG)
set_target_properties(scintilla PROPERTIES
  IMPORTED_LOCATION_NOCONFIG "${SCINTILLA_LIBRARY}")

# now you can use scintilla as if it were a regular cmake built target in your project
add_dependencies(scintilla build_scintilla)

add_executable(foo foo.c)
target_link_libraries(foo scintilla)

# note, this will only work on linux/unix platforms, also it does building
# in the source tree which is also sort of bad style and keeps out of source 
# builds from working.  

这篇关于为cmake重用静态库的自定义makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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