1

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

6
  • 1
    Why do you have extern "C" in both the header and the .cpp file? Anyway, the error means that it is able to find the declaration, but not a definition of the function. It seems you are not linking in the definition of Get. Commented Jul 16, 2014 at 14:34
  • 1
    Where is the implementation of your Get function ? You are misunderstanding the error here. Commented Jul 16, 2014 at 14:36
  • What is D? The function expects a pointer to TYPE_D. Commented Jul 16, 2014 at 14:37
  • @JosephMansfield I am not sure, I do not really know what I am doing but rather going by trial and error (tested with only one place before). Commented Jul 16, 2014 at 14:39
  • 1
    @rattmuff: And how are you compiling and linking the two files? Commented Jul 16, 2014 at 14:40

3 Answers 3

2

"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.

Sign up to request clarification or add additional context in comments.

Comments

0

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() {}

2 Comments

extern "C" { extern "C" { is nonsense, and that function definition is not suitable for a header.
@Potatoswatter: extern "C" { extern "C" { is what their code preprocesses to. And it's not nonsense, it's just unnecessary.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.