By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

C++ library access from C

P: n/a
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

Sep 29 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Sep 29 '06 #2

P: n/a
[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.

Sep 29 '06 #3

P: n/a
On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>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
Sep 29 '06 #4

P: n/a
In article <11**********************@e3g2000cwe.googlegroups. com>,
TBass <tb*@automateddesign.comwrote:
>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?
Sep 29 '06 #5

P: n/a
In article <45**************@news.utanet.at>,
Roland Pibinger <rp*****@yahoo.comwrote:
>On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>>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?
Sep 29 '06 #6

P: n/a
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
Sep 29 '06 #7

P: n/a
On 29 Sep 2006 18:28:10 -0400, co****@panix.com (Greg Comeau) wrote:
>In article <45**************@news.utanet.at>,
Roland Pibinger <rp*****@yahoo.comwrote:
>>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
Sep 30 '06 #8

P: n/a
In article <45**************@news.utanet.at>,
Roland Pibinger <rp*****@yahoo.comwrote:
>On 29 Sep 2006 18:28:10 -0400, co****@panix.com (Greg Comeau) wrote:
>>In article <45**************@news.utanet.at>,
Roland Pibinger <rp*****@yahoo.comwrote:
>>>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?
Sep 30 '06 #9

P: n/a
On 30 Sep 2006 09:22:43 -0400, co****@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
Sep 30 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.