{"id":2250,"date":"2016-10-31T15:12:18","date_gmt":"2016-10-31T22:12:18","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=2250"},"modified":"2020-06-04T14:27:11","modified_gmt":"2020-06-04T21:27:11","slug":"solving-the-name-conflicts-between-c-and-c-files","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/tips\/externc\/","title":{"rendered":"Solving function name conflicts between C and C++ files"},"content":{"rendered":"<p>This tutorial shows how to resolve the &#8220;undefined reference&#8221; problem when using both C and C++ code in one project. We will create a basic project, show how to reproduce the &#8220;undefined reference&#8221; error for a function that is defined in a C file, explain how it happens and show how to solve it in a universal way.<\/p>\n<ol>\n<li>Start Visual Studio and create a project using any of the VisualGDB project wizards. We will show it based on the Embedded wizard, but the same steps will also work for the Linux, Android and Windows wizards:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-externcname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2251\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-externcname.png\" alt=\"01-externcname\" width=\"822\" height=\"485\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-externcname.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-externcname-300x177.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Add a new file called add.h to your project and add the following code there:\n<pre class=\"\">#pragma once\r\nint add(int a, int b);<\/pre>\n<\/li>\n<li>Create a new file called <strong>add.c<\/strong> (not <strong>add.cpp<\/strong>) and provide an implementation for that function. You can use the &#8216;Create implementation&#8217; command provided by the Clang IntelliSense for simplicity:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-header.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2252\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-header.png\" alt=\"02-header\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-header.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-header-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-header-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Build the project to ensure there are no syntax errors:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-source.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2253\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-source.png\" alt=\"03-source\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-source.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-source-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-source-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Now include the &lt;add.h&gt; file from your main C++ source (if your project has a .c source, change its extension to .cpp). If you build your project now, you will see the following error:\n<pre class=\"\">undefined reference to `add(int, int)'<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-undefined.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2254\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-undefined.png\" alt=\"04-undefined\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-undefined.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-undefined-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-undefined-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>The error happens at the linking stage, i.e. after each of the source files was compiled to a .o file but before all the .o files are merged into the final executable. To see why exactly this happens we will use the &#8220;nm&#8221; tool from the GCC toolchain. The tool displays a list of symbols in an object file. Run it on the object file that defines the add() function and the one that uses it:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-names.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2258\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-names.png\" alt=\"05-names\" width=\"979\" height=\"325\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-names.png 979w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-names-300x100.png 300w\" sizes=\"(max-width: 979px) 100vw, 979px\" \/><\/a>Note that the add.o file provides a symbol called &#8220;<strong>add<\/strong>&#8221; while the LEDBlink.o file expects a symbol called &#8220;<strong>_Z3addii<\/strong>&#8221; (&#8216;U&#8217; before the names means that the symbol is undefined in this object file and must be defined elsewhere). The difference in the names causes the &#8220;undefined reference&#8221; error shown above.<\/li>\n<li>The &#8220;<strong>_Z3addii<\/strong>&#8221; is the internal GCC name for &#8216;function <strong>add<\/strong> that takes arguments of type <strong>int<\/strong> and <strong>int<\/strong>). This name transformation is done automatically to support function overloading. E.g. you can declare another function called &#8216;add&#8217; that will accept 3 arguments and the code will compile successfully (and stop at linking as before):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-add2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2257\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-add2.png\" alt=\"06-add2\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-add2.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-add2-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-add2-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Looking into the new .o file will show that it now expects 2 functions: <strong>_Z3addii<\/strong> and <strong>_Z3addiii<\/strong>. So the name transformation is needed for the linker to distinguish between those functions:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-sym2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2256\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-sym2.png\" alt=\"07-sym2\" width=\"979\" height=\"325\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-sym2.png 979w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-sym2-300x100.png 300w\" sizes=\"(max-width: 979px) 100vw, 979px\" \/><\/a><\/li>\n<li>The easiest way to fix this error is to put an <strong>extern &#8220;C&#8221;<\/strong> block around the #include&lt;&gt; statement:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-externc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2255\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-externc.png\" alt=\"08-externc\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-externc.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-externc-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-externc-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>When a C++ compiler encounters a function defined inside an extern &#8220;C&#8221; block, it disables the name transformation for it, so it will generate the same name for it as the C compiler would:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-externsym.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2259\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-externsym.png\" alt=\"09-externsym\" width=\"979\" height=\"325\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-externsym.png 979w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-externsym-300x100.png 300w\" sizes=\"(max-width: 979px) 100vw, 979px\" \/><\/a><\/li>\n<li>As a side effect of this, you will not be able to overload the function anymore. Any attempt to declare another function called &#8216;add&#8217; will result in a compilation error, as the compiler cannot distinguish between those functions without using name transformation that was disabled by extern &#8220;C&#8221;: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-conflict.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2260\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-conflict.png\" alt=\"10-conflict\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-conflict.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-conflict-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-conflict-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>In order to avoid putting extern &#8220;C&#8221; around the included header files, it is recommended to put this block directly inside them. However putting it unconditionally will prevent the C files from compiling properly:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-externerr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2261\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-externerr.png\" alt=\"11-externerr\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-externerr.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-externerr-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-externerr-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>The solution to this is very simple. Use the __cplusplus macro to detect whether the header file is parsed by a C or C++ compiler and only include extern &#8220;C&#8221; for C++ files:\n<pre class=\"\">#pragma once\r\n\r\n#ifdef __cplusplus\r\nextern \"C\"\r\n{\r\n#endif\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 \/\/place your functions here\r\n\u00a0\u00a0 \u00a0\r\n#ifdef __cplusplus\u00a0\u00a0 \u00a0\r\n}\r\n#endif<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-ifdef.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2262\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-ifdef.png\" alt=\"12-ifdef\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-ifdef.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-ifdef-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-ifdef-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Same problem will happen if a .c file tries to call a function defined in a .cpp file without declaring it with <strong>extern &#8220;C&#8221;<\/strong>. If this happens, ensure you have <strong>extern &#8220;C&#8221;<\/strong> in your header file and that the header file is actually included by the .cpp file before the function is defined.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to resolve the &#8220;undefined reference&#8221; problem when using both C and C++ code in one project.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[134],"tags":[136,213],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2250"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=2250"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2250\/revisions"}],"predecessor-version":[{"id":2264,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2250\/revisions\/2264"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}