PlatformIO Community

Can't build code for native platform

I’m trying to compile my code for native platform. Unfortunately, I get some linker’s errors.
Code(on branch bug/nativeCompilingError): https://github.com/Michal123456747/Robots-of-education-Robots
edit: I’m working on Macbook M1
image

Compiling on Windows with GCC 10.2.0 gives me

Linking .pio\build\native\program.exe
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Exceptions\Exceptions.o:Exceptions.cpp:(.text+0xee): undefined reference to `IMessage::~IMessage()'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Exceptions\Exceptions.o:Exceptions.cpp:(.text+0x104): undefined reference to `IMessage::~IMessage()'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Exceptions\Exceptions.o:Exceptions.cpp:(.rdata$.refptr._ZN10Exceptions8_messageE[.refptr._ZN10Exceptions8_messageE]+0x0): undefined reference to `Exceptions::_message'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Exceptions\Exceptions.o:Exceptions.cpp:(.rdata$.refptr._ZN10Exceptions12_sendMessageE[.refptr._ZN10Exceptions12_sendMessageE]+0x0): undefined reference to `Exceptions::_sendMessage'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Exceptions\Exceptions.o:Exceptions.cpp:(.rdata$.refptr._ZN10Exceptions15exceptionCalledE[.refptr._ZN10Exceptions15exceptionCalledE]+0x0): undefined reference to `Exceptions::exceptionCalled'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Exceptions\Exceptions.o:Exceptions.cpp:(.rdata$.refptr._ZTV8IMessage[.refptr._ZTV8IMessage]+0x0): undefined reference to `vtable for IMessage'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Message\Message.o:Message.cpp:(.text+0x1e2): undefined reference to `IMessage::~IMessage()'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Message\Message.o:Message.cpp:(.text+0x33d3): undefined reference to `Task::setTask(short)'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\SerialCommunication.o:SerialCommunication.cpp:(.text+0x458): undefined reference to `IMessage::setMessageByJson(char*)'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\SerialCommunication.o:SerialCommunication.cpp:(.text+0x4c7): undefined reference to `IMessage::getCharMessage()'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\SerialCommunication.o:SerialCommunication.cpp:(.text+0x4d4): undefined reference to `IMessage::getCharMessageLength()'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\SerialCommunication.o:SerialCommunication.cpp:(.rdata$.refptr._ZN19SerialCommunication15_throwExceptionE[.refptr._ZN19SerialCommunication15_throwExceptionE]+0x0): undefined reference to `SerialCommunication::_throwException'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\SerialCommunication.o:SerialCommunication.cpp:(.rdata$.refptr._ZN19SerialCommunication7_serialE[.refptr._ZN19SerialCommunication7_serialE]+0x0): undefined reference to `SerialCommunication::_serial'
c:/program files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\native\src\Symbols\SymbolsBase.o:SymbolsBase.cpp:(.rdata$.refptr._ZN11SymbolsBase15_throwExceptionE[.refptr._ZN11SymbolsBase15_throwExceptionE]+0x0): undefined reference to `SymbolsBase::_throwException'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\native\program.exe] Error 1

So, looks like buggy code.

If I look at a specific instance of an error, say

Exceptions.cpp:(.text+0xee): undefined reference to `IMessage::~IMessage()’

I see that

is a non-defined virtual constructor (https://stackoverflow.com/a/3065223/5296568), then lots of class-static variables that were only ever declared but never defined in the C++ classes, and the fact that one function returns IMessage is not good because this pure-virtual type can’t be returned by reference, so I made it a pointer, also multiple functions receive an argument of that kind by-copy when it should be by-reference (C++ "Cannot declare parameter to be of abstract type - Stack Overflow).

See PR https://github.com/Michal123456747/Robots-of-education-Robots/pull/4.

It’s working. Thanks for help. I’m confused, that compilation under Arduino didn’t show these errors. Unfortunately native tests don’t compile :frowning:
The same repo, branch testBug
Errors: Collected 1 itemsProcessing natUnitTest_SymbolsBase in native environment- - Pastebin.com

Edit: Assertions work ok, but I can’t include to unit test, file from project.

Yeah no way that can work. If I compile on Windows I get

In file included from c:\program files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\include\c++\10.2.0\bits\stl_algobase.h:60,
                 from c:\program files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\include\c++\10.2.0\bits\stl_tree.h:63,
                 from c:\program files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\include\c++\10.2.0\map:60,
                 from .pio\libdeps\native\ArduinoFake\src/ArduinoFake.h:7,
                 from test\natUnitTest_SymbolsBase/natUnitTest_SymbolsBase.hpp:1,
                 from test\NativeProcess.hpp:1,
                 from test\test_main.cpp:25:
c:\program files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\include\c++\10.2.0\bits\functexcept.h: In function 'int main()':
c:\program files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\include\c++\10.2.0\bits\functexcept.h:42:1: error: 'namespace' definition is not allowed here

which directly lead me to the above error source.

Moving the #include out of the function gives

natUnitTest_SymbolsBase  native         PASSED    00:00:05.158
=============== 1 succeeded in 00:00:05.158 ===============

Once again thanks. :slight_smile: