How to get path to object files with CMake for both multiconfiguration generator and makefile based ones?

后端 未结 1 1888
闹比i
闹比i 2021-01-03 02:20

I would like to generate a module definition file based on all symbols available in object files in dynamic fashion (think of GTKMM\'s gendef).

For this, I would lik

相关标签:
1条回答
  • 2021-01-03 02:48

    Turning my comment into an answer

    Makefile projects generated by CMake have a totally different internal structure then solutions/projects generated for Visual Studio. I think this is neither a bug nor a feature, those structures are just optimized for their usecases.

    And as far as I know there is no easy CMake internal way to get the list of object files or the path to the intermediate files directory with e.g. reading a target property.

    So I have taken your code example and have done some testing for alternatives with CMake 3.3.2 using Visual Studio 14 2015 and NMake Makefiles generators.

    Alternatives

    1. One related discussion on the CMake mailing list named "CMake: Is there an elegant way to get list of object files participating into a library?" does suggest using an intermediate static library:

      add_library(tgtlib STATIC tgt.c)
      add_custom_command(
          OUTPUT tgt.def
          COMMAND gendef tgt.def $<TARGET_FILE_NAME:tgt> $<TARGET_FILE:tgtLib> 
      )
      file(WRITE dummy.c "")
      add_library(tgt SHARED dummy.c tgt.def)
      target_link_libraries(tgt tgtlib)
      
    2. You could add build environment specific elements to your PRE_LINK step:

      if(CMAKE_CONFIGURATION_TYPES)
          set(_obj_files "$(IntermediateOutputPath)*.obj")
      else()
          set(_obj_files "$?")
      endif()
      add_custom_command(
          TARGET MainProject 
          PRE_LINK
          COMMAND gendef tgt.def $<TARGET_FILE_NAME:tgt> ${_obj_files}
      )
      

    References

    • NMAKE: Filename Macros
    0 讨论(0)
提交回复
热议问题