Reusing custom makefile for static library with cmake

后端 未结 2 1498
礼貌的吻别
礼貌的吻别 2021-02-06 10:45

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

2条回答
  •  迷失自我
    2021-02-06 11:05

    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.  
    

提交回复
热议问题