linux – 使用cmake重用静态库的自定义makefile

前端之家收集整理的这篇文章主要介绍了linux – 使用cmake重用静态库的自定义makefile前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想这将是一个关于在cmake中包含现有makefile的库的一般性问题;但这是我的背景 –

我试图将scintilla包含在另一个CMake项目中,我有以下问题:

Linux上,scintilla在(例如)${CMAKE_CURRENT_SOURCE_DIR} / scintilla / gtk目录中有一个makefile;如果您在该目录中运行make(像往常一样),您将获得${CMAKE_CURRENT_SOURCE_DIR} /scintilla/bin/scintilla.a文件 – 我猜这是静态库.

现在,如果我尝试使用cmake的ADD_LIBRARY,我必须在cmake中手动指定scintilla的来源 – 我宁愿不要弄乱它,因为我已经有了一个makefile.所以,我宁愿调用通常的scintilla make – 然后指示CMAKE以某种方式引用生成的scintilla.a. (我想这不会确保跨平台兼容性 – 但请注意,目前跨平台对我来说不是问题;我只想构建scintilla作为已经使用cmake的项目的一部分,仅在Linux中)

所以,我尝试了一下这个:

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

…但是,add_custom_command添加了“没有输出的目标”;所以我正在尝试几种方法来构建它,所有这些都失败了(作为注释给出的错误):

  1. ADD_CUSTOM_TARGET(scintilla STATIC DEPENDS scintilla.a) # Target "scintilla" of type UTILITY may not be linked into another target.
  2. ADD_LIBRARY(scintilla STATIC DEPENDS scintilla.a) # Cannot find source file "DEPENDS".
  3. ADD_LIBRARY(scintilla STATIC) # You have called ADD_LIBRARY for library scintilla without any source files.
  4. ADD_DEPENDENCIES(scintilla scintilla.a)

我显然用cmake引用了一个noob – 所以,是否有可能让cmake运行一个预先存在的makefile,并“捕获”它的输出文件,这样cmake项目的其他组件可以链接它?

非常感谢任何答案,
干杯!

编辑:可能重复:CMake: how do i depend on output from a custom target? – Stack Overflow – 但是,这里的破损似乎是由于需要专门有一个库,其余的cmake项目将识别…

>另一个相关:cmake – adding a custom command with the file name as a target – Stack Overflow;但是,它专门从源文件构建可执行文件(我想避免).

最佳答案
您还可以使用导入的目标和这样的自定义目标:

  1. # set the output destination
  2. set(SCINTILLA_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/scintilla/gtk/scintilla.a)
  3. # create a custom target called build_scintilla that is part of ALL
  4. # and will run each time you type make
  5. add_custom_target(build_scintilla ALL
  6. COMMAND ${CMAKE_MAKE_PROGRAM}
  7. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scintilla/gtk
  8. COMMENT "Original scintilla makefile target")
  9. # now create an imported static target
  10. add_library(scintilla STATIC IMPORTED)
  11. # Import target "scintilla" for configuration ""
  12. set_property(TARGET scintilla APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG)
  13. set_target_properties(scintilla PROPERTIES
  14. IMPORTED_LOCATION_NOCONFIG "${SCINTILLA_LIBRARY}")
  15. # now you can use scintilla as if it were a regular cmake built target in your project
  16. add_dependencies(scintilla build_scintilla)
  17. add_executable(foo foo.c)
  18. target_link_libraries(foo scintilla)
  19. # note,this will only work on linux/unix platforms,also it does building
  20. # in the source tree which is also sort of bad style and keeps out of source
  21. # builds from working.

猜你在找的Linux相关文章