Reusing custom makefile for static library with cmake

后端 未结 2 1463
礼貌的吻别
礼貌的吻别 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.  
    
    0 讨论(0)
  • 2021-02-06 11:10

    OK, I think I have it somewhat; basically, in the CMakeLists.txt that build scintilla, I used this only:

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

    ... and then, the slightly more complicated part, was to find the correct cmake file elsewhere in the project, where the ${PROJECT_NAME} was defined - so as to add a dependency:

    ADD_DEPENDENCIES(${PROJECT_NAME} scintilla.a)
    

    ... and finally, the library needs to be linked.

    Note that in the commands heretofore, the scintilla.a is merely a name/label/identifier/string (so it could be anything else, like scintilla--a or something); but for linking - the full path to the actual `scintilla.a file is needed (which in this project ends up in a variable ${SCINTILLA_LIBRARY}). In this project, the linking basically occurs through a form of a

    list(APPEND PROJ_LIBRARIES ${SCINTILLA_LIBRARY} )
    

    ... and I don't really know how cmake handles the actual linking afterwards (but it seems to work)

     

    For consistency, I tried to use ${SCINTILLA_LIBRARY} instead of scintilla.a as identifier in the ADD_CUSTOM_TARGET, but got error: "Target names may not contain a slash. Use ADD_CUSTOM_COMMAND to generate files". So probably this could be solved smarter/more correct with ADD_CUSTOM_COMMAND - however, I read that it "defines a new command that can be executed during the build process. The outputs named should be listed as source files in the target for which they are to be generated."... And by now I'm totally confused so as to what is a file, what is a label, and what is a target - so I think I'll leave at this (and not fix it if it ain't broken :) )

    Well, it'd still be nice to know a more correct way to do this eventually,
    Cheers!

    0 讨论(0)
提交回复
热议问题