Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ namespace in header issue

Tags:

c++

visual-c++

I am trying to implement a Websocket Server. I am using the libwebsockets library.

ConnectionServer.c file has setup code for the library and main() function (I don't see anything of importance to post here.) This file includes 1 file for the received data callback called: dmserver_callback.cpp. This file then includes another file (my custom parser file) called: data_parser.cpp. This file then includes a file called definitions.h (the source of the problem).

Just bear with me; I understand that including files (daisy chaining; so to speak) probably isn't the best way to do this, and I more than likely should be using header files and such. One question I have is that is this particularly necessary?

To clarify, everything is working as intended until I try to add my own parsing mechanism.

The definitions.h file is as follows:

namespace EngineDefinitions {
enum Version {
    Major = 1,
    Minor = 2
}; //Version;

namespace Server {
    enum enum_Server {
        MAX_PLAYERS = 200,
        MAX_TABLES = 42, // 3 tables per row.
        MAX_TABLE_PLAYERS = 10,
        GAME_PORT = 2040, //2042
        MAX_PARAMS = 10
    }; //Server;
};

namespace Login {
    enum enum_Login {
        USERNAME = 1,
        PASSWORD = 2
    }; //Login;
};
};

My error is:

definitions.h(1): error C2061: syntax error : identifier 'EngineDefinitions'

I loaded the exact same header in a new Win32 Console Project in Visual C++ 2010 Express and there it works. The only difference I see is the main file (where int main function resides). In the project that the header file works is called: ConectionServer.cpp (C++)

and the main project file that doesn't work is named: ConnectionServer.c (C)

Does this have something to do with the file being compiled in C vs C++?

I think that the libwebsocket library is coded in C. I can't recall if I created the project files in exactly the same manner or not.

P.S. Please let me know if there is any other information I can provide to help.

EDIT: I also realize you're not supposed to define types inside a header file (eg: enums). I DID try to separate the source into .cpp and a header file using the extern enum with no difference. In fact, got more errors (redefinitions) than I bargained for when trying to use them.

like image 827
Levi Roberts Avatar asked Feb 19 '26 18:02

Levi Roberts


2 Answers

You need to understand that C++ and C are different languages. When you include the header file in a .cpp file, the compiler will try to compile the .h as C++ code. C++ supports namespaces, so everyone is happy. But when you try to include the .h from the .c file (which is what is actually happening if you follow the #includes), the compiler attempts to compile the .h as C code, yet fails because namespaces do not exist in C.

A common way to solve this problem is to use predefined macros. __cplusplus is defined when compiling as C++ code, and not defined when compiling as C code (obviously).

You could do this:

#ifdef __cplusplus
namespace EngineDefinitions {
#endif
enum Version {
    Major = 1,
    Minor = 2
}; //Version;

#ifdef __cplusplus
namespace Server {
#endif
    enum enum_Server {
        MAX_PLAYERS = 200,
        MAX_TABLES = 42, // 3 tables per row.
        MAX_TABLE_PLAYERS = 10,
        GAME_PORT = 2040, //2042
        MAX_PARAMS = 10
    }; //Server;
#ifdef __cplusplus
};
#endif

#ifdef __cplusplus
namespace Login {
#endif
    enum enum_Login {
        USERNAME = 1,
        PASSWORD = 2
    }; //Login;
#ifdef __cplusplus
};
#endif

And of course, you lose the ability of namespaces in C anyways.

like image 82
Marlon Avatar answered Feb 22 '26 06:02

Marlon


C does not have namespaces. For Real.

like image 24
Joe Avatar answered Feb 22 '26 06:02

Joe