我想这将是一个关于在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中)
所以,我尝试了一下这个:
ADD_CUSTOM_COMMAND(
OUTPUT scintilla.a
COMMAND ${CMAKE_MAKE_PROGRAM}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scintilla/gtk
COMMENT "Original scintilla makefile target" )
…但是,add_custom_command添加了“没有输出的目标”;所以我正在尝试几种方法来构建它,所有这些都失败了(作为注释给出的错误):
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)
我显然用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;但是,它专门从源文件构建可执行文件(我想避免).
# 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.