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"
}