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

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

问题描述

我对整个CMake都是新手.跟随帖子,现在我想使用pybind11在Python中调用MAXON函数.到目前为止,我所做的事情:

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:

  • The library can be downloaded from this page (direct download link).
wget https://www.maxongroup.com/medias/sys_master/root/8837358518302/EPOS-Linux-Library-En.zip

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

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

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

        • 现在在此处中组合CMakeLists.txt个文件:
          • Now combining the CMakeLists.txt files from here:
          • # 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:13(添加可执行文件)上的CMake错误:

                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,我认为这不是您想要的.

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

                模仿说.

                此函数的行为与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天全站免登陆