The correct CMakeLists.txt file to call a MAXON libarary in a Python script using pybind11

后端 未结 2 1298
一整个雨季
一整个雨季 2020-12-12 05:55

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
相关标签:
2条回答
  • 2020-12-12 06:16

    Thanks to abhilb post and his kind followup in the comments I was able to figure the problem out. well, at least find a temporary workaround:

    • According to this post, the last two lines of the CMakeLists.txt file should change to
    # this line can be removed
    # add_executable(${PROJECT_NAME} HelloEposCmd.cpp)
    
    target_link_libraries(${PROJECT_NAME} PRIVATE -lEposCmd)
    
    • and then because according to this post pybind11 doesn't support double pointers we change the run function to:
    int run() {
    
        int argc = 1;
        char* argv[] = {"./HelloEposCmd"};
    
    ...
    }
    

    which I suppose to be a horrible workaround (inspired by information from this page). Now running cmake ., make and python3 HelloEposCmd.py should work properly (except a small c++ warning!).

    P.S.1. Maybe someone could use std::vector<std::string> as suggested here. This idea was proposed here and there are already some answers worth investigating.

    P.S.2. Following this discussion, another workaround could be something like:

    #include <stdio.h>
    #include <stdlib.h>
    
    void myFunc(int argc, char* argv[]) {
        for (int i = 0; i < argc; ++i) {
            printf("%s\n", argv[i]);
        }
    }
    
    int run(int argc, long* argv_) {
    
        char** argv = (char**)malloc(argc * sizeof(char*));
    
        for (int i = 0; i < argc; ++i) {
            argv[i] = (char*)(argv_[i]);
        }
    
        myFunc(argc, argv);
    
        free(argv);
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-12 06:19

    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 is needed if you are building an executable (.exe), which I believe is not what you want.

    Documenation of pybind11 says.

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

    0 讨论(0)
提交回复
热议问题