<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Eclipse with the C++ plug-in also has support for Arduino, it might have better support for templates. Have not really tried it, just another option.</div>
<div>
<div id="appendonsend"></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> TriEmbed <triembed-bounces@triembed.org> on behalf of Jon Wolfe via TriEmbed <triembed@triembed.org><br>
<b>Sent:</b> Tuesday, April 21, 2020 10:15 PM<br>
<b>To:</b> Robert Mackie <rob@mackies.org><br>
<b>Cc:</b> alex--- via TriEmbed <triembed@triembed.org><br>
<b>Subject:</b> Re: [TriEmbed] C++ class templates in Arduino - problems</font>
<div> </div>
</div>
<div lang="EN-US">
<div class="x_WordSection1">
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
Coincidentally, I ported the Marlin firmware to build independent of the Arduino build system before they officially supported it. It’s not too hard. Arduino’s libraires are nice, but I’ve always hated its IDE and convoluted build system.</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
If you’re on Windows, Atmel Studio is the way to go for AVR development. In the past few years Atmel Studio did add support for compiling Arduino projects directly, but then that just emulates the same wacky build system.
</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
Once more thought on the original code having issues. That code is using operator new[ ] on Arduino. That is often not defined on embedded platforms. It’s been a little while since I was in Arduino/AVR land, but I think it may be excluded by default to save
 space. Its not hard to add your own definition. It may be giving off that error for that reason. You could try commenting out the original code and try something like:</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
Int* foo = new int[42]; </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
And see if that compiles and links ok.  </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
If not, you need to add 4 operators:</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
operator new, operator new[], operator delete, operator delete[]</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
the operator new and operator new[] can just be wrappers for malloc</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
void* operator new[](size_t size)</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
{</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
                return malloc(size);</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
}</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
And operator delete and delete[] can be just wrappers for free:</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
void operator delete(void* p)</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
{</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
                free(p);</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
}</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
(note one little shortcut, you never need to do a null-check on “free” because the C library spec says calling free with a null pointer is a no-op.  delete nullptr in C++ is also in its spec as a no-op. )</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
All that being said, you really want to avoid dynamic memory on most AVR chips if you can, with 8 or 16k total sram there is not much margin and it wouldn’t take much to fragment the memory to where allocations will fail.
</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
Also always use delete[] ptr;, if ptr was allocated with new[]. In many cases the behavior is identical, but for classes with destructors, calling delete[] ensures that the destructor for each object in the array is called.
</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0in 0in 0in">
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;border:none; padding:0in">
<b>From: </b><a href="mailto:rob@mackies.org">Robert Mackie</a><br>
<b>Sent: </b>Tuesday, April 21, 2020 9:43 PM<br>
<b>To: </b><a href="mailto:jonjwolfe@anibit.com">Jon Wolfe</a><br>
<b>Cc: </b><a href="mailto:triembed@triembed.org">alex--- via TriEmbed</a><br>
<b>Subject: </b>Re: [TriEmbed] C++ class templates in Arduino - problems</p>
</div>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
 </p>
</div>
<div dir="ltr">Or just build a file to so that you are building under gcc/g++ and not arduino. If you are looking for an example of how to do that, I just recently noticed that Marlin (the firmware for a lot 3d printers including reprap) has an example of how
 to do it. You can still build edit and build from the arduino IDE but the build system doesn't play all the games, as far as I can tell. (it adds special file that does something to make that work) At least something like that is going on.
<div><br>
</div>
<div>It has a Marlin.ino file that does NOT have the void setup() and void loop() functions. Rather it has some #ifdef's and some #include's then there is a file named "Marlin_main.cpp" that has the "void loop()" and the "void setup()" and some other stuff
 in it. I don't see a lot of template work, but g++ is g++ with respect to templates. Whatever they are doing works some magic with how the files are organized so that it can be more like a normal code layout, again - I *think*.</div>
<div><br>
</div>
<div>There are many git repos out there with variations of the Marlin source tree if you want to look at it. google or github can find them.</div>
<div><br>
</div>
<div>Here's a comment I found in the Marlin.ino file</div>
<div><br>
/* All the implementation is done in *.cpp files to get better compatibility with avr-gcc without the Arduino IDE */<br>
/* Use this file to help the Arduino IDE find which Arduino libraries are needed and to keep documentation on GCode */<br>
</div>
<div><br>
</div>
<div>No, I don't know what I'm talking about with the Arduino IDE. I just keep faking it to success.</div>
<div><br>
</div>
<div>Rob.<br>
<div><br>
</div>
<div>Rob.</div>
</div>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Tue, Apr 21, 2020 at 5:43 PM Jon Wolfe via TriEmbed <<a href="mailto:triembed@triembed.org">triembed@triembed.org</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div lang="EN-US">
<div class="x_gmail-m_-3051836478725385721WordSection1">
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<u></u> <u></u></p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
Arduino uses gcc under the hood.</p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<u></u> <u></u></p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
The error about the “for” could be a red herring. GCC is full of those. </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<u></u> <u></u></p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
Templates in C++ can be very sensitive to the proper declarations in place prior to a specialization of it.. The Arduino IDE plays games with how files are #included, which is one of the things I do not like about it, it makes things easier for beginners, at
 the expense on control for power users. </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<u></u> <u></u></p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
You syntax looks correct. (as it ought to since it compiles in another environment). Try moving those lines around within the source file, or even putting them in a header files in an inline function, just short term, in an attempt to get a more clear error
 message. </p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<u></u> <u></u></p>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<u></u> <u></u></p>
<div style="border-right:none; border-bottom:none; border-left:none; border-top:1pt solid rgb(225,225,225); padding:3pt 0in 0in">
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;border:none; padding:0in">
<b>From: </b><a href="mailto:triembed@triembed.org" target="_blank">alex--- via TriEmbed</a><br>
<b>Sent: </b>Tuesday, April 21, 2020 5:33 PM<br>
<b>To: </b><a href="mailto:triembed@triembed.org" target="_blank">triembed@triembed.org</a><br>
<b>Subject: </b>[TriEmbed] C++ class templates in Arduino - problems</p>
</div>
<p class="x_MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<u></u> <u></u></p>
</div>
</div>
Anyone familiar with class templates? Code which has no problems under clang-1100.0.33.16 appears to have trouble under whatever Arduino 1.8.9 is using as a compiler.<br>
<br>
Take these two lines as an example:<br>
<br>
Vec3<unsigned char> *frameBuffer = new Vec3<unsigned char>[imageWidth * imageHeight];<br>
for (uint32_t i = 0; i < imageWidth * imageHeight; ++i) frameBuffer[i] = Vec3<unsigned char>(255);<br>
<br>
On Arduino, it complains the ‘for’ on the second line was unexpected. The compiler must be doing something wrong with substituting code on line 1 and losing the ‘;’ perhaps. But why?<br>
<br>
For those with time:<br>
Arduino code is here:<br>
<a href="https://github.com/quarterturn/simple-wireframe" target="_blank">https://github.com/quarterturn/simple-wireframe</a><br>
<br>
Linux or OSX command line code is here:<br>
<a href="https://github.com/quarterturn/wireframe" target="_blank">https://github.com/quarterturn/wireframe</a><br>
<br>
What the code does is render a 3D model as a wireframe, using surface normals for back surface culling, as well as a z-buffer for hidden line removal. On the Arduino, should I ever get it working, it will send a vector list to an Arduino Due, which will use
 it to draw the image on an oscilloscope in XY mode.<br>
<br>
I gotta get off Arduino. Only so many systicks in a day though.<br>
<br>
Alex<br>
<br>
<br>
<br>
_______________________________________________<br>
Triangle, NC Embedded Computing mailing list<br>
<br>
To post message: <a href="mailto:TriEmbed@triembed.org" target="_blank">TriEmbed@triembed.org</a><br>
List info: <a href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org" target="_blank">
http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>
TriEmbed web site: <a href="http://TriEmbed.org" target="_blank">http://TriEmbed.org</a><br>
To unsubscribe, click link and send a blank message: mailto:<a href="mailto:unsubscribe-TriEmbed@bitser.net" target="_blank">unsubscribe-TriEmbed@bitser.net</a>?subject=unsubscribe<br>
<br>
_______________________________________________<br>
Triangle, NC Embedded Computing mailing list<br>
<br>
To post message: <a href="mailto:TriEmbed@triembed.org" target="_blank">TriEmbed@triembed.org</a><br>
List info: <a href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org" rel="noreferrer" target="_blank">
http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>
TriEmbed web site: <a href="http://TriEmbed.org" rel="noreferrer" target="_blank">
http://TriEmbed.org</a><br>
To unsubscribe, click link and send a blank message: mailto:<a href="mailto:unsubscribe-TriEmbed@bitser.net" target="_blank">unsubscribe-TriEmbed@bitser.net</a>?subject=unsubscribe<br>
<br>
</blockquote>
</div>
</div>
</div>
</body>
</html>