access C++ objects within C. but linking error.

This is a discussion on access C++ objects within C. but linking error. within the C Programming forums, part of the General Programming Boards category; Hello everybody, My goal is accessing c++ objects within c by using wrapper and externals. To get the pointer to ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    Question access C++ objects within C. but linking error.

    Hello everybody,
    My goal is accessing c++ objects within c by using wrapper and externals. To get the pointer to the c++ object I use a type "void *".
    But i get an error while linking: undefined reference to "create_mycpp".

    Does anybody has an idea what the problem is? Or should I take an other way to access c++ objects?

    Code:
    //-------------------------------------
    //mycpp.cpp
    #include "mycpp.h"
    Mycpp::void func(int i)
    {
        i += 1;
    }
    
    //-------------------------------------
    //mycpp.h
    #ifndef MYCPP_H
    #define MYCPP_H
    #endif
    class Mycpp {
        public:
            void func(int);
    }
    
    //-------------------------------------
    //mywrapper.h
    #ifndef MYWRAPPER_H
    #define MYWRAPPER_H
    #endif
    typedef void * tMYCPP;
    tMYCPP create_mycpp();
    void mycpp_func(tMYCPP, int);
    
    //-------------------------------------
    //mywrapper.cpp
    #include "mywrapper.h"
    #include "mycpp.h"
    #ifdef __cplusplus
    extern "C" {
        void * create_mycpp() {
            return (tMYCPP)new Mycpp();
        }
        void mycpp_func(void * _mycpp, int _value) {
            ((Mycpp*) _mycpp)->func(_value);
        }
    }
    #endif
    
    //-------------------------------------
    //main.c
    #include "mywrapper.h"
    tMYCPP aCpp = create_mycpp();
    mycpp_func(aCpp, 2);
    
    //-------------------------------------

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Your mywrapper.h file needs the same extern "C" scope that your mywrapper.cpp file has.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Quote Originally Posted by Salem View Post
    Your mywrapper.h file needs the same extern "C" scope that your mywrapper.cpp file has.
    Hello Salem,
    have still same error : undefined reference to `create_mycpp' with:

    Code:
    #ifndef MYWRAPPER_H
    #define MYWRAPPER_H
    #endif
    
    #ifdef __cplusplus
    extern "C" {
        typedef void * tMYCPP;
        tMYCPP create_mycpp();
        void mycpp_func(tMYCPP, int);
    }
    #endif

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    shouldn't this :
    Code:
    Mycpp::void func(int i)
    be this:
    Code:
    void Mycpp::func(int i)
    and here is usual convention for an include file that is used in both C and cpp files
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    typedef void * tMYCPP;
    tMYCPP create_mycpp();
    void mycpp_func(tMYCPP, int);
    #ifdef __cplusplus
    }
    #endif

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    *shrug*
    When I fixed some syntax errors, it seems fine here.
    Code:
    // -------------------------------
    // mycpp.cpp
    // -------------------------------
    //-------------------------------------
    //mycpp.cpp
    #include "mycpp.h"
    void Mycpp::func(int i)
    {
        i += 1;
    }
    // -------------------------------
    // mycpp.h
    // -------------------------------
    #ifndef MYCPP_H
    #define MYCPP_H
    class Mycpp {
        public:
            void func(int);
    };
    #endif
    // -------------------------------
    // mywrapper.cpp
    // -------------------------------
    //-------------------------------------
    //mywrapper.cpp
    #include "mywrapper.h"
    #include "mycpp.h"
    #ifdef __cplusplus
    extern "C" {
        void * create_mycpp() {
            return (tMYCPP)new Mycpp();
        }
        void mycpp_func(void * _mycpp, int _value) {
            ((Mycpp*) _mycpp)->func(_value);
        }
    }
    #endif
    // -------------------------------
    // mywrapper.h
    // -------------------------------
    //-------------------------------------
    //mywrapper.h
    #ifndef MYWRAPPER_H
    #define MYWRAPPER_H
    extern "C" {
      typedef void * tMYCPP;
      tMYCPP create_mycpp();
      void mycpp_func(tMYCPP, int);
    }
    #endif
    // -------------------------------
    // foo.cpp
    // -------------------------------
    #include "mywrapper.h"
    int main ( ) {
      tMYCPP aCpp = create_mycpp();
      mycpp_func(aCpp, 2);
    }
    $ 
    $ g++ -Wall foo.cpp mycpp.cpp mywrapper.cpp
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Thank you very much dmh2000 and Salem now it works fine. The problem was also that i called gcc as compiler. i thought gcc can manage c++ files too?

    gcc -Wall foo.c mycpp.cpp mywrapper.cpp -> gives errors.

    Quote Originally Posted by Salem View Post
    *shrug*
    When I fixed some syntax errors, it seems fine here.
    Code:
    // -------------------------------
    // mycpp.cpp
    // -------------------------------
    //-------------------------------------
    //mycpp.cpp
    #include "mycpp.h"
    void Mycpp::func(int i)
    {
        i += 1;
    }
    // -------------------------------
    // mycpp.h
    // -------------------------------
    #ifndef MYCPP_H
    #define MYCPP_H
    class Mycpp {
        public:
            void func(int);
    };
    #endif
    // -------------------------------
    // mywrapper.cpp
    // -------------------------------
    //-------------------------------------
    //mywrapper.cpp
    #include "mywrapper.h"
    #include "mycpp.h"
    #ifdef __cplusplus
    extern "C" {
        void * create_mycpp() {
            return (tMYCPP)new Mycpp();
        }
        void mycpp_func(void * _mycpp, int _value) {
            ((Mycpp*) _mycpp)->func(_value);
        }
    }
    #endif
    // -------------------------------
    // mywrapper.h
    // -------------------------------
    //-------------------------------------
    //mywrapper.h
    #ifndef MYWRAPPER_H
    #define MYWRAPPER_H
    extern "C" {
      typedef void * tMYCPP;
      tMYCPP create_mycpp();
      void mycpp_func(tMYCPP, int);
    }
    #endif
    // -------------------------------
    // foo.cpp
    // -------------------------------
    #include "mywrapper.h"
    int main ( ) {
      tMYCPP aCpp = create_mycpp();
      mycpp_func(aCpp, 2);
    }
    $ 
    $ g++ -Wall foo.cpp mycpp.cpp mywrapper.cpp
    $

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Thank you very much dmh2000 and Salem now it works fine. The problem was also that i called gcc as compiler. i thought gcc can manage c++ files too?

    gcc -Wall foo.c mycpp.cpp mywrapper.cpp -> gives errors.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    How to mix C and C++, C++ FAQ
    You must use g++ when compiling main, and invoking the linker.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking Objects between Vectors - Best method
    By Woetren in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2011, 10:24 AM
  2. Linking with two objects
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-07-2006, 06:42 AM
  3. Linking objects to your C++ projects
    By Dwizard in forum Windows Programming
    Replies: 5
    Last Post: 09-24-2005, 07:11 PM
  4. Linking objects internally
    By bennyandthejets in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 01:34 AM
  5. linking objects
    By doleman19 in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2001, 11:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21