Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ library access from C

Reply
Thread Tools

C++ library access from C

 
 
TBass
Guest
Posts: n/a
 
      09-29-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-29-2006
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


 
Reply With Quote
 
 
 
 
TBass
Guest
Posts: n/a
 
      09-29-2006
[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.

 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      09-29-2006
On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
<(E-Mail Removed)> 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
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      09-29-2006
In article <(E-Mail Removed). com>,
TBass <(E-Mail Removed)> 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?
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      09-29-2006
In article <(E-Mail Removed)>,
Roland Pibinger <(E-Mail Removed)> wrote:
>On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
><(E-Mail Removed)> 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?
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-29-2006
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.;
return 0;
}

--

Frederick Gotham
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      09-30-2006
On 29 Sep 2006 18:28:10 -0400, http://www.velocityreviews.com/forums/(E-Mail Removed) (Greg Comeau) wrote:
>In article <(E-Mail Removed)>,
>Roland Pibinger <(E-Mail Removed)> 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
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      09-30-2006
In article <(E-Mail Removed)>,
Roland Pibinger <(E-Mail Removed)> wrote:
>On 29 Sep 2006 18:28:10 -0400, (E-Mail Removed) (Greg Comeau) wrote:
>>In article <(E-Mail Removed)>,
>>Roland Pibinger <(E-Mail Removed)> 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?
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      09-30-2006
On 30 Sep 2006 09:22:43 -0400, (E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Very annoying error: Access to the path is denied. ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity Jay ASP .Net 2 08-20-2007 07:38 PM
403 Forbidden: You were denied access because: Access denied by access control list Southern Kiwi NZ Computing 6 03-19-2006 05:19 AM
How do I let people access the internet via an access point but not allow them access to my network yar Wireless Networking 4 09-21-2004 03:48 AM
Re: Difference between Web Control Library and Class Library Alan Ferrandiz [MCT] ASP .Net 0 09-11-2004 01:51 PM
Re: Difference between Web Control Library and Class Library Mythran ASP .Net 0 08-24-2004 05:53 PM



Advertisments