cmake Pybind11链接错误:在创建共享对象时,不能使用针对隐藏符号'_ZTI9nan_error'的重定位R_X86_64_32

ogsagwnx  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(232)

尊敬的StackOverflow社区:
我想使用pybind 11从一个c代码创建一个python模块,在我的例子中,引导目录名为process_hclust,里面有一个src文件夹,包含所需的c文件。


* process_hclust (main folder)

  | setup.py
  | CMakeLists.txt
  * src
   | main.cpp
 **hclust-cpp
     | CMakeLists.txt
     | fastcluster.h
     | fastcluster.cpp
  • 主要的CMakeLists.txt在“src/main.cpp”中添加了pybind 11模块,并链接了第二个CMakeLists.txt中所需的库(据我所知)。
cmake_minimum_required(VERSION 3.22.1)
project(process_hclust)

include(FetchContent)
FetchContent_Declare(
    pybind11
    GIT_REPOSITORY https://github.com/pybind/pybind11
    GIT_TAG        v2.2.3
)

FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
    FetchContent_Populate(pybind11)
    add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()

pybind11_add_module(${PROJECT_NAME} src/main.cpp)

add_subdirectory(src/hclust-cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE fastcluster)
  • hclust-cpp中的第二个. CMakeLists.txt文件声明需要链接哪些文件。
add_library(fastcluster fastcluster.cpp fastcluster.h)
  • 在main.cpp文件中,我使用以下命令调用头文件

# include "hclust-cpp/fastcluster.h"

setup.py只调用了cmake。

这段代码可以在我的本地计算机上运行我在那里安装了python3.9。它可以通过简单的使用来运行:

pip install ./process_hclust

但是,在使用我的大学机器和python3.7安装模块时,我收到以下错误:

Processing ./cpp/process_hclust
  Preparing metadata (setup.py) ... done
Using legacy 'setup.py install' for process-hclust, since package 'wheel' is not installed.
Installing collected packages: process-hclust
  Attempting uninstall: process-hclust
    Found existing installation: process-hclust 0.0.0
    Uninstalling process-hclust-0.0.0:
      Successfully uninstalled process-hclust-0.0.0
  Running setup.py install for process-hclust ... error
  error: subprocess-exited-with-error

  × Running setup.py install for process-hclust did not run successfully.
  │ exit code: 1
  ╰─> [57 lines of output]
      running install
      running build
      running build_ext
      -- pybind11 v2.2.3
      -- Configuring done
      -- Generating done
      -- Build files have been written to: /afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/cpp/process_hclust/build/temp.linux-x86_64-3.7/process_hclust
      Consolidate compiler generated dependencies of target fastcluster
      [ 50%] Built target fastcluster
      Consolidate compiler generated dependencies of target process_hclust
      [ 75%] Linking CXX shared module ../../lib.linux-x86_64-3.7/process_hclust.cpython-37m-x86_64-linux-gnu.so
      /usr/bin/ld: src/hclust-cpp/libfastcluster.a(fastcluster.cpp.o): relocation R_X86_64_32 against hidden symbol `_ZTI9nan_error' can not be used when making a shared object
      /usr/bin/ld: final link failed: Nonrepresentable section on output
      collect2: error: ld returned 1 exit status
      gmake[2]:***[CMakeFiles/process_hclust.dir/build.make:98: ../../lib.linux-x86_64-3.7/process_hclust.cpython-37m-x86_64-linux-gnu.so] Error 1
      gmake[1]:***[CMakeFiles/Makefile2:118: CMakeFiles/process_hclust.dir/all] Error 2
      gmake:***[Makefile:91: all] Error 2
      Traceback (most recent call last):
        File "<string>", line 36, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/cpp/process_hclust/setup.py", line 137, in <module>
          python_requires=">=3.6",
        File "/afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/env/lib/python3.7/site-packages/setuptools/__init__.py", line 145, in setup
          return distutils.core.setup(**attrs)
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/core.py", line 148, in setup
          dist.run_commands()
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/dist.py", line 966, in run_commands
          self.run_command(cmd)
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/env/lib/python3.7/site-packages/setuptools/command/install.py", line 61, in run
          return orig.install.run(self)
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/command/install.py", line 545, in run
          self.run_command('build')
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/command/build.py", line 135, in run
          self.run_command(cmd_name)
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/env/lib/python3.7/site-packages/setuptools/command/build_ext.py", line 78, in run
          _build_ext.run(self)
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/command/build_ext.py", line 340, in run
          self.build_extensions()
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/command/build_ext.py", line 449, in build_extensions
          self._build_extensions_serial()
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/distutils/command/build_ext.py", line 474, in _build_extensions_serial
          self.build_extension(ext)
        File "/afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/cpp/process_hclust/setup.py", line 122, in build_extension
          subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=build_temp)
        File "/opt/crc/p/python/3.7.3/gcc/lib/python3.7/subprocess.py", line 347, in check_call
          raise CalledProcessError(retcode, cmd)
      subprocess.CalledProcessError: Command '['cmake', '--build', '.']' returned non-zero exit status 2.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  Rolling back uninstall of process-hclust
  Moving to /afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/env/lib/python3.7/site-packages/process_hclust-0.0.0-py3.7.egg-info
   from /afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/env/lib/python3.7/site-packages/~rocess_hclust-0.0.0-py3.7.egg-info
  Moving to /afs/crc.nd.edu/user/j/jmarti53/Public/brain_project/LINKPROJECT/python/env/lib/python3.7/site-packages/process_hclust.cpython-37m-x86_64-linux-gnu.so
   from /tmp/pip-uninstall-2ijatdvx/process_hclust.cpython-37m-x86_64-linux-gnu.so
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> process-hclust

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

我相信主要问题是在

[ 75%] Linking CXX shared module ../../lib.linux-x86_64-3.7/process_hclust.cpython-37m-x86_64-linux-gnu.so
      /usr/bin/ld: src/hclust-cpp/libfastcluster.a(fastcluster.cpp.o): relocation R_X86_64_32 against hidden symbol `_ZTI9nan_error' can not be used when making a shared object
      /usr/bin/ld: final link failed: Nonrepresentable section on output

我对pybind 11、cmake和构建python库的了解不多,这让我认为问题是在程序链接hclust-cpp目录,寻找fastcluster.h头文件时出现的,但我还是不完全明白发生了什么。
我已经尝试解决这个问题好几天了,所以任何关于该怎么做的建议都将受到高度赞赏;如果问题令人困惑和/或需要更多信息,请让我知道。
提前问候!

3zwjbxry

3zwjbxry1#

我亲爱的StackOverflow社区,
我解决了这个问题。它通过在第二个CMakeLists.txt中添加SHARED得到了解决

add_library(fastcluster SHARED fastcluster.cpp fastcluster.h)

现在,它起作用了!!!我太高兴了!!

相关问题