Skip to content

Fix for undefined reference to atexit() function. #2229

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

Closed
wants to merge 2 commits into from
Closed

Fix for undefined reference to atexit() function. #2229

wants to merge 2 commits into from

Conversation

Chris--A
Copy link
Contributor

@Chris--A Chris--A commented Aug 4, 2014

Hi, I recently ran into an issue which is present in all branches of the IDE.

The linker fails due to a missing standard library function atexit().
This issue affects all IDE branches I have tested:

  • 1.0.5-r2
  • 1.5.6-r2
  • 1.5.7 using C++11

The problem manifests itself when giving an object with a defined destructor, static local storage duration.

Here are two basic examples of the error:

void setup(){
  static String s;
}

void loop() {}

Its not the String libraries fault, any object that requires implicit destruction causes an error:

struct Foo{
  Foo(){}
  ~Foo(){}
};

void setup(){
  static Foo f;
}

void loop() {}

The fix I propose will allow these examples to compile, and I have tested it on the three versions of the IDE I mentioned above.

I created this patch on the master, can you automatically merge this with 1.5.x, or do I need to make a new pull request?

As a side note, if the function is moved else where, it must be declared extern "C". This is taken care of by Arduino.h

Fixes #1919

Added replacement stub for cstdlib `atexit()` funciton.
This is an empty stub to simply allow use of complex types with a non global static lifetime.
For more complex handling the function `atexit` can be redefined in user code.
@Chris--A Chris--A changed the title Patch 3 Fix for undefined reference to atexit() function. Aug 4, 2014
@cmaglie cmaglie added this to the Release 1.5.8 milestone Aug 6, 2014
@matthijskooijman
Copy link
Collaborator

Code looks good to me. I do have some comments on the PR itself:

  • These two commits should really be a single commit - neither of them makes sense without the other.
  • You should put "Fixes atexit() called after setup() #1919" at the end of the commit message, to have github autoclose that issue.
  • I'd like a little bit more explanation (in the comments in the code) as to why it's ok to have an empty exit function (i.e., because an Arduino program never actually exits, there is no need to call atexit handlers, but since the compiler inserts calls to atexit automatically, it does need to exist).

To update your pullrequest, you can apply the changes locally (if you don't know how, try running git rebase -i origin/master and be amazed about the power of rebase ;-p) and then do a forced push to your patch-3 branch to update the PR here.

I think having a PR for ide-1.5.x would also be good. The changes are fairly trivial, but when you submit a PR we can be sure the changes merged are exactly the ones you tested. However, @cmaglie has the final word on this, he's the one doing the merges.

@cmaglie
Copy link
Member

cmaglie commented Aug 6, 2014

I regularly merge master into ide-1.5.x so there is no need to create a separate patch for 1.5.

@Chris--A
If you can rewrite the commit as suggested by Matthijs it would be great!

@Chris--A
Copy link
Contributor Author

Chris--A commented Aug 6, 2014

I have tried downloading the repo on windows a couple of times without luck, I have made this request via the webpage which is why its two commits.

I'll try again to see if it will work. If it does, I'll update the PR. It mat take a few hours so I'll report back.

I will also update the pull request info too.

@Chris--A
Copy link
Contributor Author

Chris--A commented Aug 6, 2014

Every time I try and clone the repo it fails. Git-hub responds that the repo is in an unknown state and possibly corrupted. I think windows git-hub has some problems with long path names.

I can update the code comments, however it requires an additional commit when using the web page editor.

@matthijskooijman
Copy link
Collaborator

If you update using the editor, then either @cmaglie can manually merge and squahs the commits, or perhaps I can do it and create a new PR for it.

cmaglie pushed a commit that referenced this pull request Aug 8, 2014
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

#2229
#1919
@cmaglie
Copy link
Member

cmaglie commented Aug 8, 2014

I've manually merged the patch, here the "squashed" commit:

1bbcb2f

Thank you!

@cmaglie cmaglie closed this Aug 8, 2014
klightspeed pushed a commit to klightspeed/EthertenMP3Player that referenced this pull request Aug 30, 2015
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

arduino/Arduino#2229
arduino/Arduino#1919
klightspeed pushed a commit to klightspeed/Arduino-Libraries that referenced this pull request Sep 6, 2015
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

arduino/Arduino#2229
arduino/Arduino#1919

(Filtered from arduino/Arduino@1bbcb2f)
facchinm pushed a commit to arduino/ArduinoCore-avr that referenced this pull request Sep 20, 2017
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

arduino/Arduino#2229
arduino/Arduino#1919
facchinm pushed a commit to arduino/ArduinoCore-avr that referenced this pull request Sep 20, 2017
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

arduino/Arduino#2229
arduino/Arduino#1919
facchinm pushed a commit to arduino/ArduinoCore-avr that referenced this pull request Sep 20, 2017
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

arduino/Arduino#2229
arduino/Arduino#1919
facchinm pushed a commit to arduino/ArduinoCore-avr that referenced this pull request Sep 20, 2017
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

arduino/Arduino#2229
arduino/Arduino#1919
rickyrockrat pushed a commit to rickyrockrat/Arduino.hardware that referenced this pull request Apr 10, 2018
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

arduino/Arduino#2229
arduino/Arduino#1919
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API feature request A request to make an enhancement (not a bug fix)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

atexit() called after setup()
3 participants