<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Makefile: Compiling C++</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/46490/makefile-compiling-c</link><description>Details: SDK v15.2.0. Windows 8.1. 
 
 I am trying to adjust the ble_app_hts Makefile such that I can compile C++. I added cppmain.cpp to SRC_FILES and that threw me an error 
 
 
 
 My cppmain.cpp looks like this 
 
 
 
 I can remove this error by adding</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 02 May 2019 16:40:12 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/46490/makefile-compiling-c" /><item><title>RE: Makefile: Compiling C++</title><link>https://devzone.nordicsemi.com/thread/184982?ContentTypeID=1</link><pubDate>Thu, 02 May 2019 16:40:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f15128cc-ec9f-4dcd-81cf-e2a51196b980</guid><dc:creator>mbards</dc:creator><description>&lt;p&gt;Many libraries have extern &amp;quot;C&amp;quot; baked into each header file (ex: stm, tensorflow, &lt;a href="https://stackoverflow.com/questions/67894/why-do-we-need-extern-c-include-foo-h-in-c"&gt;1&lt;/a&gt;). Purely out of curiosity, why did Nordic choose not to do this?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#ifdef __cplusplus
 extern &amp;quot;C&amp;quot; {
#endif

...code...

#ifdef __cplusplus
 }
#endif&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;To wrap up the question &amp;quot;how do I adjust the SDK makefile so it compiles C++&amp;quot; the answer is, you don&amp;#39;t have to do anything. If you add a .cpp file to the SRC_FILES list it compiles fine. Just wrap any C includes in extern &amp;quot;C&amp;quot;. For example, in main.cpp:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;extern &amp;quot;C&amp;quot; {
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for your response Hakon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Makefile: Compiling C++</title><link>https://devzone.nordicsemi.com/thread/184255?ContentTypeID=1</link><pubDate>Mon, 29 Apr 2019 09:26:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ee6c95a-a08b-496b-9007-7c420fbe6236</guid><dc:creator>Hakon</dc:creator><description>[quote user=""]I can remove this error by adding extern &amp;quot;C&amp;quot; before the declaration of&amp;nbsp; &lt;em&gt;nrf_pwr_mgmt_run()&lt;/em&gt; but that&amp;#39;s hacky.[/quote]
&lt;p&gt;&amp;nbsp;That&amp;#39;s because the &lt;em&gt;nrf_pwr_mgmt_run() &lt;/em&gt;is a function from a library written in C that you refer to in a C++ file. That&amp;#39;s how you&amp;#39;re supposed to handle C code in a C++ project. Look at &lt;a href="https://isocpp.org/wiki/faq/mixing-c-and-cpp"&gt;&lt;em&gt;&lt;/em&gt;&lt;/a&gt;&lt;em&gt;https://isocpp.org/wiki/faq/mixing-c-and-cpp.&lt;/em&gt;&lt;em&gt; &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]but if there is an easier way please let me know[/quote]
&lt;p&gt;&amp;nbsp;Not as far as I know.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>