* mixing C/C++
@ 2003-11-08 9:30 Elias Athanasopoulos
2003-11-08 13:36 ` James Stevenson
2003-11-10 15:00 ` Matthew Studley
0 siblings, 2 replies; 8+ messages in thread
From: Elias Athanasopoulos @ 2003-11-08 9:30 UTC (permalink / raw)
To: linux-c-programming
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,
--
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: mixing C/C++
2003-11-08 9:30 mixing C/C++ Elias Athanasopoulos
@ 2003-11-08 13:36 ` James Stevenson
2003-11-10 15:00 ` Matthew Studley
1 sibling, 0 replies; 8+ messages in thread
From: James Stevenson @ 2003-11-08 13:36 UTC (permalink / raw)
To: Elias Athanasopoulos; +Cc: linux-c-programming
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
On Sat, 8 Nov 2003, Elias Athanasopoulos wrote:
> 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,
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: mixing C/C++
2003-11-08 9:30 mixing C/C++ Elias Athanasopoulos
2003-11-08 13:36 ` James Stevenson
@ 2003-11-10 15:00 ` Matthew Studley
2003-11-10 17:37 ` Elias Athanasopoulos
1 sibling, 1 reply; 8+ messages in thread
From: Matthew Studley @ 2003-11-10 15:00 UTC (permalink / raw)
To: Elias Athanasopoulos, linux-c-programming
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 <elathan@phys.uoa.gr>
To: <linux-c-programming@vger.kernel.org>
Sent: Saturday, November 08, 2003 9:30 AM
Subject: mixing C/C++
> 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,
> --
> 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
> the body of a message to majordomo@vger.kernel.org
> 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
>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: mixing C/C++
2003-11-10 15:00 ` Matthew Studley
@ 2003-11-10 17:37 ` Elias Athanasopoulos
2003-11-12 4:35 ` convert INT to CHAR but print's BEEP J.
0 siblings, 1 reply; 8+ messages in thread
From: Elias Athanasopoulos @ 2003-11-10 17:37 UTC (permalink / raw)
To: Matthew Studley; +Cc: linux-c-programming
On Mon, Nov 10, 2003 at 03:00:31PM -0000, Matthew Studley wrote:
> 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,
--
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky
^ permalink raw reply [flat|nested] 8+ messages in thread
* convert INT to CHAR but print's BEEP...
2003-11-10 17:37 ` Elias Athanasopoulos
@ 2003-11-12 4:35 ` J.
2003-11-12 7:11 ` Mikael Aronsson
2003-11-13 6:36 ` convert INT to CHAR but print's BEEP Jeff Woods
0 siblings, 2 replies; 8+ messages in thread
From: J. @ 2003-11-12 4:35 UTC (permalink / raw)
To: linux-c-programming
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: convert INT to CHAR but print's BEEP...
2003-11-12 4:35 ` convert INT to CHAR but print's BEEP J.
@ 2003-11-12 7:11 ` Mikael Aronsson
2003-11-13 1:26 ` convert INT to CHAR - SOLVED J.
2003-11-13 6:36 ` convert INT to CHAR but print's BEEP Jeff Woods
1 sibling, 1 reply; 8+ messages in thread
From: Mikael Aronsson @ 2003-11-12 7:11 UTC (permalink / raw)
To: linux-c-programming
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." <mailing-lists@xs4all.nl>
To: <linux-c-programming@vger.kernel.org>
Sent: Wednesday, November 12, 2003 5:35 AM
Subject: 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
>
> -
> To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: convert INT to CHAR - SOLVED....
2003-11-12 7:11 ` Mikael Aronsson
@ 2003-11-13 1:26 ` J.
0 siblings, 0 replies; 8+ messages in thread
From: J. @ 2003-11-13 1:26 UTC (permalink / raw)
To: linux-c-programming
On Wed, 12 Nov 2003, Mikael Aronsson wrote:
> 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
THnkx.. I fixed that, and I allocated the return value with calloc, this
works ... :)
I learnend something again....
Thnkx
J.
--
FOnkTong
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: convert INT to CHAR but print's BEEP...
2003-11-12 4:35 ` convert INT to CHAR but print's BEEP J.
2003-11-12 7:11 ` Mikael Aronsson
@ 2003-11-13 6:36 ` Jeff Woods
1 sibling, 0 replies; 8+ messages in thread
From: Jeff Woods @ 2003-11-13 6:36 UTC (permalink / raw)
To: J.; +Cc: linux-c-programming
At 11/12/2003 05:35 AM +0100, J. wrote:
>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 <kazrak+kernel@cesmail.net>
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2003-11-13 6:36 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-08 9:30 mixing C/C++ Elias Athanasopoulos
2003-11-08 13:36 ` James Stevenson
2003-11-10 15:00 ` Matthew Studley
2003-11-10 17:37 ` Elias Athanasopoulos
2003-11-12 4:35 ` convert INT to CHAR but print's BEEP J.
2003-11-12 7:11 ` Mikael Aronsson
2003-11-13 1:26 ` convert INT to CHAR - SOLVED J.
2003-11-13 6:36 ` convert INT to CHAR but print's BEEP Jeff Woods
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).