How to use internal c++ class types from C? [duplicate]How to call C++ function from C?Why can't templates be within extern “C” blocks?Using “extern C” on non function call related declarationsHow do you set, clear, and toggle a single bit?What are the differences between a pointer variable and a reference variable in C++?Storing C++ template function definitions in a .CPP fileWhat are POD types in C++?The Definitive C++ Book Guide and ListHow to determine a Python variable's type?What is the effect of extern “C” in C++?What is the “-->” operator in C++?Combining C++ and C - how does #ifdef __cplusplus work?Why is reading lines from stdin much slower in C++ than Python?
Why should we care about syntactic proofs if we can show semantically that statements are true?
In Pokémon Go, why does one of my Pikachu have an option to evolve, but another one doesn't?
Can I legally make a real mobile app based on a fictional app from a TV show?
Why aren’t emergency services using callsigns?
Why did Gandalf use a sword against the Balrog?
Team goes to lunch frequently, I do intermittent fasting but still want to socialize
Accidentals - some in brackets, some not
How to use grep to search through the --help output?
Was this a rapid SCHEDULED disassembly? How was it done?
During the Space Shuttle Columbia Disaster of 2003, Why Did The Flight Director Say, "Lock the doors."?
Is it okay for a ticket seller to grab a tip in the USA?
How can I iterate this process?
How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?
Could one become a successful researcher by writing some really good papers while being outside academia?
Three legged NOT gate? What is this symbol?
Why do oscilloscopes use SMPS instead of linear power supply?
First amendment and employment: Can an police department terminate an officer for speech?
Optimal way to extract "positive part" of a multivariate polynomial
Generator for parity?
Acceptable to cut steak before searing?
Author changing name
Table content alignment to centre using tabular
Y2K... in 2019?
Are there any differences in causality between linear and logistic regression?
How to use internal c++ class types from C? [duplicate]
How to call C++ function from C?Why can't templates be within extern “C” blocks?Using “extern C” on non function call related declarationsHow do you set, clear, and toggle a single bit?What are the differences between a pointer variable and a reference variable in C++?Storing C++ template function definitions in a .CPP fileWhat are POD types in C++?The Definitive C++ Book Guide and ListHow to determine a Python variable's type?What is the effect of extern “C” in C++?What is the “-->” operator in C++?Combining C++ and C - how does #ifdef __cplusplus work?Why is reading lines from stdin much slower in C++ than Python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
How to call C++ function from C?
6 answers
Why can't templates be within extern “C” blocks?
5 answers
Using “extern C” on non function call related declarations
3 answers
I have a C++ class MyClass
that declare a public enum type MyEnum
, and I want to use that enum in a C file. How can I do that ?
I tried to declare my functions in a C++ file and then put everything as extern "C"
, but sadly I am using some functions defined in big_hugly_include.h
and this header does not like being included as external "C"
(it gives me a template with C linkage
error).
I cannot (don't want to) change this include, and I need it because it defines my_function_from_big_include
. Am I stuck ?
my_class_definition.h
:
class MyClass
public:
// I would like to keep it that way as it is mainly used in C++ files
typedef enum
MY_ENUM_0,
MY_ENUM_1,
MY_ENUM_2
MyEnum;
;
Try 1 : my_c_function_definition.c
:
#include "my_class_definition.h"
// I cannot remove this header
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
// I need to call this function with the enum from the C++ class
// This doesn't work (class name scope does not exist in C)
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
Try 2 : my_c_function_definition.cpp
:
#include "my_class_definition.h"
extern "C"
// Error template with C linkage
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
// end of extern "C"
Edited in response to @artcorpse
Try 3 : my_c_function_definition.cpp
:
#include "my_class_definition.h"
// Error multiple definition of [...]
// Error undefined reference to [...]
#include "big_hugly_include.h"
extern "C"
// foo is called in other C files
void foo()
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
// end of extern "C"
c++ c types calling-convention cross-language
marked as duplicate by Kamil Cuk, Kamiccolo, Umair, Rekshino, cegfault Mar 27 at 15:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 5 more comments
This question already has an answer here:
How to call C++ function from C?
6 answers
Why can't templates be within extern “C” blocks?
5 answers
Using “extern C” on non function call related declarations
3 answers
I have a C++ class MyClass
that declare a public enum type MyEnum
, and I want to use that enum in a C file. How can I do that ?
I tried to declare my functions in a C++ file and then put everything as extern "C"
, but sadly I am using some functions defined in big_hugly_include.h
and this header does not like being included as external "C"
(it gives me a template with C linkage
error).
I cannot (don't want to) change this include, and I need it because it defines my_function_from_big_include
. Am I stuck ?
my_class_definition.h
:
class MyClass
public:
// I would like to keep it that way as it is mainly used in C++ files
typedef enum
MY_ENUM_0,
MY_ENUM_1,
MY_ENUM_2
MyEnum;
;
Try 1 : my_c_function_definition.c
:
#include "my_class_definition.h"
// I cannot remove this header
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
// I need to call this function with the enum from the C++ class
// This doesn't work (class name scope does not exist in C)
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
Try 2 : my_c_function_definition.cpp
:
#include "my_class_definition.h"
extern "C"
// Error template with C linkage
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
// end of extern "C"
Edited in response to @artcorpse
Try 3 : my_c_function_definition.cpp
:
#include "my_class_definition.h"
// Error multiple definition of [...]
// Error undefined reference to [...]
#include "big_hugly_include.h"
extern "C"
// foo is called in other C files
void foo()
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
// end of extern "C"
c++ c types calling-convention cross-language
marked as duplicate by Kamil Cuk, Kamiccolo, Umair, Rekshino, cegfault Mar 27 at 15:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
9
If your enum is in a class, you can't access it from C.
– Matthieu Brucher
Mar 27 at 8:02
1
don't think there is much you can do other than move/copy your enum into the global namespace
– Alan Birtles
Mar 27 at 8:02
1
Why thattypedef enum ...
in C++ ?
– bruno
Mar 27 at 8:07
your .h is not your real .h, since at leastclass
orstruct
and;
are missing. Are there other things that you've left out ?
– Christophe
Mar 27 at 8:17
1
The duplicate provided to close this answer is IMHO not an appropriate one, since it doesn’t address the issue with types.
– Christophe
Mar 27 at 16:44
|
show 5 more comments
This question already has an answer here:
How to call C++ function from C?
6 answers
Why can't templates be within extern “C” blocks?
5 answers
Using “extern C” on non function call related declarations
3 answers
I have a C++ class MyClass
that declare a public enum type MyEnum
, and I want to use that enum in a C file. How can I do that ?
I tried to declare my functions in a C++ file and then put everything as extern "C"
, but sadly I am using some functions defined in big_hugly_include.h
and this header does not like being included as external "C"
(it gives me a template with C linkage
error).
I cannot (don't want to) change this include, and I need it because it defines my_function_from_big_include
. Am I stuck ?
my_class_definition.h
:
class MyClass
public:
// I would like to keep it that way as it is mainly used in C++ files
typedef enum
MY_ENUM_0,
MY_ENUM_1,
MY_ENUM_2
MyEnum;
;
Try 1 : my_c_function_definition.c
:
#include "my_class_definition.h"
// I cannot remove this header
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
// I need to call this function with the enum from the C++ class
// This doesn't work (class name scope does not exist in C)
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
Try 2 : my_c_function_definition.cpp
:
#include "my_class_definition.h"
extern "C"
// Error template with C linkage
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
// end of extern "C"
Edited in response to @artcorpse
Try 3 : my_c_function_definition.cpp
:
#include "my_class_definition.h"
// Error multiple definition of [...]
// Error undefined reference to [...]
#include "big_hugly_include.h"
extern "C"
// foo is called in other C files
void foo()
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
// end of extern "C"
c++ c types calling-convention cross-language
This question already has an answer here:
How to call C++ function from C?
6 answers
Why can't templates be within extern “C” blocks?
5 answers
Using “extern C” on non function call related declarations
3 answers
I have a C++ class MyClass
that declare a public enum type MyEnum
, and I want to use that enum in a C file. How can I do that ?
I tried to declare my functions in a C++ file and then put everything as extern "C"
, but sadly I am using some functions defined in big_hugly_include.h
and this header does not like being included as external "C"
(it gives me a template with C linkage
error).
I cannot (don't want to) change this include, and I need it because it defines my_function_from_big_include
. Am I stuck ?
my_class_definition.h
:
class MyClass
public:
// I would like to keep it that way as it is mainly used in C++ files
typedef enum
MY_ENUM_0,
MY_ENUM_1,
MY_ENUM_2
MyEnum;
;
Try 1 : my_c_function_definition.c
:
#include "my_class_definition.h"
// I cannot remove this header
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
// I need to call this function with the enum from the C++ class
// This doesn't work (class name scope does not exist in C)
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
Try 2 : my_c_function_definition.cpp
:
#include "my_class_definition.h"
extern "C"
// Error template with C linkage
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
// end of extern "C"
Edited in response to @artcorpse
Try 3 : my_c_function_definition.cpp
:
#include "my_class_definition.h"
// Error multiple definition of [...]
// Error undefined reference to [...]
#include "big_hugly_include.h"
extern "C"
// foo is called in other C files
void foo()
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
// end of extern "C"
This question already has an answer here:
How to call C++ function from C?
6 answers
Why can't templates be within extern “C” blocks?
5 answers
Using “extern C” on non function call related declarations
3 answers
c++ c types calling-convention cross-language
c++ c types calling-convention cross-language
edited Mar 27 at 18:59
Christophe
43.6k4 gold badges41 silver badges87 bronze badges
43.6k4 gold badges41 silver badges87 bronze badges
asked Mar 27 at 7:53
4nti7rust4nti7rust
568 bronze badges
568 bronze badges
marked as duplicate by Kamil Cuk, Kamiccolo, Umair, Rekshino, cegfault Mar 27 at 15:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Kamil Cuk, Kamiccolo, Umair, Rekshino, cegfault Mar 27 at 15:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Kamil Cuk, Kamiccolo, Umair, Rekshino, cegfault Mar 27 at 15:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
9
If your enum is in a class, you can't access it from C.
– Matthieu Brucher
Mar 27 at 8:02
1
don't think there is much you can do other than move/copy your enum into the global namespace
– Alan Birtles
Mar 27 at 8:02
1
Why thattypedef enum ...
in C++ ?
– bruno
Mar 27 at 8:07
your .h is not your real .h, since at leastclass
orstruct
and;
are missing. Are there other things that you've left out ?
– Christophe
Mar 27 at 8:17
1
The duplicate provided to close this answer is IMHO not an appropriate one, since it doesn’t address the issue with types.
– Christophe
Mar 27 at 16:44
|
show 5 more comments
9
If your enum is in a class, you can't access it from C.
– Matthieu Brucher
Mar 27 at 8:02
1
don't think there is much you can do other than move/copy your enum into the global namespace
– Alan Birtles
Mar 27 at 8:02
1
Why thattypedef enum ...
in C++ ?
– bruno
Mar 27 at 8:07
your .h is not your real .h, since at leastclass
orstruct
and;
are missing. Are there other things that you've left out ?
– Christophe
Mar 27 at 8:17
1
The duplicate provided to close this answer is IMHO not an appropriate one, since it doesn’t address the issue with types.
– Christophe
Mar 27 at 16:44
9
9
If your enum is in a class, you can't access it from C.
– Matthieu Brucher
Mar 27 at 8:02
If your enum is in a class, you can't access it from C.
– Matthieu Brucher
Mar 27 at 8:02
1
1
don't think there is much you can do other than move/copy your enum into the global namespace
– Alan Birtles
Mar 27 at 8:02
don't think there is much you can do other than move/copy your enum into the global namespace
– Alan Birtles
Mar 27 at 8:02
1
1
Why that
typedef enum ...
in C++ ?– bruno
Mar 27 at 8:07
Why that
typedef enum ...
in C++ ?– bruno
Mar 27 at 8:07
your .h is not your real .h, since at least
class
or struct
and ;
are missing. Are there other things that you've left out ?– Christophe
Mar 27 at 8:17
your .h is not your real .h, since at least
class
or struct
and ;
are missing. Are there other things that you've left out ?– Christophe
Mar 27 at 8:17
1
1
The duplicate provided to close this answer is IMHO not an appropriate one, since it doesn’t address the issue with types.
– Christophe
Mar 27 at 16:44
The duplicate provided to close this answer is IMHO not an appropriate one, since it doesn’t address the issue with types.
– Christophe
Mar 27 at 16:44
|
show 5 more comments
2 Answers
2
active
oldest
votes
I want to use that enum in a C file. How can I do that?
Enum concept in C++ originates from C, so only thing you have to do is to isolate definition this enum from pure cpp API which is not known to C (remember about name mangling, see below).
Since C do not know in class/struct enums you can't use them. You have to define global scope enumeration or create such enumeration which will map C++ specific enumeration.
So create separate header file where shared API should be located. Do something like this:
// shared C, C++ header
#ifdef __cplusplus
extern "C"
#endif
enum YourMagicEnum
YourMagicEnumAValue,
YourMagicEnumBValue,
YourMagicEnumCValue,
;
void someFunction(YourMagicEnum x);
#ifdef __cplusplus
// extern "C"
#endif
Now this extern "C"
is needed only for functions to disable name mangling (in C++ you can do function overloads, so compiler generates names which contain arguments type information).
When defining such function it should also have the extern "C"
in front of that definition.
And remember in that header only C specific features and functions can be placed.
Also remember that VLA (Variable-length array) is in C standard, but not in C++ standard (most compiler support VLA for C++).
For more information see this page.
I will probably end up doing that, but I have only one C file using this class, everything else is C++ and I wanted to keep everything inside class namespace to avoid all my enum definition to be global (I really have a lot of them and it will quickly be very messy).
– 4nti7rust
Mar 27 at 13:37
There are no namespaces in C or neither classes, so you can't use them if you need access API form C.
– Marek R
Mar 27 at 13:40
I understand that, but is there a way to define a C type that is a copy of the one inside my class ?
– 4nti7rust
Mar 27 at 13:44
only some script which will parse original file and generate respective C header.
– Marek R
Mar 27 at 13:54
Ugly. Ok, I think I will just do it manually, hopefully when the code will evolve to more C++ we will get rid of it. I will wait a few more hours to see if someone else comes with another workaround, else I think your answer is the one that makes more sense. If you could rewrite your answer with the same namings as my example, and show how to include this type declaration back to the class, that would make it perfect !
– 4nti7rust
Mar 27 at 14:02
|
show 1 more comment
Your Try2 is pretty close to a solution. Try to move the include outside extern "C".
I usually just mark each function individually:
extern "C" void foo()
...
This has the benefit of just exporting the one symbol as a C symbol, instead of trying to convert everything.
That is a good proposal, but sadly I have linking errors when doing that (Undefined reference
andmultiple definition
). Do you know what could create that (big_hugly_include.h
is really full of ****) ? I have added try 3 in my question.
– 4nti7rust
Mar 27 at 13:33
Unfortunately I don't think I can give too much insight without learning about this big_hugly_include.h. It sounds like a C++ header file, so it's most probably important for it to be outside the extern "C" block. The two types of errors you're mentioning sound like linking errors, so some of your other cpp files must be mismatched. Are you getting any errors related to "_foo"?
– artcorpse
Mar 27 at 15:44
No error related to _foo, but I suspect this include to have mixed c and cpp header files.
– 4nti7rust
Mar 27 at 15:46
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I want to use that enum in a C file. How can I do that?
Enum concept in C++ originates from C, so only thing you have to do is to isolate definition this enum from pure cpp API which is not known to C (remember about name mangling, see below).
Since C do not know in class/struct enums you can't use them. You have to define global scope enumeration or create such enumeration which will map C++ specific enumeration.
So create separate header file where shared API should be located. Do something like this:
// shared C, C++ header
#ifdef __cplusplus
extern "C"
#endif
enum YourMagicEnum
YourMagicEnumAValue,
YourMagicEnumBValue,
YourMagicEnumCValue,
;
void someFunction(YourMagicEnum x);
#ifdef __cplusplus
// extern "C"
#endif
Now this extern "C"
is needed only for functions to disable name mangling (in C++ you can do function overloads, so compiler generates names which contain arguments type information).
When defining such function it should also have the extern "C"
in front of that definition.
And remember in that header only C specific features and functions can be placed.
Also remember that VLA (Variable-length array) is in C standard, but not in C++ standard (most compiler support VLA for C++).
For more information see this page.
I will probably end up doing that, but I have only one C file using this class, everything else is C++ and I wanted to keep everything inside class namespace to avoid all my enum definition to be global (I really have a lot of them and it will quickly be very messy).
– 4nti7rust
Mar 27 at 13:37
There are no namespaces in C or neither classes, so you can't use them if you need access API form C.
– Marek R
Mar 27 at 13:40
I understand that, but is there a way to define a C type that is a copy of the one inside my class ?
– 4nti7rust
Mar 27 at 13:44
only some script which will parse original file and generate respective C header.
– Marek R
Mar 27 at 13:54
Ugly. Ok, I think I will just do it manually, hopefully when the code will evolve to more C++ we will get rid of it. I will wait a few more hours to see if someone else comes with another workaround, else I think your answer is the one that makes more sense. If you could rewrite your answer with the same namings as my example, and show how to include this type declaration back to the class, that would make it perfect !
– 4nti7rust
Mar 27 at 14:02
|
show 1 more comment
I want to use that enum in a C file. How can I do that?
Enum concept in C++ originates from C, so only thing you have to do is to isolate definition this enum from pure cpp API which is not known to C (remember about name mangling, see below).
Since C do not know in class/struct enums you can't use them. You have to define global scope enumeration or create such enumeration which will map C++ specific enumeration.
So create separate header file where shared API should be located. Do something like this:
// shared C, C++ header
#ifdef __cplusplus
extern "C"
#endif
enum YourMagicEnum
YourMagicEnumAValue,
YourMagicEnumBValue,
YourMagicEnumCValue,
;
void someFunction(YourMagicEnum x);
#ifdef __cplusplus
// extern "C"
#endif
Now this extern "C"
is needed only for functions to disable name mangling (in C++ you can do function overloads, so compiler generates names which contain arguments type information).
When defining such function it should also have the extern "C"
in front of that definition.
And remember in that header only C specific features and functions can be placed.
Also remember that VLA (Variable-length array) is in C standard, but not in C++ standard (most compiler support VLA for C++).
For more information see this page.
I will probably end up doing that, but I have only one C file using this class, everything else is C++ and I wanted to keep everything inside class namespace to avoid all my enum definition to be global (I really have a lot of them and it will quickly be very messy).
– 4nti7rust
Mar 27 at 13:37
There are no namespaces in C or neither classes, so you can't use them if you need access API form C.
– Marek R
Mar 27 at 13:40
I understand that, but is there a way to define a C type that is a copy of the one inside my class ?
– 4nti7rust
Mar 27 at 13:44
only some script which will parse original file and generate respective C header.
– Marek R
Mar 27 at 13:54
Ugly. Ok, I think I will just do it manually, hopefully when the code will evolve to more C++ we will get rid of it. I will wait a few more hours to see if someone else comes with another workaround, else I think your answer is the one that makes more sense. If you could rewrite your answer with the same namings as my example, and show how to include this type declaration back to the class, that would make it perfect !
– 4nti7rust
Mar 27 at 14:02
|
show 1 more comment
I want to use that enum in a C file. How can I do that?
Enum concept in C++ originates from C, so only thing you have to do is to isolate definition this enum from pure cpp API which is not known to C (remember about name mangling, see below).
Since C do not know in class/struct enums you can't use them. You have to define global scope enumeration or create such enumeration which will map C++ specific enumeration.
So create separate header file where shared API should be located. Do something like this:
// shared C, C++ header
#ifdef __cplusplus
extern "C"
#endif
enum YourMagicEnum
YourMagicEnumAValue,
YourMagicEnumBValue,
YourMagicEnumCValue,
;
void someFunction(YourMagicEnum x);
#ifdef __cplusplus
// extern "C"
#endif
Now this extern "C"
is needed only for functions to disable name mangling (in C++ you can do function overloads, so compiler generates names which contain arguments type information).
When defining such function it should also have the extern "C"
in front of that definition.
And remember in that header only C specific features and functions can be placed.
Also remember that VLA (Variable-length array) is in C standard, but not in C++ standard (most compiler support VLA for C++).
For more information see this page.
I want to use that enum in a C file. How can I do that?
Enum concept in C++ originates from C, so only thing you have to do is to isolate definition this enum from pure cpp API which is not known to C (remember about name mangling, see below).
Since C do not know in class/struct enums you can't use them. You have to define global scope enumeration or create such enumeration which will map C++ specific enumeration.
So create separate header file where shared API should be located. Do something like this:
// shared C, C++ header
#ifdef __cplusplus
extern "C"
#endif
enum YourMagicEnum
YourMagicEnumAValue,
YourMagicEnumBValue,
YourMagicEnumCValue,
;
void someFunction(YourMagicEnum x);
#ifdef __cplusplus
// extern "C"
#endif
Now this extern "C"
is needed only for functions to disable name mangling (in C++ you can do function overloads, so compiler generates names which contain arguments type information).
When defining such function it should also have the extern "C"
in front of that definition.
And remember in that header only C specific features and functions can be placed.
Also remember that VLA (Variable-length array) is in C standard, but not in C++ standard (most compiler support VLA for C++).
For more information see this page.
edited Mar 27 at 10:06
answered Mar 27 at 9:59
Marek RMarek R
15.1k3 gold badges30 silver badges81 bronze badges
15.1k3 gold badges30 silver badges81 bronze badges
I will probably end up doing that, but I have only one C file using this class, everything else is C++ and I wanted to keep everything inside class namespace to avoid all my enum definition to be global (I really have a lot of them and it will quickly be very messy).
– 4nti7rust
Mar 27 at 13:37
There are no namespaces in C or neither classes, so you can't use them if you need access API form C.
– Marek R
Mar 27 at 13:40
I understand that, but is there a way to define a C type that is a copy of the one inside my class ?
– 4nti7rust
Mar 27 at 13:44
only some script which will parse original file and generate respective C header.
– Marek R
Mar 27 at 13:54
Ugly. Ok, I think I will just do it manually, hopefully when the code will evolve to more C++ we will get rid of it. I will wait a few more hours to see if someone else comes with another workaround, else I think your answer is the one that makes more sense. If you could rewrite your answer with the same namings as my example, and show how to include this type declaration back to the class, that would make it perfect !
– 4nti7rust
Mar 27 at 14:02
|
show 1 more comment
I will probably end up doing that, but I have only one C file using this class, everything else is C++ and I wanted to keep everything inside class namespace to avoid all my enum definition to be global (I really have a lot of them and it will quickly be very messy).
– 4nti7rust
Mar 27 at 13:37
There are no namespaces in C or neither classes, so you can't use them if you need access API form C.
– Marek R
Mar 27 at 13:40
I understand that, but is there a way to define a C type that is a copy of the one inside my class ?
– 4nti7rust
Mar 27 at 13:44
only some script which will parse original file and generate respective C header.
– Marek R
Mar 27 at 13:54
Ugly. Ok, I think I will just do it manually, hopefully when the code will evolve to more C++ we will get rid of it. I will wait a few more hours to see if someone else comes with another workaround, else I think your answer is the one that makes more sense. If you could rewrite your answer with the same namings as my example, and show how to include this type declaration back to the class, that would make it perfect !
– 4nti7rust
Mar 27 at 14:02
I will probably end up doing that, but I have only one C file using this class, everything else is C++ and I wanted to keep everything inside class namespace to avoid all my enum definition to be global (I really have a lot of them and it will quickly be very messy).
– 4nti7rust
Mar 27 at 13:37
I will probably end up doing that, but I have only one C file using this class, everything else is C++ and I wanted to keep everything inside class namespace to avoid all my enum definition to be global (I really have a lot of them and it will quickly be very messy).
– 4nti7rust
Mar 27 at 13:37
There are no namespaces in C or neither classes, so you can't use them if you need access API form C.
– Marek R
Mar 27 at 13:40
There are no namespaces in C or neither classes, so you can't use them if you need access API form C.
– Marek R
Mar 27 at 13:40
I understand that, but is there a way to define a C type that is a copy of the one inside my class ?
– 4nti7rust
Mar 27 at 13:44
I understand that, but is there a way to define a C type that is a copy of the one inside my class ?
– 4nti7rust
Mar 27 at 13:44
only some script which will parse original file and generate respective C header.
– Marek R
Mar 27 at 13:54
only some script which will parse original file and generate respective C header.
– Marek R
Mar 27 at 13:54
Ugly. Ok, I think I will just do it manually, hopefully when the code will evolve to more C++ we will get rid of it. I will wait a few more hours to see if someone else comes with another workaround, else I think your answer is the one that makes more sense. If you could rewrite your answer with the same namings as my example, and show how to include this type declaration back to the class, that would make it perfect !
– 4nti7rust
Mar 27 at 14:02
Ugly. Ok, I think I will just do it manually, hopefully when the code will evolve to more C++ we will get rid of it. I will wait a few more hours to see if someone else comes with another workaround, else I think your answer is the one that makes more sense. If you could rewrite your answer with the same namings as my example, and show how to include this type declaration back to the class, that would make it perfect !
– 4nti7rust
Mar 27 at 14:02
|
show 1 more comment
Your Try2 is pretty close to a solution. Try to move the include outside extern "C".
I usually just mark each function individually:
extern "C" void foo()
...
This has the benefit of just exporting the one symbol as a C symbol, instead of trying to convert everything.
That is a good proposal, but sadly I have linking errors when doing that (Undefined reference
andmultiple definition
). Do you know what could create that (big_hugly_include.h
is really full of ****) ? I have added try 3 in my question.
– 4nti7rust
Mar 27 at 13:33
Unfortunately I don't think I can give too much insight without learning about this big_hugly_include.h. It sounds like a C++ header file, so it's most probably important for it to be outside the extern "C" block. The two types of errors you're mentioning sound like linking errors, so some of your other cpp files must be mismatched. Are you getting any errors related to "_foo"?
– artcorpse
Mar 27 at 15:44
No error related to _foo, but I suspect this include to have mixed c and cpp header files.
– 4nti7rust
Mar 27 at 15:46
add a comment |
Your Try2 is pretty close to a solution. Try to move the include outside extern "C".
I usually just mark each function individually:
extern "C" void foo()
...
This has the benefit of just exporting the one symbol as a C symbol, instead of trying to convert everything.
That is a good proposal, but sadly I have linking errors when doing that (Undefined reference
andmultiple definition
). Do you know what could create that (big_hugly_include.h
is really full of ****) ? I have added try 3 in my question.
– 4nti7rust
Mar 27 at 13:33
Unfortunately I don't think I can give too much insight without learning about this big_hugly_include.h. It sounds like a C++ header file, so it's most probably important for it to be outside the extern "C" block. The two types of errors you're mentioning sound like linking errors, so some of your other cpp files must be mismatched. Are you getting any errors related to "_foo"?
– artcorpse
Mar 27 at 15:44
No error related to _foo, but I suspect this include to have mixed c and cpp header files.
– 4nti7rust
Mar 27 at 15:46
add a comment |
Your Try2 is pretty close to a solution. Try to move the include outside extern "C".
I usually just mark each function individually:
extern "C" void foo()
...
This has the benefit of just exporting the one symbol as a C symbol, instead of trying to convert everything.
Your Try2 is pretty close to a solution. Try to move the include outside extern "C".
I usually just mark each function individually:
extern "C" void foo()
...
This has the benefit of just exporting the one symbol as a C symbol, instead of trying to convert everything.
edited Mar 27 at 9:50
answered Mar 27 at 9:44
artcorpseartcorpse
1904 silver badges7 bronze badges
1904 silver badges7 bronze badges
That is a good proposal, but sadly I have linking errors when doing that (Undefined reference
andmultiple definition
). Do you know what could create that (big_hugly_include.h
is really full of ****) ? I have added try 3 in my question.
– 4nti7rust
Mar 27 at 13:33
Unfortunately I don't think I can give too much insight without learning about this big_hugly_include.h. It sounds like a C++ header file, so it's most probably important for it to be outside the extern "C" block. The two types of errors you're mentioning sound like linking errors, so some of your other cpp files must be mismatched. Are you getting any errors related to "_foo"?
– artcorpse
Mar 27 at 15:44
No error related to _foo, but I suspect this include to have mixed c and cpp header files.
– 4nti7rust
Mar 27 at 15:46
add a comment |
That is a good proposal, but sadly I have linking errors when doing that (Undefined reference
andmultiple definition
). Do you know what could create that (big_hugly_include.h
is really full of ****) ? I have added try 3 in my question.
– 4nti7rust
Mar 27 at 13:33
Unfortunately I don't think I can give too much insight without learning about this big_hugly_include.h. It sounds like a C++ header file, so it's most probably important for it to be outside the extern "C" block. The two types of errors you're mentioning sound like linking errors, so some of your other cpp files must be mismatched. Are you getting any errors related to "_foo"?
– artcorpse
Mar 27 at 15:44
No error related to _foo, but I suspect this include to have mixed c and cpp header files.
– 4nti7rust
Mar 27 at 15:46
That is a good proposal, but sadly I have linking errors when doing that (
Undefined reference
and multiple definition
). Do you know what could create that (big_hugly_include.h
is really full of ****) ? I have added try 3 in my question.– 4nti7rust
Mar 27 at 13:33
That is a good proposal, but sadly I have linking errors when doing that (
Undefined reference
and multiple definition
). Do you know what could create that (big_hugly_include.h
is really full of ****) ? I have added try 3 in my question.– 4nti7rust
Mar 27 at 13:33
Unfortunately I don't think I can give too much insight without learning about this big_hugly_include.h. It sounds like a C++ header file, so it's most probably important for it to be outside the extern "C" block. The two types of errors you're mentioning sound like linking errors, so some of your other cpp files must be mismatched. Are you getting any errors related to "_foo"?
– artcorpse
Mar 27 at 15:44
Unfortunately I don't think I can give too much insight without learning about this big_hugly_include.h. It sounds like a C++ header file, so it's most probably important for it to be outside the extern "C" block. The two types of errors you're mentioning sound like linking errors, so some of your other cpp files must be mismatched. Are you getting any errors related to "_foo"?
– artcorpse
Mar 27 at 15:44
No error related to _foo, but I suspect this include to have mixed c and cpp header files.
– 4nti7rust
Mar 27 at 15:46
No error related to _foo, but I suspect this include to have mixed c and cpp header files.
– 4nti7rust
Mar 27 at 15:46
add a comment |
9
If your enum is in a class, you can't access it from C.
– Matthieu Brucher
Mar 27 at 8:02
1
don't think there is much you can do other than move/copy your enum into the global namespace
– Alan Birtles
Mar 27 at 8:02
1
Why that
typedef enum ...
in C++ ?– bruno
Mar 27 at 8:07
your .h is not your real .h, since at least
class
orstruct
and;
are missing. Are there other things that you've left out ?– Christophe
Mar 27 at 8:17
1
The duplicate provided to close this answer is IMHO not an appropriate one, since it doesn’t address the issue with types.
– Christophe
Mar 27 at 16:44