Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static variable - __cxa_guard_acquire and __cxa_guard_release implementation #500

Closed
ghost opened this issue Jul 1, 2015 · 24 comments
Closed

Comments

@ghost
Copy link

ghost commented Jul 1, 2015

Getting the following errors with the minimal test program below. Can work around by making variable global, but it would be good to fix the problem with static.

C:\Users\Ray\AppData\Local\Temp\build2036106428985831191.tmp\sketch_jul01b.cpp.o: In function `setup':
sketch_jul01b.cpp:(.text+0xc): undefined reference to `__cxa_guard_acquire'
sketch_jul01b.cpp:(.text+0x10): undefined reference to `__cxa_guard_release'
C:\Users\Ray\AppData\Local\Temp\build2036106428985831191.tmp\sketch_jul01b.cpp.o: In function `loop':
sketch_jul01b.cpp:(.text+0x25): undefined reference to `__cxa_guard_acquire'
sketch_jul01b.cpp:(.text+0x37): undefined reference to `__cxa_guard_release'
collect2.exe: error: ld returned 1 exit status
Error compiling.
void setup() 
{
}

void loop() 
{
    static uint32_t lastMillis = millis();
}
@reaper7
Copy link
Contributor

reaper7 commented Jul 1, 2015

declaring static variable in the loop? again and again and ....

I think the proper method is:

static uint32_t lastMillis;

void setup() 
{
}

void loop() 
{
    lastMillis = millis();
}

@ghost
Copy link
Author

ghost commented Jul 1, 2015

Static means it is initialised the first time loop() is executed and it retains its value when loop() is called again. The advantage is that the variable has local scope in loop() rather than being global.

Code above compiles OK for Arduino Uno etc but not for ESP8266. Maybe it's not supported on ESP8266, in which case note should be added to documentation page.

See www.learncpp.com/cpp-tutorial/43-static-duration-variables/

On 1 Jul 2015, at 20:00, reaper7 notifications@github.com wrote:

declaring a static variable in the loop? again and again and ....

I think the proper method is:

static uint32_t lastMillis;

void setup()
{
}

void loop()
{
lastMillis = millis();
}

Reply to this email directly or view it on GitHub.

@reaper7
Copy link
Contributor

reaper7 commented Jul 1, 2015

probably You are right :)

@Links2004
Copy link
Collaborator

@hackscribble what version and OS you use?
I use windows with the latest git and I can use static in the loop or in any other function it is working fine.

@ghost
Copy link
Author

ghost commented Jul 1, 2015

Arduino IDE 1.6.5 on Windows 8.

esp8266 version 1.6.4-673-g8cd3697 installed through Boards manager.

Board settings: Generic ESP8266 module, 80MHz, 512K, 115200.

EDIT Tried removing and reinstalling via Boards Manager, and also tested on 1.6.4. Same errors.

@Links2004
Copy link
Collaborator

ok can reproduce it, only using a function is not working.

no error with:

void setup() 
{
}

void loop() 
{
   static uint32_t lastMillis = 0;
   lastMillis = millis();
}

strange.

Note code will not have same functionality.

@ghost
Copy link
Author

ghost commented Jul 1, 2015

Thank you @Links2004 - that's a good workaround.

@Makuna
Copy link
Collaborator

Makuna commented Jul 2, 2015

Actually, the original code is flawed in that when the static is initialed you have no idea if millis has been initialized, so it should be avoided. You should use Links2004 code as the correct way to do this.

@ghost
Copy link
Author

ghost commented Jul 2, 2015

Thanks, @Makuna. I had not thought about that before, but I see exactly what you mean. As @Links2004 pointed out, his code is not exactly the same functionality. So I think I will change to a global variable, set to millis() at the end of setup().

@igrr
Copy link
Member

igrr commented Jul 6, 2015

Actually the original code is fine — C++ standard guarantees that local static initializer will be called exactly once, first time the function is executed. We just need to add __cxa_guard* implementations.

@Makuna
Copy link
Collaborator

Makuna commented Jul 6, 2015

@igrr when does the timer/variables get initialized for millis? Can you guarantee this happens before this static variable is initialised? This is my point.

@igrr
Copy link
Member

igrr commented Jul 6, 2015

In this case local static variable will be initialized the first time loop() function runs, that's what C++ standard guarantees. millis is certainly initialized before that, so that you can use it in setup()/loop().

@Links2004 Links2004 changed the title Static variable Static variable - __cxa_guard_acquire and __cxa_guard_release implementation Nov 27, 2015
ofesseler added a commit to braubar/braubar-temp that referenced this issue Jan 4, 2016
because of a known bug, the declaration of DhcpClass has to be outside
the EthernetClass::begin(*mac) Method. Only if it is used with DHCP this
 error occurs.

More details: esp8266/Arduino#500
@tuxedo0801
Copy link
Contributor

Is there a chance to get this fixed instead of implementing a workaround? Is there someone out there with the required expert knowledge?

Looks like I cannot eliminate/workaround all static stuff in the library I try to port to ESP8266 (see #1498).

@tuxedo0801
Copy link
Contributor

push

@Duality4Y
Copy link
Contributor

you don't have to set a value for static variables,
they will always be initialized to zero.

besides that links2004's code is the way you should do it!

@tuxedo0801
Copy link
Contributor

The point is: The library (not written by me, see related #1498) I'm talking about works quite well with Atmel/AVR Arduinos.

But using it with ESP8266 brings up those errors.

Rewriting the library so that it works with ESP8266 (as suggestes by @Links2004), would result in rewriting the whole library (yes, it makes massive use of those static stuff. I'm also not happy with it, but that's the current fact).

That's not the idea of a portable library and an "arduino-style" compatible ESP8266.

So instead of working around the problem, I would really recommend solving this issue at it's root.
Sadly, I'm not that good in C/C++ to do it. So I hope someone else can step up and fix it. I would even donate for this fix...

br,
Alex

@Duality4Y
Copy link
Contributor

I see your point and I agree.
as iggr said:

We just need to add __cxa_guard* implementations.

I myself have walked against this error. and solved it by making that variable (which is an object in my case) global to that function.

I was just pointing out though the correctness and convention of such static variable use in a function :)

@igrr
Copy link
Member

igrr commented Feb 5, 2016

#1586 should resolve this.

@tuxedo0801 could you please try this pull request with your library and see if it works?
Thanks.

@tuxedo0801
Copy link
Contributor

Thanks a lot. Will test it, but takes until start of next week.. Will report back about the result.

@igrr igrr closed this as completed Mar 11, 2016
@kenji21
Copy link

kenji21 commented Jun 22, 2016

Got the same issue with a Feather 32u4 from adafruit in /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp :

  101 PluggableUSB_& PluggableUSB()
  102 {
  103         static PluggableUSB_ obj;
  104         return obj;
  105 }

Moving the static before the method worked (not fan because it is an Arduino.app provided file). But also adding the flag (like the arduino IDE does) : -fno-threadsafe-statics make compilation succeed.

@vdiallonort
Copy link

Hi,I am looking for the work-arround you guys find as i am hitting the same issue.Does there is a fork of the library somewhere or little information about how to deal with this issue ?

@igrr
Copy link
Member

igrr commented Jul 28, 2016

This is fixed since 2.2.0.
If you want more info about the fix, feel free to scroll the page up and click on the link which says "#1586 should resolve this" :) There you will find corresponding pull request including all the changes.

@vdiallonort
Copy link

Thanks,just to make sure i got it straight version 2.2 is for the ESP8266 arduino core ? So that mean i got the wrong version somehow and need to upgrade

@igrr
Copy link
Member

igrr commented Jul 28, 2016

The latest version right now is 2.3.0. If you see this happening in 2.3.0, please open a new issue and post detailed steps to reproduce (example sketch, etc.). Thanks.

edit: https://github.com/esp8266/Arduino#stable-version-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants