如何避免CMake将对象的完整路径写入静态库?

How to avoid CMake writting full path of objects to the static library?

提问人:Duke M 提问时间:4/19/2023 更新时间:4/19/2023 访问量:66

问:

原来的问题在这里

几天后,我发现问题就在这里。 我尝试使用以下CMakeLists.txt来构建:tcmalloc_minimal_static.lib

#...
# https://github.com/gperftools/gperftools/blob/master/CMakeLists.txt#L593
if(MINGW OR MSVC)
  set(WINDOWS_INCLUDES
#       ...
          src/windows/preamble_patcher.h)
  set(libwindows_la_SOURCES ${WINDOWS_INCLUDES}
#       ...
          src/windows/preamble_patcher_with_stub.cc)
  add_library(windows_object OBJECT ${libwindows_la_SOURCES})
  add_library(windows INTERFACE)
  target_sources(windows INTERFACE $<TARGET_OBJECTS:windows_object>)
  # patch_functions.cc uses Psapi.lib.  MSVC has a #pragma for that, but not us.
  target_link_libraries(windows INTERFACE psapi)
#...

# https://github.com/gperftools/gperftools/blob/master/CMakeLists.txt#L811
set(libtcmalloc_minimal_internal_la_SOURCES src/common.cc
#       ...
        ${TCMALLOC_MINIMAL_INCLUDES})
> add_library(tcmalloc_minimal_internal_object OBJECT ${libtcmalloc_minimal_internal_la_SOURCES})
# We #define NO_TCMALLOC_SAMPLES, since sampling is turned off for _minimal.
target_compile_definitions(tcmalloc_minimal_internal_object PRIVATE NO_TCMALLOC_SAMPLES NO_HEAP_CHECK NDEBUG)
add_library(tcmalloc_minimal_internal INTERFACE)
target_link_libraries(tcmalloc_minimal_internal INTERFACE ${LIBSPINLOCK} ${maybe_threads_lib})
target_sources(tcmalloc_minimal_internal INTERFACE $<TARGET_OBJECTS:tcmalloc_minimal_internal_object>)
#...

# https://github.com/gperftools/gperftools/blob/master/CMakeLists.txt#L847
if(GPERFTOOLS_BUILD_STATIC)
    add_library(tcmalloc_minimal_static STATIC ${libtcmalloc_minimal_internal_la_SOURCES})
    target_compile_definitions(tcmalloc_minimal_static PRIVATE NO_TCMALLOC_SAMPLES NDEBUG)
    target_link_libraries(tcmalloc_minimal_static PRIVATE tcmalloc_minimal_internal Threads::Threads)
    if(MINGW)
      target_link_libraries(tcmalloc_minimal_static PRIVATE stacktrace)
    endif()
    if(NOT MSVC)
      set_target_properties(tcmalloc_minimal_static PROPERTIES
              OUTPUT_NAME tcmalloc_minimal)
    endif()
    weaken_object(tcmalloc_minimal_static)
    install(TARGETS tcmalloc_minimal_static)
endif()

然后我弄错了.由7z.exe打开:tcmalloc_minimal_static.lib

tcmalloc_minimal_static.lib
 - F:/
   - *full/path/to/my/project/*
     - build/Release/
       - _deps/
         - gperftools-build/
           - tcmalloc_minimal_internal_object.dir/
              - Release/
                - central_freelist.obj
                - ...
           - windows_object.dir/
              - Release/
                - ia32_modrm_map.obj
                - ...
 - tcmalloc_minimal_static.dir/
   - Release/
     - central_freelist.obj
     - ...
 - 1.txt
 - 2.txt

这太荒谬了。然后 objcopy.exe 根本无法读取这个库。

我试图添加到cmake启动参数。但它根本没有用。太多其他问题与共享库有关,但我面临着两个静态库。 我只想删除库中那些长长的绝对路径,以便 objcopy.exe 可以读取它。-DCMAKE_SKIP_RPATH

C++ cmake objcopy tcmalloc gperftools

评论

0赞 Tsyvarev 4/19/2023
对于库,您可以指定两次变量的源:1. .2. ,该库的对象与 INTERFACE 的对象来自相同的来源。您可以在树中看到重复项:文件从不同的位置列出两次。tcmalloc_minimal_staticlibtcmalloc_minimal_internal_la_SOURCESadd_library(tcmalloc_minimal_static STATIC ${libtcmalloc_minimal_internal_la_SOURCES})target_link_libraries(tcmalloc_minimal_static PRIVATE tcmalloc_minimal_internal Threads::Threads)tcmalloc_minimal_internaltcmalloc_minimal_static.libcentral_freelist.obj
0赞 Botje 4/19/2023
我不明白问题到底是什么。文件路径在很大程度上与最终库无关。
0赞 Friedrich 4/19/2023
请澄清您的具体问题或提供其他详细信息以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。

答: 暂无答案