使用 pybind11 在 Python 脚本中调用 MAXON 库的正确 CMakeLists.txt 文件 [英] The correct CMakeLists.txt file to call a MAXON libarary in a Python script using pybind11

查看:25
本文介绍了使用 pybind11 在 Python 脚本中调用 MAXON 库的正确 CMakeLists.txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对整个 CMake 很陌生.关注这个这个a> 帖子,现在我想在 Python 中调用 MAXON 函数,使用 pybind11.到目前为止我做了什么:

I'm very new to the whole CMake. Following this and this posts, now I want to call a MAXON function inside Python, using pybind11. What I have done so far:

wget https://www.maxongroup.com/medias/sys_master/root/8837358518302/EPOS-Linux-Library-En.zip

  • 解压:
  • unzip EPOS-Linux-Library-En.zip
    

    • 使安装 shell 脚本可执行并运行它:
    • chmod +x ./install.sh
      sudo ./install.sh
      

      • 然后转到示例文件夹:
      • cd /opt/EposCmdLib_6.6.1.0/examples/HelloEposCmd/
        

        • 现在合并来自此处CMakeLists.txt文件:
        • # CMakeLists.txt
          
          cmake_minimum_required(VERSION 2.8.12)
          project (HelloEposCmd)
          
          set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
          set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
          
          set(CMAKE_POSITION_INDEPENDENT_CODE ON)
          
          find_package(pybind11 REQUIRED)
          pybind11_add_module(${PROJECT_NAME} HelloEposCmd.cpp)
          
          add_executable(${PROJECT_NAME} HelloEposCmd.cpp)
          
          target_link_libraries(${PROJECT_NAME} -lEposCmd)
          

          • HelloEposCmd.cpp 这一行添加在其他头文件之后:
            • and the HelloEposCmd.cpp this line is added right after other header files:
            • #include <pybind11/pybind11.h>
              

              主函数重命名为:

              int run(int argc, char** argv)
              

              以及pybind11添加模块的语法写在最后:

              and the pybind11 syntax to add the module is written at the end:

              PYBIND11_MODULE(HelloEposCmd, m) {
              
                  m.def("run", &run, "runs the HelloEposCmd");
              }
              

              但是,当我运行 cmake . 时出现错误:

              However, When I run the cmake . I get the error:

              CMakeLists.txt 中的 CMake 错误:13 (add_executable):

              CMake Error at CMakeLists.txt:13 (add_executable):

              add_executable 无法创建目标HelloEposCmd";因为另一个同名的目标已经存在.现有目标是在源目录/opt/EposCmdLib_6.6.1.0/examples/HelloEposCmd"中创建的模块库.如需了解详情,请参阅政策 CMP0002 的文档.

              add_executable can not create target "HelloEposCmd" because another target with the same name already exists. The existing target is a module library created in source directory "/opt/EposCmdLib_6.6.1.0/examples/HelloEposCmd" See documentation for policy CMP0002 for more details.

              ...

              我想知道您是否可以帮助我获得正确的 CMakeList.txt 文件.理想情况下,我应该能够在 python 中调用已编译的模块:

              I was wondering if you could be kind to help me get the right CMakeList.txt file. Ideally, I should be able to call the compiled module in python:

              # HelloEposCmd.py
              
              import HelloEposCmd
              
              HelloEposCmd.run()
              

              提前感谢您的支持.

              推荐答案

              pybind11_add_module 已经为你创建了一个目标.所以你不再需要 add_executable 了.只需删除该行,当您构建时,您将获得一个名为 HelloEposCmd

              pybind11_add_module already creates a target for you. So you don't need add_executable anymore. Just remove that line and when you will build you will get a library with the name HelloEposCmd

              add_executable 如果您正在构建可执行文件 (.exe),我认为这不是您想要的.

              add_executable is needed if you are building an executable (.exe), which I believe is not what you want.

              文档 pybind11 说.

              Documenation of pybind11 says.

              此函数的行为与 CMake 的内置 add_library 非常相似(实际上,它是该命令的包装函数).

              This function behaves very much like CMake’s builtin add_library (in fact, it’s a wrapper function around that command).

              这篇关于使用 pybind11 在 Python 脚本中调用 MAXON 库的正确 CMakeLists.txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆