2

Following is my C++ code:

#include<iostream>
#include<string.h>
extern "C" void wrapper(struct NB*, int );

struct AssetDbFilter {
    std::string attribute;
    std::string value;
  };

class NB {

public:
    void myTest(int a);
};

Following is C++ code:

#include<iostream>
#include<string.h>
 #include "test.h"
using namespace std;

void NB :: myTest (int a) {

cout << a;
}
void wrapper(NB* nb, int a) {
nb->myTest(a);
}

Following is my c code:

#include<stdio.h>
#include "test.h"
void wrapper(struct NB* ,int );
int main()
{
    AssetDbFilter* assetdb;
    struct NB* nb;
    wrapper(nb, 5);
}

I am not able to execute this code. I am calling C++ member function from a C code. When I execute this C file as g++ test.c I am getting following error:

/tmp/ccr1IaLa.o: In function `main':
test1.c:(.text+0x15): undefined reference to `wrapper'
collect2: error: ld returned 1 exit status

Can anyone tell me how can I resolve this?

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jan 15, 2019 at 22:09

1 Answer 1

4

You built test.c, great! Now you need to build the C++ file as well so that definitions are present.

Currently your toolchain thinks you want to compile and link test.c into an application, but test.c isn't your whole source code.

You can compile test.c without linking:

gcc test.c -c

Then do the same with your C++ file:

g++ whatever.cpp -c

Then link them together to get an executable:

g++ test.o whatever.o -o myProgram

Unfortunately your program will still not work because you are invoking a member function on a dangling/uninitialised pointer. Since you cannot instantiate an NB from your C code, this approach can't work.

I recommend NB contain only C-compatible members. Then you can put its definition into a shared header file and instantiate it whereever you please.

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

3 Comments

... or just delegate object creation to C++: extern "C" struct NB* createNB() { return new NB(); }
IMHO it's worth of noting, for the sake of completness, that in pure c++ producing object files for external-linking is technically counterproductive, because there is name mangling and (I think) argument passing mode which are not standardized, so finding and using compiled functions is fragile. Keywords C++ ABI, external and internal linkage. The counter measure is of course to specify C-linkage, which c++-compliant compiler should provide and has its own implications.
@rustyx in which case be careful to also provide freeNB or similarly named function and document it in immediate vicinity of createNB, lest a C user of the API becomes confused about how to deal with the memory.

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.