{"id":586,"date":"2022-08-30T15:03:49","date_gmt":"2022-08-30T15:03:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/c-include-and-mulitple-typedefs-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:03:49","modified_gmt":"2022-08-30T15:03:49","slug":"c-include-and-mulitple-typedefs-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/c-include-and-mulitple-typedefs-collection-of-common-programming-errors\/","title":{"rendered":"c &#8211; #include and mulitple typedefs-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m writing an embedded C program in eclipse with gcc and can&#8217;t see how to get around a certain issue. I have a typedef in a header file which is protected by an include guard. Since I use this typedef in multiple headers, I need to include it in other headers, however when I try to compile, no matter what I do, one of the headers cannot see the typedef and complains of an unknown type name.<\/p>\n<p>I believe this illustrates the problem:<\/p>\n<p>header a.h:<\/p>\n<pre><code>#ifndef _a_h\n#define _a_h\n\ntypedef enum {\n  USBD_OK   = 0,\n  USBD_BUSY,\n  USBD_FAIL,\n}USBD_Status;\n\n#endif\n<\/code><\/pre>\n<p>header b.h:<\/p>\n<pre><code>#ifndef _b_h\n#define _b_h\n\n#include \"a.h\"\n\nextern USBD_Status USB_getStatus(void);\n\n#endif\n<\/code><\/pre>\n<p>header c.h:<\/p>\n<pre><code>#ifndef _c_h\n#define _c_h\n\n#include \"a.h\"\n\nextern USBD_Status USBD_Sync(void);\n\n#endif\n<\/code><\/pre>\n<p>This always seems to result in the error &#8220;unknown type name &#8216;USBD_Status'&#8221; as whichever header is compiled second cannot see the typedef. Removing the include guard from a.h results in the complaint that USBD_Status is being redeclared.<\/p>\n<p>edit: I have double checked all include paths, all includes, all file names and all include guards &#8211; there are no duplicates or typos.<\/p>\n<ol>\n<li>\n<p>It could be that you have another header that uses the same header guard name.<\/p>\n<p>You could add some code to the top of your a.h that does this:<\/p>\n<pre><code>#ifdef _a_h_\n#error this header is already defined\n#endif\n<\/code><\/pre>\n<p>This way you can track down everywhere that a.h is included and see where any oddities may occur.<\/p>\n<p>As stated in the comments, your above example works so the problem must lie somewhere else &#8230;<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:06:19. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing an embedded C program in eclipse with gcc and can&#8217;t see how to get around a certain issue. I have a typedef in a header file which is protected by an include guard. Since I use this typedef in multiple headers, I need to include it in other headers, however when I try [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-586","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=586"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/586\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}