Go Down

Topic: c++ library restrictions (Read 772 times) previous topic - next topic

NJavrGuy

Hi everyone, I am pretty new to arduino, and somewhat new to C++.

I am trying to create some libraries, and I am wondering if anyone can help me understand what c++ libraries are accessible.

For example, in a sketch, I tried to make a vector, a dynamic array, but the sketch would not allow me to do this.

Would it be possible to do this in a library that I use in a sketch?

I don't quite understand how this would work, any help would be appreciated.

Nick Gammon

Read this before posting a programming question

Quote
For example, in a sketch, I tried to make a vector ...


Post this sketch.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Denbo


For example, in a sketch, I tried to make a vector, a dynamic array, but the sketch would not allow me to do this.


NJavrGuy,  Are you trying to use vectors from the standard template library?  Just fyi they are not there.

However someone did go through the trouble of porting them  http://andybrown.me.uk/wk/2011/01/15/the-standard-template-library-stl-for-avr-with-c-streams/

NJavrGuy

#3
Feb 08, 2013, 10:49 pm Last Edit: Feb 08, 2013, 10:52 pm by NJavrGuy Reason: 1
OK, sure, this is not meant to do anything more than test out vectors in arduino ide.

I am getting an error message: 'vector' was not declared in this scope, and the highligted line is:

vector<int> DynArrNums(1);


Thanks for your help!

Code: [Select]

#include <vector>

void setup()
{
 
        using namespace std;

       Serial.begin(9600);
 
       vector<int> DynArrNums(1);

DynArrNums[0]=365;

int aNewNumber=335;

DynArrNums.push_back(aNewNumber);


       for(int i=0;i<DynArrNums.size();++i)
         {
           Serial.println(DynArrNums[i];
         }
}


void loop()
{
 
 
}

NJavrGuy



For example, in a sketch, I tried to make a vector, a dynamic array, but the sketch would not allow me to do this.


NJavrGuy,  Are you trying to use vectors from the standard template library?  Just fyi they are not there.

However someone did go through the trouble of porting them  http://andybrown.me.uk/wk/2011/01/15/the-standard-template-library-stl-for-avr-with-c-streams/


Yup, that is definitely the problem.

This is kind of what I am trying to understand, what libraries are in arduino, and which ones are not.

Interesting, I will check out the post, thanks.

Denbo


Yup, that is definitely the problem.

This is kind of what I am trying to understand, what libraries are in arduino, and which ones are not.

Interesting, I will check out the post, thanks.


Here is the list of libraries that are installed with the Arduino ide:   http://arduino.cc/en/Reference/Libraries

If you decide to download a library and install it yourself, follow these instructions:   http://arduino.cc/en/Guide/Libraries

Also just FYI... when you include a file it normally has the .h extension.  The code you posted just said #include <vector> but should have been #include <vector.h> had you indeed installed the library.


NJavrGuy



Yup, that is definitely the problem.

This is kind of what I am trying to understand, what libraries are in arduino, and which ones are not.

Interesting, I will check out the post, thanks.


Here is the list of libraries that are installed with the Arduino ide:   http://arduino.cc/en/Reference/Libraries

If you decide to download a library and install it yourself, follow these instructions:   http://arduino.cc/en/Guide/Libraries

Also just FYI... when you include a file it normally has the .h extension.  The code you posted just said #include <vector> but should have been #include <vector.h> had you indeed installed the library.




Ok, thanks!

When I use vector in eclipse, I am able to just include vector, and not the h file, good to know.

Can any c++ library be brought into arduino? Or does something have to be done to do so?

Denbo

#7
Feb 08, 2013, 11:11 pm Last Edit: Feb 08, 2013, 11:13 pm by Denbo Reason: 1

Can any c++ library be brought into arduino? Or does something have to be done to do so?


It really depends on the library.   If you read the link I posted on the STL port you will see he had to do a bit of work to get things to work.  Something simple might be a straight port while others might be quite impossible with the limited memory one must work with.  

Oh and let me correct myself, normally libraries end with .h.  I thought his port for the vector library had .h on the end but according to the sample code on his website I was mistaken.


NJavrGuy



Can any c++ library be brought into arduino? Or does something have to be done to do so?


It really depends on the library.   If you read the link I posted on the STL port you will see he had to do a bit of work to get things to work.  Something simple might be a straight port while others might be quite impossible with the limited memory one must work with.  

Oh and let me correct myself, normally libraries end with .h.  I thought his port for the vector library had .h on the end but according to the sample code on his website I was mistaken.




Thanks Denbo, very helpful.

michinyon

If,  by "vector",   you mean an array with dynamic length,   then it is unlikely to be useful on the Arduino.
Dynamically allocated memory does not work very well on the arduino.

You'd be better advised to learn to do with it,  and implement your project another way.

Nick Gammon

If you follow the link to Andy Brown's port of the STL, you can have that going fairly easily. You need to copy quite a few files as described on his page.

However as others are pointing out, the STL uses dynamic memory allocation, and you don't have a heap of free memory on the Arduino. Plus currently the free() function has a bug, which you can correct by following this link:

  http://arduino.cc/forum/index.php/topic,145765

Personally I like the Standard Template Library (STL), but as I say, I would be using it with caution on this platform.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

NJavrGuy


If you follow the link to Andy Brown's port of the STL, you can have that going fairly easily. You need to copy quite a few files as described on his page.

However as others are pointing out, the STL uses dynamic memory allocation, and you don't have a heap of free memory on the Arduino. Plus currently the free() function has a bug, which you can correct by following this link:

  http://arduino.cc/forum/index.php/topic,145765

Personally I like the Standard Template Library (STL), but as I say, I would be using it with caution on this platform.

If,  by "vector",   you mean an array with dynamic length,   then it is unlikely to be useful on the Arduino.
Dynamically allocated memory does not work very well on the arduino.

You'd be better advised to learn to do with it,  and implement your project another way.


Thanks to both of you for the heads up.

I will definitely keep this in mind.

Go Up