我想这将是一个关于在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" )
Run Code Online (Sandbox Code Playgroud)
...但是,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)
Run Code Online (Sandbox Code Playgroud)
我显然用cmake引用了一个noob - 所以,是否有可能cmake
运行一个预先存在的makefile,并"捕获"它的输出库文件,这样cmake项目的其他组件可以链接它?
非常感谢任何答案,
干杯!
编辑:可能重复:CMake:我如何依赖自定义目标的输出?- 堆栈溢出 - 然而,这里的破损似乎是由于需要专门拥有一个库,其余的cmake项目将识别...
小智 9
您还可以使用导入的目标和这样的自定义目标:
# 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.
Run Code Online (Sandbox Code Playgroud)