0

I am writting a "library" in C++ that is supposed to be used also by C code. I have followed the process as such:
- Using g++ and gcc compilers (as they are compatible).
- Used extern "C" in functions to be used by C code.
- My top level uses a wrapper function with signature that can be understood by a C compiler.
- Compiled to objective files the C++ code (with g++).
- Compiled my client code file with gcc.
- Edit: Tried/want to link them with gcc (or ld) (and not g++).

An MCV example would be the following:

mymalloc.h

#include <stdlib.h> /* for size_t */

#ifdef __cplusplus
extern "C"
#endif
void *mymalloc(size_t cbytes);

#ifdef __cplusplus
extern "C"
#endif
void myfree(void *ptr);

allocator.cpp

#include "allocator.h"
#include "mymalloc.h"

Allocator *  Allocator::instance = 0;

extern "C" void *mymalloc(size_t cbytes){
    Allocator *allocator = Allocator::getInstance();
    return allocator->allocateFor(cbytes);
}

extern "C" void myfree(void *ptr){
    Allocator *allocator = Allocator::getInstance();
    allocator->freePtr(ptr);
}

...

That source file defines the functions to be used by C code, as well as methods of allocator.h which by itself includes other C++ stuff.

My C client file (cclient.c) is:

#include <stdio.h>
#include "mymalloc.h"

int main(void){
    void *p = mymalloc(500);
    printf("%p\n", p);
    return 0;
}

I was under the impression that since I have those wrapper functions at my top level and since their declarations can be viewed by C code everything should be ok but I am getting linking errors: (sample/example shown)

./libmymalloc.a(allocator.o): In function `Allocator::getInstance()':
allocator.cpp:(.text+0x3cf): undefined reference to `operator new(unsigned long)'
allocator.cpp:(.text+0x3fa): undefined reference to `operator delete(void*, unsigned long)'
./libmymalloc.a(allocator.o):(.data.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'

The way I am compiling and linking, in favor of clarity, is

g++ -c allocator.cpp
... (same for the rest .cpp files)
gcc -c cclient.c
gcc cclient.o allocator.o ...(all other c++ objectives)...

Edit (After first three answers): I know that I can link using g++. I would be interested in how to link with the C compiler, so that someone having available only gcc and the objectives (or a library from those) could link on his own.

(This question could be a duplicate, but I have read all other questions in SO and I couldn't deduct what I was doing wrong.)

2
  • What's in allocator.h? And calling C++ code from C is really not a good idea. Commented Oct 24, 2017 at 21:23
  • @NeilButterworth Allocator class and include's to other header (C++). Yes, I am aware of that. Commented Oct 24, 2017 at 21:36

3 Answers 3

3

You did not post your command lines but I think that -lstdc++ (that is invoked automatically by g++) is missing from those.

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

1 Comment

Using g++ to link instead of gcc would work, and you do imply that. I think your answer would be better to actually state that simply linking with g++ instead of gcc also works.
2

This looks like the allocator Allocator uses new and delete, and the link does not include a C++ library to satisfy the missing functions.

Comments

2

Your problem is here:

gcc cclient.o allocator.o ...(all other c++ objectives)...

You are using the C compiler to link the objects.
This knows nothing about C++ or the libraries it uses.

g++ cclient.o allocator.o ...(all other c++ objectives)...

That should work as it not only links the objects but also includes the C++ standard libraries (that include new and delete).

1 Comment

That is true, I suppose it wasn't inferred from the question, but I would like (if possible) to use gcc for compiling (and linking) and not g++ -- Going to edit that

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.