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.

________________________________
From: TriEmbed <[email protected]> on behalf of Jon Wolfe via 
TriEmbed <[email protected]>
Sent: Tuesday, April 21, 2020 10:15 PM
To: Robert Mackie <[email protected]>
Cc: alex--- via TriEmbed <[email protected]>
Subject: Re: [TriEmbed] C++ class templates in Arduino - problems


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.



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.



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:



Int* foo = new int[42];



And see if that compiles and links ok.



If not, you need to add 4 operators:

operator new, operator new[], operator delete, operator delete[]





the operator new and operator new[] can just be wrappers for malloc



void* operator new[](size_t size)

{

                return malloc(size);

}



And operator delete and delete[] can be just wrappers for free:



void operator delete(void* p)

{

                free(p);

}





(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. )





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.



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.







From: Robert Mackie<mailto:[email protected]>
Sent: Tuesday, April 21, 2020 9:43 PM
To: Jon Wolfe<mailto:[email protected]>
Cc: alex--- via TriEmbed<mailto:[email protected]>
Subject: Re: [TriEmbed] C++ class templates in Arduino - problems



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.

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*.

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.

Here's a comment I found in the Marlin.ino file

/* All the implementation is done in *.cpp files to get better compatibility 
with avr-gcc without the Arduino IDE */
/* Use this file to help the Arduino IDE find which Arduino libraries are 
needed and to keep documentation on GCode */

No, I don't know what I'm talking about with the Arduino IDE. I just keep 
faking it to success.

Rob.

Rob.

On Tue, Apr 21, 2020 at 5:43 PM Jon Wolfe via TriEmbed 
<[email protected]<mailto:[email protected]>> wrote:



Arduino uses gcc under the hood.



The error about the “for” could be a red herring. GCC is full of those.



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.



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.





From: alex--- via TriEmbed<mailto:[email protected]>
Sent: Tuesday, April 21, 2020 5:33 PM
To: [email protected]<mailto:[email protected]>
Subject: [TriEmbed] C++ class templates in Arduino - problems



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.

Take these two lines as an example:

Vec3<unsigned char> *frameBuffer = new Vec3<unsigned char>[imageWidth * 
imageHeight];
for (uint32_t i = 0; i < imageWidth * imageHeight; ++i) frameBuffer[i] = 
Vec3<unsigned char>(255);

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?

For those with time:
Arduino code is here:
https://github.com/quarterturn/simple-wireframe

Linux or OSX command line code is here:
https://github.com/quarterturn/wireframe

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.

I gotta get off Arduino. Only so many systicks in a day though.

Alex



_______________________________________________
Triangle, NC Embedded Computing mailing list

To post message: [email protected]<mailto:[email protected]>
List info: http://mail.triembed.org/mailman/listinfo/triembed_triembed.org
TriEmbed web site: http://TriEmbed.org
To unsubscribe, click link and send a blank message: 
mailto:[email protected]<mailto:[email protected]>?subject=unsubscribe

_______________________________________________
Triangle, NC Embedded Computing mailing list

To post message: [email protected]<mailto:[email protected]>
List info: http://mail.triembed.org/mailman/listinfo/triembed_triembed.org
TriEmbed web site: http://TriEmbed.org
To unsubscribe, click link and send a blank message: 
mailto:[email protected]<mailto:[email protected]>?subject=unsubscribe

_______________________________________________
Triangle, NC Embedded Computing mailing list

To post message: [email protected]
List info: http://mail.triembed.org/mailman/listinfo/triembed_triembed.org
TriEmbed web site: http://TriEmbed.org
To unsubscribe, click link and send a blank message: 
mailto:[email protected]?subject=unsubscribe

Reply via email to