Creat C++ command for python fail

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
randy19962
Posts: 33
Joined: Fri Oct 30, 2015 9:40 am

Creat C++ command for python fail

Post by randy19962 »

Hello,I want to make a C++ command and call by python
I read the code AppDraftUtilsPy.cpp and try to modified it.
However I always get the First Error
" undefined reference to `Py::ExtensionModuleBase::module() const' "
But I have no idea about how to fix the problem
Here is my code for make the c++ command, for now The command did not do anything.

Could anyone give me some hints?

Code: Select all


#include "PreCompiled.h"
#ifndef _PreComp_
# include <Python.h>
#endif

#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>

namespace MyCommand {
class Module : public Py::ExtensionModule<Module>
{
public:
    Module() : Py::ExtensionModule<Module>("MyCommand")
    {
        add_varargs_method("Command",&Module::Command,
            "Nothing"
        );
        initialize("nothing"); // register with Python
    }

    virtual ~Module() {}

private:
    Py::Object Command(const Py::Tuple& args)
    {
        return Py::None();
    }
};

PyObject* initModule()
{
    return (new Module)->module().ptr();
}

}
mumme
Posts: 35
Joined: Sun Jun 21, 2015 4:52 pm

Re: Creat C++ command for python fail

Post by mumme »

randy19962 wrote:However I always get the First Error
" undefined reference to `Py::ExtensionModuleBase::module() const' "
But I have no idea about how to fix the problem
I don't know if it's it but I got a error like this during linking, turns out i had to include the moc file generated by the build system

like in this file:
https://github.com/FreeCAD/FreeCAD/blob ... r.cpp#L476

See last line
randy19962
Posts: 33
Joined: Fri Oct 30, 2015 9:40 am

Re: Creat C++ command for python fail

Post by randy19962 »

mumme wrote:
randy19962 wrote:However I always get the First Error
" undefined reference to `Py::ExtensionModuleBase::module() const' "
But I have no idea about how to fix the problem
I don't know if it's it but I got a error like this during linking, turns out i had to include the moc file generated by the build system

like in this file:
https://github.com/FreeCAD/FreeCAD/blob ... r.cpp#L476

See last line

Thank you very much mumme, Finally I find the problem. I also read this post.
viewtopic.php?t=7143
It is linking problem and my code is below.

c++ file

Code: Select all

/*#include <Base/Console.h> */
#include <iostream>
#include <Python.h>
#include <FCConfig.h>
#include <Base/PyObjectBase.h>

static PyObject * open(PyObject *self, PyObject *args)
{
    std::cerr << "Import Test Open" << std::endl;
    Py_Return;
}

struct PyMethodDef Test_Import_methods[] = {
    {"open"       ,open ,       METH_VARARGS, "Some description"},
    {NULL, NULL}  /* sentinel */
};

PyDoc_STRVAR(module_doc,
"Copied from Mesh\n"
"\n");

/* Python entry */
extern "C" {
void initimportTest()
{
    PyObject* importTest = Py_InitModule3("importTest", Test_Import_methods, module_doc);   /* mod name, table ptr */
    /*Base::Console().Log("Loading Mesh module... done\n");*/
    std::cerr << "Loading Test .. done\n" << std::endl;

}
}
cmake list

Code: Select all


if(MSVC)
    add_definitions(-DHAVE_ACOSH -DHAVE_ASINH -DHAVE_ATANH -D_CRT_SECURE_NO_WARNINGS)
else(MSVC)
    add_definitions(-DHAVE_LIMITS_H -DHAVE_CONFIG_H)
endif(MSVC)


include_directories(
    ${CMAKE_BINARY_DIR}
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_BINARY_DIR}/src
    ${CMAKE_CURRENT_BINARY_DIR}
    ${Boost_INCLUDE_DIRS}
    ${PYTHON_INCLUDE_DIRS}
)

set(importTest_LIBS

    FreeCADApp
)


SET(importTest_SRCS
    importTest.cpp

)

add_library(importTest SHARED ${importTest_SRCS})
target_link_libraries(importTest ${importTest_LIBS})


SET_BIN_DIR(importTest importTest /Mod/Test)
SET_PYTHON_PREFIX_SUFFIX(importTest)

INSTALL(TARGETS importTest DESTINATION ${CMAKE_INSTALL_LIBDIR})
Post Reply