This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

what's thie meaning for "#ifndef __C51__"

In Keil demos, I find some definitions such as "#ifndef __C51__", what's the meaning for double '_'.

Parents
  • Some points need clarification here, I think:

    *) Even in a single-source-file project, you should make it a habit to always have multiple-inclusion guards on your header files. Never write any .h file without one --- odds are it'll save your day years later, even if it's not actually needed right now.

    *) You say: "I don't know the difference between definition and declaration". That's exactly the reason you're having problems with header files. To declare a variable in a header file, write:

    extern type_name variable_name;

    The extern is the crucial part, here. Without it, the same line is a (tentative) definition, which gets you exactly those multiple definition complaints from the linker you mentioned.

    *) You mentioned "extern imports" in your .c files --- those are symptoms of the exact same trouble. As a rule of thumb, "extern" should never appear inside a .c file. Each time it does, you really should have #include'd a header file instead.

    *) As to Pascal's name resolution policy: that comes nowhere close to what "namespaces" really should be. Look at C++ for a better idea. Following your description, Pascal would only support two namespaces: "here" and "elsewhere", with no way to access an object in "elsewhere" once you've defined one of the same name "here".

Reply
  • Some points need clarification here, I think:

    *) Even in a single-source-file project, you should make it a habit to always have multiple-inclusion guards on your header files. Never write any .h file without one --- odds are it'll save your day years later, even if it's not actually needed right now.

    *) You say: "I don't know the difference between definition and declaration". That's exactly the reason you're having problems with header files. To declare a variable in a header file, write:

    extern type_name variable_name;

    The extern is the crucial part, here. Without it, the same line is a (tentative) definition, which gets you exactly those multiple definition complaints from the linker you mentioned.

    *) You mentioned "extern imports" in your .c files --- those are symptoms of the exact same trouble. As a rule of thumb, "extern" should never appear inside a .c file. Each time it does, you really should have #include'd a header file instead.

    *) As to Pascal's name resolution policy: that comes nowhere close to what "namespaces" really should be. Look at C++ for a better idea. Following your description, Pascal would only support two namespaces: "here" and "elsewhere", with no way to access an object in "elsewhere" once you've defined one of the same name "here".

Children
No data