PlatformIO not building all project library files

Hello, I’m working on a PlatformIO project and the program isn’t compiling any of the State files. It only fully builds when the State classes are commented out. Otherwise, it doesn’t correctly build. I’ve been stuck for quite some time to no avail, any help?

C:.
|   .DS_Store
|   .gitignore
|   LICENSE
|   platformio.ini
|   README.md
|   
+---.pio
+---.vscode
+---include
|       README
|       
+---lib
|   |   README
|   |   
|   +---Elevator
|   |   |   .gitignore
|   |   |   
|   |   +---ElevatorSystem
|   |   |       Elevator.cpp
|   |   |       Elevator.h
|   |   |       
|   |   \---States
|   |           InitialState.cpp
|   |           InitialState.h
|   |           State.h
|   |           tempCodeRunnerFile.h
|   |           
|   \---src
\---src
    |   .DS_Store
    |   
    \---Communication
            comm.py
            main.cpp

Example code

Error:

In function `global constructors keyed to 65535_0_main.cpp.o.1990':
<artificial>:(.text.startup+0xe8): undefined reference to `Elevator::Elevator()'
<artificial>:(.text.startup+0x104): undefined reference to `InitialState::InitialState(Elevator*, unsigned char, unsigned char, unsigned char)'

In function `main':
<artificial>:(.text.startup+0x38c): undefined reference to `Elevator::setDoorStatus(bool)'
<artificial>:(.text.startup+0x398): undefined reference to `Elevator::isDoorStatus()'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\megaatmega2560\firmware.elf] Error 1

This is an unsupported folder structure. All sources files have to beither in Elevator/ or one folder beneath Elevator, say, src/.

EDIT: I see that @maxgerhardt replied while I was busy typing. I obvioulsy take too long… :wink:

I’ve had a play with your example source code in the link given. Unfortunately, a couple of files are missing, however…

If you do a verbose build:

  • Click the PlatformIO “alien” head on the left toolbar
  • Choose your environment, mine is Uno
  • Click Advanced to open the list
  • Click Verbose Build

(I’m assuming you use VSCode of course. On the command line it’s pio run -v.) You’ll get something like:

Building in release mode
avr-g++ -o .pio/build/uno/src/main.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive 
-std=gnu++11 -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega328p 
-DPLATFORMIO=50101 -DARDUINO_AVR_UNO -DF_CPU=16000000L 
-DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc 
-I/home/norman/.platformio/packages/framework-arduino-avr/cores/arduino 
-I/home/norman/.platformio/packages/framework-arduino-avr/variants/standard src/main.cpp
...

From that command line, you will see that header files are only looked for in -Iinclude -Isrc – so all your “library” files are not being properly found.

You can, if you wish, add a few extra includes to the platformio.ini file to make is scan for your headers and source files. See here for details. However, your “libraries” are not actually libraries, they are part of your source, so ideally, I’d be putting your State and Elevator files (*.h, *.cpp) into the src folder. You can, if desired, put the *.h files into include – I prefer not to myself, but your mileage may vary. In this case, I took the plunge!

So I then reorganised your code as per my suggestions above, plus changed main.cpp, Elevator.h and InitialState.h to remove any foldr names from the various #include statements. Just use the file names.

I also added a semi-colon after the closing brace for your Elevator class, and removed the ‘…’ to keep the compiler happy. (How many times I forget that final semi-colon…:wink:) Now the structure looks like this:

Elevator
├── include
│   ├── Elevator.h
│   ├── InitialState.h
│   ├── README
│   └── State.h
├── lib
│   └── README
├── platformio.ini
├── src
│   └── main.cpp
└── test
    └── README

You still need some cpp files in the src folder of course, but at least now it compiles. The linker gives errors too, but that’s to be expected.

By the way, is this for an Arduino? If so, you shouldn’t be using a main() function, just setup() and loop(). The linker complains otherwise. You have #include "Arduino.h" in Elevator.h and State.h and removing those two lines does cause problems, because you are no longer able to define a data type of String.

Ok, you can use a main() function, but if you do, you don;t get any of the Arduino stuff in the background, so forget about digitalRead() etc - they no longer exist, you are on your own! :frowning_face:

FYI: When you want to keep the formatting for source files, directory structures (as per your post above) please wrap the text in three (or more) back ticks, not single quotes. Like this:

```
This is some
     formatted
     code
```

That way, it’s easier to scan plus, some characters in the plain text escape other characters, so Windows directory paths, for example, get a bit weird with missing bits all over the place – it’s the back slash that does it. Thanks.

HTH

Cheers,
Norm.

Hi!! I figured it out, i had to delete the elevator systems folder and have two seperate independent folders (Elevator and State) in lib because it was incorrectly formatted. Thanks!