Skip to main content
Rollback to Revision 7
Source Link
Misha M
  • 11.4k
  • 17
  • 58
  • 67

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?

Thanks

:UPDATE:

 

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

    typedef struct X { ... } X

     typedef struct X { ... } X 
  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?

Thanks

:UPDATE:

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

    typedef struct X { ... } X

  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?

 

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

     typedef struct X { ... } X 
  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

Rollback to Revision 6
Source Link
Misha M
  • 11.4k
  • 17
  • 58
  • 67

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?


 

Thanks

:UPDATE:

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

     typedef struct X { ... } X 

    typedef struct X { ... } X

  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?


 

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

     typedef struct X { ... } X 
  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?

Thanks

:UPDATE:

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

    typedef struct X { ... } X

  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

deleted 10 characters in body
Source Link
Niall
  • 30.8k
  • 10
  • 106
  • 155

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?

Thanks

:UPDATE:

 

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

    typedef struct X { ... } X

     typedef struct X { ... } X 
  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?

Thanks

:UPDATE:

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

    typedef struct X { ... } X

  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?

 

Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.
#ifdef __cplusplus extern "C" { #endif // // Code goes here ... // #ifdef __cplusplus } // extern "C" #endif
  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.

  2. Careful of C++ identifiers as names in C code

  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.

  4. For structs follow the following form so that C does not get confused.

     typedef struct X { ... } X 
  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

edited body
Source Link
erip
  • 17.1k
  • 11
  • 73
  • 131
Loading
fixed some grammar and reformatted code for easier reading
Source Link
Misha M
  • 11.4k
  • 17
  • 58
  • 67
Loading
Rollback to Revision 2
Link
Misha M
  • 11.4k
  • 17
  • 58
  • 67
Loading
edited tags; edited title; edited title
Link
Hernán Eche
  • 7k
  • 13
  • 54
  • 81
Loading
update on various gotcha's
Source Link
Misha M
  • 11.4k
  • 17
  • 58
  • 67
Loading
Post Made Community Wiki by CommunityBot
Source Link
Misha M
  • 11.4k
  • 17
  • 58
  • 67
Loading