Skip to contentSkip to conversation

mixing C/C++

DISCUSSION Elias Athanasopoulos 6 participants 8 messages Nov 8, 2003 – Nov 13, 2003

lore
Share & tools
lore
#0 lore raw

Hello!

I want to create Ruby bindings for a C++ project, so my first step is to call C++ code from C, since Ruby has a plain C API.

Consider I have a Foo class which its implementation is compiled in a shared lib (libtest.so). I have a second wrapper lib:

#include "libtest.h"

extern "C" class Foo * wrap_foo_ctor() { return new Foo(); } extern "C" void wrap_foo_set_food(Foo *f, int i) { f->set_food(i); } extern "C" int wrap_foo_hello(Foo *f) { return f->hello(); }

Using the above my C program is:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct Foo *m = (struct Foo*) wrap_foo_ctor();

  wrap_foo_set_food(m, 10);
  wrap_foo_hello(m);

  free(m);

return 1;
}

And:

elathan@velka:~/src/bindings> gcc foo.c -Wall -ltest -lwrap -o foo -L/home/elathan/src/bindings foo.c: In function `main':
foo.c:6: warning: implicit declaration of function `wrap_foo_ctor'
foo.c:8: warning: implicit declaration of function `wrap_foo_set_food'
foo.c:9: warning: implicit declaration of function `wrap_foo_hello' elathan@velka:~/src/bindings> ./foo
10

My main question is how to silent the implicit declaration warning in gcc, which is, of course, correct. I want everything to compile with -Wall. Is there a warkaround?

I tried to create a C header file, but I don't know how to make a C prototype of:

class Foo * wrap_foo_ctor() { return new Foo(); }

Or silent the 'icompatible pointer type' warning in declarations, such as:

int wrap_foo_hello(Foo *f) { return f->hello(); }

TIA,

Signature · 2 lines
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky 
#1 lore raw

Hi

this is because you program doesnt know what the functions are called so you need to add protypes.

Either create a header and put a prototype of the function name in there and include the header file

or

add the prototypes to the top of the C file.

James

Quoted 57 lines · Elias Athanasopoulos

Hello!

I want to create Ruby bindings for a C++ project, so my first step is to call C++ code from C, since Ruby has a plain C API.

Consider I have a Foo class which its implementation is compiled in a shared lib (libtest.so). I have a second wrapper lib:

#include "libtest.h"

extern "C" class Foo * wrap_foo_ctor() { return new Foo(); } extern "C" void wrap_foo_set_food(Foo *f, int i) { f->set_food(i); } extern "C" int wrap_foo_hello(Foo *f) { return f->hello(); }

Using the above my C program is:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct Foo *m = (struct Foo*) wrap_foo_ctor();

  wrap_foo_set_food(m, 10);
  wrap_foo_hello(m);

  free(m);

return 1;
}

And:

elathan@velka:~/src/bindings> gcc foo.c -Wall -ltest -lwrap -o foo -L/home/elathan/src/bindings foo.c: In function `main':
foo.c:6: warning: implicit declaration of function `wrap_foo_ctor'
foo.c:8: warning: implicit declaration of function `wrap_foo_set_food'
foo.c:9: warning: implicit declaration of function `wrap_foo_hello' elathan@velka:~/src/bindings> ./foo
10

My main question is how to silent the implicit declaration warning in gcc, which is, of course, correct. I want everything to compile with -Wall. Is there a warkaround?

I tried to create a C header file, but I don't know how to make a C prototype of:

class Foo * wrap_foo_ctor() { return new Foo(); }

Or silent the 'icompatible pointer type' warning in declarations, such as:

int wrap_foo_hello(Foo *f) { return f->hello(); }

TIA,

#2 lore raw
In reply to Elias Athanasopoulos

Have you considered using SWIG to automatically generate your wrappers?

... SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is primarily used with common scripting languages such as Perl, Python, Tcl/Tk, and Ruby...

http://www.swig.org/

May be a useful resource. I've used it with C and Python.

regards

Matt

----- Original Message -----
From: Elias Athanasopoulos <[email protected]>
To: <[email protected]>
Sent: Saturday, November 08, 2003 9:30 AM
Subject: mixing C/C++

Quoted 28 lines

Hello!

I want to create Ruby bindings for a C++ project, so my first step is to call C++ code from C, since Ruby has a plain C API.

Consider I have a Foo class which its implementation is compiled in a shared lib (libtest.so). I have a second wrapper lib:

#include "libtest.h"

extern "C" class Foo * wrap_foo_ctor() { return new Foo(); } extern "C" void wrap_foo_set_food(Foo *f, int i) { f->set_food(i); } extern "C" int wrap_foo_hello(Foo *f) { return f->hello(); }

Using the above my C program is:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct Foo *m = (struct Foo*) wrap_foo_ctor();

  wrap_foo_set_food(m, 10);
  wrap_foo_hello(m);

  free(m);
Quoted 6 lines

return 1;
}

And:

elathan@velka:~/src/bindings> gcc foo.c -Wall -ltest -lwrap -o

foo -L/home/elathan/src/bindings

Quoted 10 lines

foo.c: In function `main':
foo.c:6: warning: implicit declaration of function wrap_foo_ctor' foo.c:8: warning: implicit declaration of function wrap_foo_set_food' foo.c:9: warning: implicit declaration of function `wrap_foo_hello' elathan@velka:~/src/bindings> ./foo
10

My main question is how to silent the implicit declaration warning in gcc, which is, of course, correct. I want everything to compile

with -Wall.

Quoted 3 lines

Is there a warkaround?

I tried to create a C header file, but I don't know how to make a C

prototype

Quoted 8 lines

of:

class Foo * wrap_foo_ctor() { return new Foo(); }

Or silent the 'icompatible pointer type' warning in declarations, such as:

int wrap_foo_hello(Foo *f) { return f->hello(); }

Quoted 8 lines

TIA,
--
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky

-
To unsubscribe from this list: send the line "unsubscribe

linux-c-programming" in

Quoted 5 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

This incoming email to UWE has been independently scanned for viruses and

any virus detected has been removed using McAfee anti-virus software

Quoted 3 lines
#3 lore raw
Quoted 6 lines · Matthew Studley

Have you considered using SWIG to automatically generate your wrappers?

... SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is primarily used with common scripting languages such as Perl, Python, Tcl/Tk, and Ruby...

Yes, I have tried it. I found it quite complicated in the sense that I can't control stuff; it's difficult to hack the auto-generated swig files. Also, it lacks compatibility with Ruby 1.8.

Thanks for your reply.

Regards,

Signature · 2 lines
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky 
#4 lore raw

convert INT to CHAR but print's BEEP...

Wednesday, November 12 05:19:21

Hello, I have kind of a problem with converting an int to a char. The get_rand_str() function returns an string build of random charaters with a max length of int `MAX'.

This seems to work at first glance:
~: ./program
qqxfrd

However if I examen the output closer:
~: ./program | od -a
0000000 q q soh x stx f etx r eot d enq nl
0000014

Now I know why my computer keeps beeping everytime it outputs a string :-)

What is the correct way of changing the int value to a char value so that I can append it to the return string ?

char *get_rand_str(int max) {
int i = 0;
char value;
char *retval = NULL;

for(; i < max; i++) {
value = get_ascii_code(97, 122);
retval = (char *)realloc(retval, sizeof(char));
strcat(retval, &value);
}

return retval;
free(retval);
}

int get_ascii_code(int low, int high) {
int k = 0;
double d = 0;

d = (double)rand() / ((double)RAND_MAX + 1);
k = (int)(d * (high - low + 1));
return(low + k);
}

Thank you....

J.

--
KonkyFong

#5 lore raw

Re: convert INT to CHAR but print's BEEP...

Hi !

The problem with strcat is that you cannot just use a pointer to the character because all C string functions require that you end the string with a zero (0) byte.

You could do something like this for eample:
char temp[ 2];
temp[ 0] = my_character;
temp[ 1] = '\0';
strcat( org_string, temp);

Mikael

----- Original Message -----
From: "J." <[email protected]>
To: <[email protected]>
Sent: Wednesday, November 12, 2003 5:35 AM
Subject: convert INT to CHAR but print's BEEP...

Quoted 47 lines

Wednesday, November 12 05:19:21

Hello, I have kind of a problem with converting an int to a char. The get_rand_str() function returns an string build of random charaters with a max length of int `MAX'.

This seems to work at first glance:
~: ./program
qqxfrd

However if I examen the output closer:
~: ./program | od -a
0000000 q q soh x stx f etx r eot d enq nl
0000014

Now I know why my computer keeps beeping everytime it outputs a string :-)

What is the correct way of changing the int value to a char value so that I can append it to the return string ?

char *get_rand_str(int max) {
int i = 0;
char value;
char *retval = NULL;

for(; i < max; i++) {
value = get_ascii_code(97, 122);
retval = (char *)realloc(retval, sizeof(char));
strcat(retval, &value);
}

return retval;
free(retval);
}

int get_ascii_code(int low, int high) {
int k = 0;
double d = 0;

d = (double)rand() / ((double)RAND_MAX + 1);
k = (int)(d * (high - low + 1));
return(low + k);
}

Thank you....

Quoted 7 lines

J.

--
KonkyFong

-
To unsubscribe from this list: send the line "unsubscribe

linux-c-programming" in

Quoted 2 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Re: convert INT to CHAR - SOLVED....

Quoted 6 lines · Mikael Aronsson

Hi !

The problem with strcat is that you cannot just use a pointer to the character because all C string functions require that you end the string with a zero (0) byte.

Quoted 7 lines

You could do something like this for eample:
char temp[ 2];
temp[ 0] = my_character;
temp[ 1] = '\0';
strcat( org_string, temp);

Mikael

THnkx.. I fixed that, and I allocated the return value with calloc, this works ... :)

I learnend something again....

Thnkx

J.

--
FOnkTong

#7 lore raw

Re: convert INT to CHAR but print's BEEP...

In reply to J.
Quoted 1 line · J.

Hello, I have kind of a problem with converting an int to a char. The get_rand_str() function returns an string build of random charaters with a max length of int `MAX'.

I'm sure there are faster algorithms, but how about something simple like:

#include <stdlib.h>

typedef unsigned char U8;
typedef unsigned short U16;

/* Set <buf_len> bytes at <buf> to random values from <low> to <high>. */

void set_rand_buf(char *buf, size_t buf_len, U8 low, U8 high) {
    U8 range = high - low + 1;
    while (buf_len--)
        *buf++ = low + (U8)((range*rand())/(RAND_MAX+1.0));
}

/* Sample calls */

#include <assert.h>

int main(void) {
    char        buf[80];
    size_t      k;

EXAMPLE_1:

    /* Completely fill buf with random data; may include embedded nulls */
    set_rand_buf(buf, sizeof buf, 0, 255);
    for (k = 0; k < sizeof buf; k++)
        printf("byte[%3d]=%3hu\n", k, (U16)buf[k]);

EXAMPLE_2:

    set_rand_buf(buf, 50, '0', '9');    /* Get 50 random ASCII digits. */
    buf[50] = 0;        /* Null-terminate the string. */
    printf("50 digits: %s\n", buf);

EXAMPLE_3:

    set_rand_buf(buf, 26, 'a', 'z');    /* Get 26 random lower-case 

letters. */
buf[26] = 0; /* Null-terminate the string. */
printf("26 letters: %s\n", buf);

EXAMPLE_4:
#DEFINE LEN 27

    /* Get LEN random hex digits in ASCII */
    assert(LEN < sizeof buf);
    set_rand_buf(buf, LEN, 0, 15);      /* First get LEN hex values */
    {   /* Convert hex values from binary to ASCII */
        char *ptr = buf + LEN;
        while (ptr-- > buf)
            *ptr = (*ptr >= 10) ? ('a' - 10 + *ptr) : ('0' + *ptr);
    }
    buf[LEN] = 0;       /* Null-terminate as a string. */
    printf("hex digits %s\n", buf);


    return 0;   /* No-error program exit */
}

P.S. The above code has never been compiled, so there may be all kinds of typos or other errors present.

--
Jeff Woods <[email protected]>