简体   繁体   中英

Call function outside of current namespace in C++

I'm trying to call a function defined in a C file from my CPP code and I think I am having issues getting the correct namespace. When compiling I get the error: "Undefined reference to 'Get'".

My C header:

// c.h
#ifndef C_H
#define C_H

#ifdef __cplusplus
    extern "C" {
#endif

typedef enum
{
    VAL_A1,
    VAL_A2
} TYPE_A;

typedef enum
{
    VAL_B1,
    VAL_B2
} TYPE_B;

typedef enum
{
    VAL_C1,
    VAL_C2
} TYPE_C;

typedef struct
{
    TYPE_B b;
    TYPE_C c;
} TYPE_D;

TYPE_A Get(TYPE_B b, TYPE_D *d);

#ifdef __cplusplus
    }
#endif

#endif

And my CPP file:

// main.cpp
...

extern "C" {
    #include "c.h"
}

...

namespace MyNamespace
{
    ...

    MyClass::MyFunc()
    {
        TYPE_D d;
        // None of these calls will compile
        // Get(VAL_B1, &d);
        // ::Get(VAL_B1, &d);
    }

    ...
}

I have tried calling without namespace reference and also with the "root" namespace using "::" with no luck. Any help is appreciated. I've read through this which seems to clarify it but I don't really understand it:

using C++ with namespace in C

"Undefined reference" means that the function has been declared (in the header), but not defined. You'll need to define the function in a source file somewhere (presumably the C file you refer to), and make sure that is linked when you build the program.

First, let's note what that error means. An undefined reference at the linker stage means that the compiler is unable to find the instance of something. In this case, the implementation of a function.

Let's look at your code.. There are a few things missing that we need to add to make it compilable:

  • A definition for Get() .
  • main()
  • The class definition for MyClass .

Once we added those three fixes, the code compiles without error.

extern "C" { extern "C" {
    typedef enum {
        VAL_A1,
        VAL_A2
    } TYPE_A;

    typedef enum {
        VAL_B1,
        VAL_B2
    } TYPE_B;

    typedef enum {
        VAL_C1,
        VAL_C2
    } TYPE_C;

    typedef struct {
        TYPE_B b;
        TYPE_C c;
    } TYPE_D;

    TYPE_A Get(TYPE_B b, TYPE_D *d) {
        return VAL_A1;
    }
}}

namespace MyNamespace {
    struct MyClass {
        void MyFunc();
    };

    void MyClass::MyFunc() {
        TYPE_D d;

        Get(VAL_B1, &d);
        ::Get(VAL_B1, &d);
    }
}

int main() {}

The definition of Get (not shown in the question) also needs to be enclosed in extern "C" .

The main difference between C and C++ functions, in practice, is the way they are named in the executable format. C++ functions get "name mangling" treatment by the linker but C functions do not. The linker will see the C++ definition of Get and it will have no idea of its relation to the C declaration, even if they have the same signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM