PlatformIO Community

Error: redefinition of ‘void setUp()’

Hello,

This simple test is not working to me on native environment. It works without setUp mehod but it doesn’t work with it:

#include <unity.h>
 
void setUp() {}
 
void test_environment() { TEST_ASSERT_TRUE(true); }
 
int main(int argc, char **argv) {
  UNITY_BEGIN();
 
  RUN_TEST(test_environment);

  UNITY_END();
}

Output error:

Building...
Building in release mode
g++ -o .pio/build/native/test/Test_main.o -c -DPLATFORMIO=40303 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -Iinclude -Isrc -Ilib/myproject/src -I.pio/build/native/UnityTestLib -I/home/minaya/.platformio/packages/tool-unity test/Test_main.cpp
test/Test_main.cpp: In function ‘void setUp()’:
test/Test_main.cpp:3:6: error: redefinition of ‘void setUp()’
 void setUp() {}
      ^~~~~
In file included from /home/minaya/.platformio/packages/tool-unity/unity.h:44:0,
                 from test/Test_main.cpp:1:
/home/minaya/.platformio/packages/tool-unity/unity_fixture_stubs.h:38:31: note: ‘void setUp()’ previously defined here
     UNITY_WEAK_ATTRIBUTE void setUp(void) { }
                               ^~~~~
*** [.pio/build/native/test/Test_main.o] Error 1

How can I override setUp and tearDown methods?

1 Like

This is an old issue but might be related: https://github.com/ThrowTheSwitch/Unity/issues/138
In short, you might have some includes in your projects source that shouldn’t be there?

Hi joustava,

It is a clean project, it is no code yet, so it doesn’t seem like the same issue.

The other file in the project is only platform.ini:

; [env:nano33ble]
; platform = nordicnrf52
; board = nano33ble
; framework = arduino

[env:native]
platform = native

I’m going to check if there might be a problem with my version of g++ or ask to the unity’s github.

Thank you for your proposal! :slight_smile:

Looking at the error:

In file included from /home/minaya/.platformio/packages/tool-unity/unity.h:44:0,
                 from test/Test_main.cpp:1:
/home/minaya/.platformio/packages/tool-unity/unity_fixture_stubs.h:38:31: note: ‘void setUp()’ previously defined here

… it seems to be a platformio specific change …

image

… but judging from some similar issues on the unity issure tracker talking about the setUp() function being redefined - #417, #422, #423, #438 - adding build_flags -DUNITY_WEAK_ATTRIBUTE to your platformio.ini should fix it.

Hi pfeerick,

Yes, I agree, this is a platformio specific issue.

The real problem is that the compiler doesn’t seem to support weak attributes. I don’t know why, because it should. I tried g++ 7.5.0 and g++ 8.4.0.

Adding the UNITY_WEAK_ATTRIBUTE flag does not work because the functions are defined and cannot be overwritten.

My temporary solution is to modify my local copy of unity_fixture_stub.h by adding a #if !defined(UNITY_NO_WEAK) and add this flag in the build flags.

unity_fixtures_stub.h

#if !defined(UNITY_NO_WEAK)
...
#endif

platformio.ini

[env:native]
platform = native
build_flags = -DUNITY_NO_WEAK

I checked that the tool-unity is outdated from the unity master. Is there anyone who knows where the tool-unity package repository is located to mention this issue and propose workaround?

Thank you for your help!

1 Like

AFAIK, it’s just a package, and is on bintray, so there’s no real repo to lodge issues against. PIO v4.2.0 updated to v2.5.0 of Unity, (and released another version of the package a little later for some reason), so it’s only one minor version point behind atm. So I think the best place to mention it is here, and to tag @ ivankravets with a suggested workaround (minus the space between @ and ivankravets :wink: ).

I should have read that code properly… :face_with_raised_eyebrow: … it probably should have been something more like build_flags -DUNITY_WEAK_ATTRIBUTE="__attribute__((weak))" or whatever pre-clause is needed to make the compiler see reason… or better still… a conditional block around the include for unity_fixture_stubs.h… but at least you’ve gotten something working now.

Nops, I tried -DUNITY_WEAK_ATTRIBUTE="__attribute__((weak))" and "" and "extern" and … :wink: but it didn’t work.

Thank you so much for the explanation about packages and how things works here. I’m newbie on platformio community. I’ll try to find a better solution and then I’ll mention it to ivankravets.

1 Like

Stumbled into the same problem while trying to build library ArduinoFake locally with pio.

test/main.cpp: In function ‘void setUp()’:
test/main.cpp:23:6: error: redefinition of ‘void setUp()’
 void setUp(void)
      ^~~~~
In file included from /home/hst/.platformio/packages/tool-unity/unity.h:44:0,
                 from test/main.cpp:3:
/home/hst/.platformio/packages/tool-unity/unity_fixture_stubs.h:38:31: note: ‘void setUp()’ previously defined here
     UNITY_WEAK_ATTRIBUTE void setUp(void) { }
                               ^~~~~

After some investigation I found a post on stackoverflow which gave me a clue.

IMHO the problem is located in include file unity.h at line 44:

/* PlatformIO: added in order to compile legacy projects without setUp, tearDown functions */
 #include "unity_fixture_stubs.h"

That line is “magically” added to unity-tools inside of platformio universe and is not part of the original unity framework.

After removing that line from unity.h AND adding it to unity.c (both located in my local .platformio folder) everything worked like a charm.

nm shows that setUp and tearDown from unity.o now are weak references as expected

~/source/arduino/foreignlibs/ArduinoFake>[SPI+*] nm --defined .pio/build/native/UnityTestLib/unity.o |grep -e setUp -e tearDown
0000000000000000 W setUp
0000000000000007 W tearDown

whereas setUp in main.o is a strong one

 ~/source/arduino/foreignlibs/ArduinoFake>[SPI+*] nm --defined .pio/build/native/test/main.o |grep -e setUp -e tearDown
    00000000000194a8 T setUp

@ivankravets
Could you please consider to alter unity-tools in that way? Couldn’t find out how the magic works which adds unity_fixture_stubs.h to unity-tools - at least not up to now :wink:

2 Likes

Thank you arduhe!

I confirm your solution works. It is a better solution than the workaround. :slight_smile:

1 Like

This solution is still a work-around, am I wrong? the next time platformIO gets updated those changes will be lost no?

I think you are right - my solution is still only another workaround - the only advantage is that it works at once for all your projects. The older one had to be done locally in each project. And I suspect that my changes will be lost after next update, too.

@ivankravets
Please alter unity-tools distributed by platformio in the way I described

Or tell me where to find the sources - I still wasn’t able to find them :face_with_raised_eyebrow: - for that patched version. I would like to create an appropriate pull request.

Confirmed problem + fix:

  • macOs, latest
  • Fresh install of VS Code
  • Fresh install of platformio: brew install platformio
  • Download samples -> cd platformio-examples-develop/unit-testing/arduino-mock
  • platformio test

test/test_my_service.cpp:8:6: error: redefinition of ‘setUp’
void setUp(void)
^
…/.platformio/packages/tool-unity/unity_fixture_stubs.h:38:31: note: previous definition is here
UNITY_WEAK_ATTRIBUTE void setUp(void) { }

move the line

#include “unity_fixture_stubs.h”

from ~/.platformio/packages/tool-unity/unity.h to unity.c --> works !

Have you, as an alternative, tried setting

lib_archive = no

in the platformio.ini? If the function is weak it should be oerridable in user code if it is correctly linked.

Also, since Unity is a C testing framework, it may help to add extern "C" in fron the void setUp() function implementation to avoid name-mangling.

1 Like

IMHO the origin of the problem is the wrong placement of

/* PlatformIO: added in order to compile legacy projects without setUp, tearDown functions */
#include “unity_fixture_stubs.h”

in unity.h instead inside of unity.c.

The additonal header file unity_fixture_stubs.h has been introduced by platformio. The idea is right and good :+1: but the concret implementation unfortunately not :-1:.

Sorry, but I don’t understand why on the one hand many people should try to circumvent this fault by fiddeling in their projects with various settings or other workarounds - BTW both proposals don’t work, at least not for me :wink: - if on the other hand there is a simple centralized solution which solves the problem for anybody at its origin.

Thanks for pointing this out. Should be fixed in the latest tool-unity package. Please update via pio update --core-packages

3 Likes

Updated to [1.20500.200612 ]. Works as expected. Thx for fixing :+1:

2 Likes