{"id":404,"date":"2015-07-30T14:54:27","date_gmt":"2015-07-30T14:54:27","guid":{"rendered":"http:\/\/www.smr.co.uk\/?p=404"},"modified":"2025-01-23T23:27:32","modified_gmt":"2025-01-23T23:27:32","slug":"excel-addin-on-mac-os-x-using-cdt","status":"publish","type":"post","link":"http:\/\/www.smr.co.uk\/?p=404","title":{"rendered":"Working Example C++ Addin for Excel on Mac OS X using CDT"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>There are some\u00a0tutorials that try to explain how to create an Excel Addin for Mac OS X but none of the give you a working example for the Mac, and\u00a0most addin tutorials are aimed at Windows. So I\u00a0have created a working example which you can find <a href=\"https:\/\/github.com\/PollardsEtFilles\/MacCPPExcelAddinExample\">here<\/a>. This short article gives a bit more explanation on what was needed to make the example\u00a0work.<\/p>\n<p><em>Note. These instructions only work for Mac OS X.<\/em><\/p>\n<h2>Prerequisites and Installation<\/h2>\n<p>Install CDT\u00a0(I didn&#8217;t try installing the Eclipse CDT plugin but apparently it works too). I used CDT\u00a0Mars Release (4.5.0).<\/p>\n<ul>\n<li>Download site: https:\/\/eclipse.org\/cdt\/downloads.php\n<ul>\n<li>standalone: http:\/\/www.eclipse.org\/downloads\/packages\/eclipse-ide-cc-developers\/marsr<\/li>\n<li>update site: http:\/\/download.eclipse.org\/tools\/cdt\/releases\/8.7<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Install Xcode, if not already installed.\u00a0I used Version 6.4 (6E35b) with all updates applied.<\/p>\n<p>Buy and install Excel Mac 2011, if not already installed.<\/p>\n<p>Start Eclipse CDT and\u00a0follow the instructions in <a href=\"https:\/\/github.com\/PollardsEtFilles\/MacCPPExcelAddinExample\/blob\/master\/README.md\">README.md<\/a> to import three projects into Eclipse.\u00a0You should see this when you have finished:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-493\" src=\"http:\/\/www.smr.co.uk\/wp-content\/uploads\/2015\/07\/cdt-projects-300x81.png\" alt=\"\" width=\"300\" height=\"81\" srcset=\"http:\/\/www.smr.co.uk\/wp-content\/uploads\/2015\/07\/cdt-projects-300x81.png 300w, http:\/\/www.smr.co.uk\/wp-content\/uploads\/2015\/07\/cdt-projects-500x134.png 500w, http:\/\/www.smr.co.uk\/wp-content\/uploads\/2015\/07\/cdt-projects.png 562w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h2>The C++ Addin &#8211;\u00a0MacExampleAddin<\/h2>\n<p>The addin justs adds two doubles\u00a0together. Its declared in addin.hpp and defined in addin.cpp. In fact we are\u00a0not using C++ directly but we still use C++ file naming conventions which makes the compiler think we are using C++.<\/p>\n<p>The project builds\u00a0a <strong>32 bit<\/strong> dylib and was created by choosing the Shared Library -&gt; Empty Project \u00a0-&gt; MacOSX GCC in the new Eclipse C++ project dialogue box. <strong>Important:<\/strong>\u00a0<em>You must\u00a0build a 32 bit shared library for\u00a0Excel 2011 which is a 32 bit app<\/em>. So\u00a0some changes were made to the settings (Eclipse menu -&gt; Project -&gt;\u00a0Properties) as follows :<\/p>\n<ul>\n<li>In C\/C++ Build -&gt; Settings Tab make the following changes:\n<ul>\n<li>MacOS X C++ Linker -&gt;\u00a0clang++\n<ul>\n<li>Dialect -&gt; ISO C++11 (-std=c++0x)<\/li>\n<li>Miscellaneous \u00a0-&gt;\u00a0-arch i386 -fvisibility=hidden<\/li>\n<\/ul>\n<\/li>\n<li>GCC C++ Complier -&gt;\u00a0clang++\n<ul>\n<li>Dialect -&gt; ISO C++11 (-std=c++0x)<\/li>\n<li>Miscellaneous \u00a0-&gt;\u00a0-c -fmessage-length=0 -fvisibility=hidden -arch i386<\/li>\n<\/ul>\n<\/li>\n<li>GCC C Complier -&gt;\u00a0clang\n<ul>\n<li>Dialect -&gt; ISO C11 (-std=c11)<\/li>\n<li>Miscellaneous \u00a0-&gt;\u00a0-c -fmessage-length=0 -fvisibility=hidden -arch i386<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>One of my\u00a0objectives was to use C++11 but I couldn&#8217;t be bothered downloading the gcc compilers so I tried <a href=\"http:\/\/clang.llvm.org\">clang<\/a>\u00a0which is part of Xcode. After a bit of research I discovered the sensible\u00a0people that wrote clang made the options the same as gcc so that either\u00a0compiler can be used in place of the other.\u00a0Eclipse CDT is more gcc focused so doesn&#8217;t know about clang which is why the settings changes below were made. As you&#8217;ll see, if you try this approach, clang\u00a0works very well.<\/p>\n<p><em>If the settings don&#8217;t work for you either I&#8217;ve missed something in the write up here so check the settings in Eclipse or time has marched on and versions\u00a0have changed and this tutorial no longer works \ud83d\ude41<\/em><\/p>\n<h3><a href=\"https:\/\/github.com\/PollardsEtFilles\/MacCPPExcelAddinExample\/blob\/master\/addin\/src\/addin.hpp\">addin.hpp<\/a><\/h3>\n<p>I\u00a0declared the functions in an extern &#8220;C&#8221; directive to ensure the compiler doesn&#8217;t mangle the names as it will due to the hpp extension. This will make life simpler when we actually use the addin in Excel as the names in the header will be the same in the shared library. There are actually two functions on the header: addNum and addNum2 which will be explained in a moment but they do the same thing, add two numbers together.<\/p>\n<h3><a href=\"https:\/\/github.com\/PollardsEtFilles\/MacCPPExcelAddinExample\/blob\/master\/addin\/src\/addin.cpp\">addin.cpp<\/a><\/h3>\n<p>The file defines EXPORT which is a\u00a0compiler\/linker directive that allows us to selectively export functions in the library along with the appropriate compiler \/ linker options, that is &#8220;-fvisibility=hidden&#8221;.<\/p>\n<p>addNum is also wrapped in an extern directive and preceded by the EXPORT to ensure its visible outside of the shared library.<\/p>\n<p>addNum2 is similarly wrapped by extern and EXPORT but\u00a0calls a function that is invisible in the shared library called notExported. I only did this to show it is possible.<\/p>\n<p>If you examine the library with nm -gU you will see that notExported is in fact not exported which means we aren&#8217;t polluting the namespace for our clients.<\/p>\n<pre>nm -gU libMacExampleAddin.dylib\n00000f40 T _addNum\n00000f70 T _addNum2<\/pre>\n<p>The nm options are:<br \/>\n<strong>-g<\/strong> \u00a0 \u00a0 Display only global (external) symbols.<br \/>\n<b>-U<\/b> \u00a0 \u00a0 Don&#8217;t display undefined symbols.<\/p>\n<h2>The Excel Spreadsheet using the Addin &#8211;\u00a0MacExampleExcel<\/h2>\n<p>Before you start Excel copy\u00a0MacExampleAddin\/Debug\/libMacExampleAddin.dylib to the\u00a0MacExampleExcel directory. When you start Excel you&#8217;ll need to accept macros, but don&#8217;t worry there&#8217;s nothing bad in there, just some VBA to access the shared library.<\/p>\n<h3>addin-test.xlsm<\/h3>\n<p>When you start Excel it will ask you to accept macros (say yes) but it may not start the VBA editor so select menu Tools -&gt; Macro -&gt; Visual Basic Editor (I&#8217;ve exported the <a href=\"https:\/\/github.com\/PollardsEtFilles\/MacCPPExcelAddinExample\/blob\/master\/excel\/Module1.bas\">basic\u00a0module here<\/a>).<\/p>\n<p>As you can see there are two sorts of functions, Private and non-private\u00a0functions. The private ones are not visible to Excel but the rest are. The reason for separation is because that&#8217;s the only way I could get it to work, but conveniently the shared library functions are wrapped and hidden in the private functions and referenced by Lib &#8220;&#8230;.&#8221;. The public functions simply call the private ones to return the values you see in the spreadsheet.<\/p>\n<p>As you&#8217;ll see I&#8217;ve shown functions with absolute paths (addFunctionAbsolute) which only work for me. Yes, not good but it shows it works and you can change it if you want to make it work for your Mac. Secondly there are examples that assume a library is present in the same directory as the Excel spread sheet (addFunctionLocal). These also work if you look at the spreadsheet. Finally there are relative paths (addFunctionRelative) which don&#8217;t work but should, but don&#8217;t know why.<\/p>\n<p>But what you should see is this:<a href=\"http:\/\/www.smr.co.uk\/wp-content\/uploads\/2015\/07\/cdt-excel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-491\" src=\"http:\/\/www.smr.co.uk\/wp-content\/uploads\/2025\/01\/cdt-excel-300x63.png\" alt=\"\" width=\"300\" height=\"63\" srcset=\"http:\/\/www.smr.co.uk\/wp-content\/uploads\/2025\/01\/cdt-excel-300x63.png 300w, http:\/\/www.smr.co.uk\/wp-content\/uploads\/2025\/01\/cdt-excel-768x162.png 768w, http:\/\/www.smr.co.uk\/wp-content\/uploads\/2025\/01\/cdt-excel-500x105.png 500w, http:\/\/www.smr.co.uk\/wp-content\/uploads\/2025\/01\/cdt-excel.png 798w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>The C Main program that uses the Addin &#8211;\u00a0MacExampleMain<\/h2>\n<p>This is a simple \u00a0main program again generated by CDT but as a &#8220;Hello World C++ Project&#8221; with MacOSX GCC tool chains. I shan&#8217;t detail all the changes as you can look at them yourself but basically they are:<\/p>\n<ul>\n<li>added -arch i386 to all the tools<\/li>\n<li>added the include path\u00a0to MacExampleAddin\/src<\/li>\n<li>added the library path to\u00a0MacExampleAddin\/Debug<\/li>\n<li>added the library\u00a0MacExampleAddin (without the preceding lib and .dylib extension)<\/li>\n<li>used clang++ and C++11<\/li>\n<li>used clang and C11<\/li>\n<li>changed main.cpp to test addNum as follows:<\/li>\n<\/ul>\n<pre>if (addNum(1,2) != 3) {\n\u00a0 cerr &lt;&lt; \"FAIL: Wrong number from addnum expected 3 and got \" &lt;&lt; addNum(1,2) &lt;&lt; endl;\n}<\/pre>\n<p>For your convenience there is an Eclipse launch that sets up DYLD_LIBRARY_PATH path to allow you to run the project in Eclipse as follows:<\/p>\n<pre>Run -&gt; Run History -&gt;\u00a0MacExampleMain\n<\/pre>\n<p>You should see this in the console:<\/p>\n<pre>PASS: Correct number from addnum expected 3 and got 3<\/pre>\n<p>You need to set\u00a0DYLD_LIBRARY_PATH to point to the directory where the dylib is if you intend to run the program in a terminal window:<\/p>\n<pre>peter$ .\/Debug\/MacExampleMain\u00a0\ndyld: Library not loaded: libMacExampleAddin.dylib\n\u00a0 Referenced from: \/Users\/peter\/Documents\/eclipse\/workspace-cdt\/MacCPPExcelAddinExample\/main\/.\/Debug\/MacExampleMain\n\u00a0 Reason: image not found\nTrace\/BPT trap: 5<\/pre>\n<p>Fails because the library can&#8217;t be found.<\/p>\n<pre>peter$ export DYLD_LIBRARY_PATH=..\/addin\/Debug\/\npeter$ .\/Debug\/MacExampleMain\u00a0\nPASS: Correct number from addnum expected 3 and got 3<\/pre>\n<p>This works because the library can be found.<\/p>\n<p>But you don&#8217;t need to set\u00a0DYLD_LIBRARY_PATH\u00a0\u00a0if the library is in the current working directory:<\/p>\n<pre>peter$ ls -l\n-rwxr-xr-x\u00a0 1 peter\u00a0 staff\u00a0 17312 30 Jul 21:04 MacExampleMain\n-rwxr-xr-x\u00a0 1 peter\u00a0 staff \u00a0 4704 30 Jul 21:04 libMacExampleAddin.dylib\n\npeter$ .\/MacExampleMain\u00a0\nPASS: Correct number from addnum expected 3 and got 3<\/pre>\n<p>\u00a9 Copyright 2015, Peter Lappo, SMR Ltd.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction There are some\u00a0tutorials that try to explain how to create an Excel Addin for Mac OS X but none of the give you a working example for the Mac, and\u00a0most addin tutorials are aimed at Windows. So I\u00a0have created &hellip; <a href=\"http:\/\/www.smr.co.uk\/?p=404\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a> <a href=\"http:\/\/www.smr.co.uk\/?p=404\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-404","post","type-post","status-publish","format-standard","hentry","category-technology"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404"}],"version-history":[{"count":2,"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions"}],"predecessor-version":[{"id":494,"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions\/494"}],"wp:attachment":[{"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.smr.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}