473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Call C++ from C

Hello,
how can I call this C++ Code from C? I must use C, because Ruby-ffi
understand only C.

Expand|Select|Wrap|Line Numbers
  1. class Shape {
  2. public:
  3. Shape() {
  4. nshapes++;
  5. }
  6. virtual ~Shape() {
  7. nshapes--;
  8. };
  9. double  x, y;
  10. void    move(double dx, double dy);
  11. virtual double area(void) = 0;
  12. virtual double perimeter(void) = 0;
  13. static  int nshapes;
  14. };
  15.  
  16. class Circle : public Shape {
  17. private:
  18. double radius;
  19. public:
  20. Circle(double r) : radius(r) { };
  21. virtual double area(void);
  22. virtual double perimeter(void);
  23. };
  24.  
  25. class Square : public Shape {
  26. private:
  27. double width;
  28. public:
  29. Square(double w) : width(w) { };
  30. virtual double area(void);
  31. virtual double perimeter(void);
  32. };
  33.  
  34.  
Expand|Select|Wrap|Line Numbers
  1. #include "example.h"
  2. #define M_PI 3.14159265358979323846
  3.  
  4. /* Move the shape to a new location */
  5. void Shape::move(double dx, double dy) {
  6. x += dx;
  7. y += dy;
  8. }
  9.  
  10. int Shape::nshapes = 0;
  11.  
  12. double Circle::area(void) {
  13. return M_PI*radius*radius;
  14. }
  15.  
  16. double Circle::perimeter(void) {
  17. return 2*M_PI*radius;
  18. }
  19.  
  20. double Square::area(void) {
  21. return width*width;
  22. }
  23.  
  24. double Square::perimeter(void) {
  25. return 4*width;
  26. }
  27.  
Best regards
Nov 14 '08 #1
6 2170
flashdog wrote:
Hello,
how can I call this C++ Code from C? I must use C, because Ruby-ffi
understand only C.
Provide an extern "C" linkage wrapper.

--
Ian Collins
Nov 14 '08 #2
Exist maybe a tool that creat such wrapper?

Ian Collins wrote:
flashdog wrote:
Hello,
how can I call this C++ Code from C? I must use C, because Ruby-ffi
understand only C.
Provide an extern "C" linkage wrapper.

--
Ian Collins
Nov 14 '08 #3
flashdog wrote:
Exist maybe a tool that creat such wrapper?
please, do not top-post.

The thing is, there is no one-to-one correspondence between C and C++,
and in particular you cannot just automatically expose all your C++ code
as a C interface. I'm afraid the best solution is to think to a
reasonable C interface to your C++ code. That means, you will have a set
of "c-style" functions, with C linkage, that will internally use your
C++ code.

Best wishes,

Zeppe
Nov 14 '08 #4
flashdog wrote:

Please don't top-post.
Ian Collins wrote:
>flashdog wrote:
>>Hello,
how can I call this C++ Code from C? I must use C, because Ruby-ffi
understand only C.
Provide an extern "C" linkage wrapper.
Exist maybe a tool that creat such wrapper?
Your editor? Only you know what and how you want to call from C.

--
Ian Collins
Nov 14 '08 #5
On Nov 14, 4:28 am, flashdog <flash...@gmx.netwrote:
Exist maybe a tool that creat such wrapper?

Check out SWIG: http://www.swig.org/

Sean

Nov 14 '08 #6
flashdog wrote:
Hello,
how can I call this C++ Code from C? I must use C, because Ruby-ffi
understand only C.
<http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html>


Brian
Nov 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
5
by: Amaryllis | last post by:
I'm trying to call a CL which is located on our AS400 from a Windows application. I've tried to code it in different ways, but I seem to get the same error every time. Does anyone have any clue...
13
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
7
by: archana | last post by:
Hi all, I am having application in which i am doing asynchronous call.I am using manualresetevent to wait for asynchronous call to complete. I want to stop asynchronous call after certain...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.