Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a C++ library for a C application

Tags:

c++

c

I have a command line (+HTTP interface) audio application in C, which currently is being compiled with gcc on Mac OSX, but which I would like to keep this application linux compatible.

However, I would like to use the freeverb3 library. This is in C++. I would prefer not to convert all my code to C++. I don't (as far as I can see) need to call any C code from C++, nor will I need to use C++ objects in my C code. Simple method calls passing arrays of doubles plus a few ints as arguments will be all that I need in terms of interaction from my main application an the C++ code.

From some quick googling, it seems that I can write a C++ interface module, which can then expose some c compatible functions that I can call to make use of freeverb3. I"ve written a micro example to see how this might work. For this example, I have a dummy c++ file called test.cpp:

#include <iostream>
using namespace std;

class test_class
{
  int a;

  public:

    int get_a();
    void set_a( int v );
};

int test_class::get_a()
{
  return a;
}

void test_class::set_a( int v )
{
  a = v;
}

static test_class *c;

extern "C"
{
  void init();
  void set( int v );
  int get();
}

void init()
{
  c = new test_class();
}

void set( int v )
{
  c->set_a( v );
}

int get()
{
  return c->get_a();
}

I have a dummy c file that calls the functions:

#include <stdio.h>

/* Forward declaratoins for extern "C" functions in C++ code */

void init();
int get();
void set( int v );

/* C language code that references functions in C++ code */

int main()
{
  init();

  set( 55 );
  printf( "value: %d\n", get() );
  set( get() + 12 );
  printf( "value: %d\n", get() );
  return 0;
}

And, I have a makefile that creates an executable.

test: test.o user.o
    g++ -o test user.o test.o

test.o: test.cpp
    g++ -c test.cpp

user.o: user.c
    gcc -c user.c

Is this a good way of using C++ code from C? Is there a better/more sophisticated/more traditional way of achieving this aim?

like image 705
user3748029 Avatar asked Dec 06 '25 23:12

user3748029


2 Answers

You might want to think about it the other way. Write your higher level application in C++, invoke the C++ library where you want without complications and call all your current C modules from the C++ level.

IMHO, this is easier to achieve than doing the same with C as high level.

like image 51
Mast Avatar answered Dec 09 '25 14:12

Mast


If you intend to use more than one C++ object from C you need to pass an extra instance pointer (this) to the C wrapper functions:

struct A {
    A();
    ~A();
    void set(int);
};

The C wrapper:

extern "C"
{
    struct A* a_create(void);
    void a_destroy(struct A*);
    void a_set(struct A*, int);
}

You may also like to catch all C++ exceptions in the C wrapper functions and convert them to error codes.

like image 30
Maxim Egorushkin Avatar answered Dec 09 '25 12:12

Maxim Egorushkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!