Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ library access from C (http://www.velocityreviews.com/forums/t457296-c-library-access-from-c.html)

TBass 09-29-2006 04:41 PM

C++ library access from C
 
Hi,

This is going to be one of those annoyingly stupid questions.

I would like to compile a library as a C++ program, but it needs to be
able to work with a C program as well. I only want to distribute one
..lib file and an H file.

I know that I can call a C library from a C++ program. Is it possible
to compile a CPP library that can be called from a C program?

For the, "why not just make it a C library and stop wasting our time?"
question, I need to handle some OLE/OCP calls, and I'm trying to avoid
the pain the arse of doing OLE in C.

Thanks in advance,
Tom


Victor Bazarov 09-29-2006 04:50 PM

Re: C++ library access from C
 
TBass wrote:
> This is going to be one of those annoyingly stupid questions.
>
> I would like to compile a library as a C++ program, but it needs to be
> able to work with a C program as well. I only want to distribute one
> .lib file and an H file.
>
> I know that I can call a C library from a C++ program. Is it possible
> to compile a CPP library that can be called from a C program?


Yes, what you usually do is provide a C interface for every C++ function
you expect to call:

int my_Cpp_function(const char*);

extern "C" int my_Cpp_function_C_wrapper(const char* cptr) {
return my_Cpp_function(cptr);
}

Of course, it means that no C++ types can be present in the interface.

> For the, "why not just make it a C library and stop wasting our time?"
> question, I need to handle some OLE/OCP calls, and I'm trying to avoid
> the pain the arse of doing OLE in C.


Agreed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



TBass 09-29-2006 05:01 PM

Re: C++ library access from C
 
[snip]
> Yes, what you usually do is provide a C interface for every C++ function
> you expect to call:
>
> int my_Cpp_function(const char*);
>
> extern "C" int my_Cpp_function_C_wrapper(const char* cptr) {
> return my_Cpp_function(cptr);
> }
>
> Of course, it means that no C++ types can be present in the interface.

[/snip]

There aren't any. Thanks! That's a big help.


Roland Pibinger 09-29-2006 06:17 PM

Re: C++ library access from C
 
On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
<v.Abazarov@comAcast.net> wrote:
>TBass wrote:
>> This is going to be one of those annoyingly stupid questions.
>> I would like to compile a library as a C++ program, but it needs to be
>> able to work with a C program as well. I only want to distribute one
>> .lib file and an H file.

>
>Yes, what you usually do is provide a C interface for every C++ function
>you expect to call:
>
> int my_Cpp_function(const char*);
>
> extern "C" int my_Cpp_function_C_wrapper(const char* cptr) {
> return my_Cpp_function(cptr);
> }
>
>Of course, it means that no C++ types can be present in the interface.


You can use forward declared C++ types - 'class' renamed to 'struct'
which may be helpful in that case.

Best regards,
Roland Pibinger

Greg Comeau 09-29-2006 09:37 PM

Re: C++ library access from C
 
In article <1159548059.941307.147180@e3g2000cwe.googlegroups. com>,
TBass <tbj@automateddesign.com> wrote:
>I would like to compile a library as a C++ program, but it needs to be
>able to work with a C program as well. I only want to distribute one
>.lib file and an H file.
>
>I know that I can call a C library from a C++ program. Is it possible
>to compile a CPP library that can be called from a C program?
>
>For the, "why not just make it a C library and stop wasting our time?"
>question, I need to handle some OLE/OCP calls, and I'm trying to avoid
>the pain the arse of doing OLE in C.


Check out http://www.comeaucomputing.com/techtalk/#externC

If your library is a class library, or template library, etc.
for all practical purposes, don't do that.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

Greg Comeau 09-29-2006 10:28 PM

Re: C++ library access from C
 
In article <451d629c.1181799@news.utanet.at>,
Roland Pibinger <rpbg123@yahoo.com> wrote:
>On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
><v.Abazarov@comAcast.net> wrote:
>>TBass wrote:
>>> This is going to be one of those annoyingly stupid questions.
>>> I would like to compile a library as a C++ program, but it needs to be
>>> able to work with a C program as well. I only want to distribute one
>>> .lib file and an H file.

>>
>>Yes, what you usually do is provide a C interface for every C++ function
>>you expect to call:
>>
>> int my_Cpp_function(const char*);
>>
>> extern "C" int my_Cpp_function_C_wrapper(const char* cptr) {
>> return my_Cpp_function(cptr);
>> }
>>
>>Of course, it means that no C++ types can be present in the interface.

>
>You can use forward declared C++ types - 'class' renamed to 'struct'
>which may be helpful in that case.


Only if it's truly POD classes.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

Frederick Gotham 09-29-2006 10:46 PM

Re: C++ library access from C
 
TBass posted:

> I would like to compile a library as a C++ program, but it needs to be
> able to work with a C program as well. I only want to distribute one
> .lib file and an H file.
>
> I know that I can call a C library from a C++ program. Is it possible
> to compile a CPP library that can be called from a C program?



Here's a sample program:

/* defs.cpp */

extern "C" int Func(double const k) { return k / 3.2; }


/* header.h */

#ifdef __cplusplus
extern "C" int Func(double);
#else
int Func(double);
#endif

/* source1.c */

#include "header.h"

int main(void)
{
Func(53.6);
return 0;
}


/* source2.cpp */

#include "header.h"

int Abc()
{
Func(45.8);
return 0;
}

--

Frederick Gotham

Roland Pibinger 09-30-2006 08:11 AM

Re: C++ library access from C
 
On 29 Sep 2006 18:28:10 -0400, comeau@panix.com (Greg Comeau) wrote:
>In article <451d629c.1181799@news.utanet.at>,
>Roland Pibinger <rpbg123@yahoo.com> wrote:
>>You can use forward declared C++ types - 'class' renamed to 'struct'
>>which may be helpful in that case.

>
>Only if it's truly POD classes.


When you use only pointers it works with non-POD classes too, eg.

// myclass.h

struct MyClass;
typedef struct MyClass MyClass;

extern "C" {
MyClass* createMyClass();
int doSomething (MyClass* m, int i);
void destroyMyClass (MyClass* m);
}


// myclass.cpp

#include "myclass.h"

class MyClass {
public:
virtual int doSomething (int i) { return i; }
virtual ~MyClass() {}
};


MyClass* createMyClass() { return new MyClass; }
int doSomething (MyClass* m, int i) {
return m ? m->doSomething(i) : 0;
}
void destroyMyClass (MyClass* m) { delete m; }


// usage in C program:

int main() {
MyClass* m = createMyClass();
doSomething (m, 7);
destroyMyClass (m);
}

Best regards,
Roland Pibinger

Greg Comeau 09-30-2006 01:22 PM

Re: C++ library access from C
 
In article <451e2505.2621279@news.utanet.at>,
Roland Pibinger <rpbg123@yahoo.com> wrote:
>On 29 Sep 2006 18:28:10 -0400, comeau@panix.com (Greg Comeau) wrote:
>>In article <451d629c.1181799@news.utanet.at>,
>>Roland Pibinger <rpbg123@yahoo.com> wrote:
>>>You can use forward declared C++ types - 'class' renamed to 'struct'
>>>which may be helpful in that case.

>>
>>Only if it's truly POD classes.

>
>When you use only pointers it works with non-POD classes too, eg.
>
>// myclass.h
>
>struct MyClass;
>typedef struct MyClass MyClass;
>
>extern "C" {
> MyClass* createMyClass();
> int doSomething (MyClass* m, int i);
> void destroyMyClass (MyClass* m);
>}
>
>
>// myclass.cpp
>
>#include "myclass.h"
>
>class MyClass {
>public:
> virtual int doSomething (int i) { return i; }
> virtual ~MyClass() {}
>};
>
>
>MyClass* createMyClass() { return new MyClass; }
>int doSomething (MyClass* m, int i) {
> return m ? m->doSomething(i) : 0;
>}
>void destroyMyClass (MyClass* m) { delete m; }
>
>
>// usage in C program:
>
>int main() {
> MyClass* m = createMyClass();
> doSomething (m, 7);
> destroyMyClass (m);
>}


Indeed. I was thinking about him actually accessing
members of types that don't exist in C though.
Nope, not impossible, but like the above, stub city et al,
which can be a real pain to being productive unless
isolated cases.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

Roland Pibinger 09-30-2006 03:08 PM

Re: C++ library access from C
 
On 30 Sep 2006 09:22:43 -0400, comeau@panix.com (Greg Comeau) wrote:
>Nope, not impossible, but like the above, stub city et al,
>which can be a real pain to being productive unless
>isolated cases.


Yes, that's true. A tool may be helpful: http://www.swig.org/

Best regards,
Roland Pibinger


All times are GMT. The time now is 06:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.