Linking problem while calling C function in CPP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalyani1031
    New Member
    • Nov 2007
    • 4

    #1

    Linking problem while calling C function in CPP

    hi, my problem is linking my cli module with XORP modules.

    I am calling my cli_main() in the XORP router manager module rtrmgr.
    It takes the files and compiles the files that are in the router manager
    but it gives undefined references to some objects or methods which r in the other modules like libxorp libxipc ... etc.

    I wrote Makefiles for each module when I run global cli Makefile rtrmgr Makefile didn't link with the libxorp module.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You cannot call C functions in C++ unless C++ has been told that they are C functions. Otherwise, the function name gets altered (mangled) into a has name. The C++ program uses the hash name so when you call using the C name a match is never found and ytou have an unresolved external reference.

    All C functions to be called in C++ must be qualified as extern "C" in C++.

    Comment

    • kalyani1031
      New Member
      • Nov 2007
      • 4

      #3
      hi,I already included extern "C" eventhough I got some undefined references while
      I am calling the method which is in one module and the definition of that method is in the other module. It gives some linking problem like

      : undefined reference to `EventLoop::Eve ntLoop()'
      245 ./rtrmgr/librtrmgr.a(mai n_rtrmgr1.o)(.t ext+0x4c9):src/main_rtrmgr1.cc :198: undefined refe rence to `EventLoop::~Ev entLoop()'

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You cannot call class member functions from C.

        That's because the first argument of the member function is a hidden argument that is the object's this pointer. This argument cannot be specified by you. Therefore, you can't call the member function unbkles you are in C++.

        You will need to write a wrapper on the C++ side. Call the wrapper from C, the wrapper calls the member function. The object's this pointer would be passed as a void*.

        Assume your C++ class is EventLoop and you want to create an EventLoop object by calling EventLoop::Even tLoop() from C. Here is the workaround:
        [code=c]
        /* on the C side: */
        void* EventLoop; /*the EventLoop object
        EventLoop = CreateEventLoop ();

        /* on the C++ side */
        void* CreateEventLoop ()
        {
        return (void*) new EventLoop;
        }
        [/code]

        Now to call int EventLoop::Meth odA(int) you would write a wrapper:
        [code=c]
        /* on the C++ side: */
        extern "C"
        int EventLoopMethod A(void* obj, int arg)
        {
        EventLoop* temp = (EventLoop*)obj ;
        return obj->MethodA(arg) ;
        }

        /* on the C side: */
        void* EventLoop; /*the EventLoop object
        EventLoop = CreateEventLoop ();
        int result = EventLoopMethod A(EventLoop, 3);
        [/code]

        Comment

        Working...