Linking error with user defined library

My user defined library is not getting linked, the src files are not being linked with their headers. If you need the repository just ask for it. These is the linking error:

Linking .pio/build/nanorp2040connect/firmware.elf
/Users/francesco/.platformio/packages/toolchain-gccarmnoneeabi/bin/…/lib/gcc/arm-none-eabi/9.2.1/…/…/…/…/arm-none-eabi/bin/ld: .pio/build/nanorp2040connect/src/main.cc.o: in function neopixel_led::palette_crgb::CrgbPalette16::CrgbPalette16(neopixel_led::Crgb const&)': /Users/francesco/Projects/IOT-Led_Lamp/lib/include/palette_crgb.h:33: undefined reference to neopixel_led::uitl_crgb::FillCrgbArray(neopixel_led::Crgb*, unsigned short, neopixel_led::Crgb const&)’
/Users/francesco/.platformio/packages/toolchain-gccarmnoneeabi/bin/…/lib/gcc/arm-none-eabi/9.2.1/…/…/…/…/arm-none-eabi/bin/ld: .pio/build/nanorp2040connect/src/main.cc.o: in function __static_initialization_and_destruction_0(int, int)': /Users/francesco/Projects/IOT-Led_Lamp/src/main.cc:7: undefined reference to neopixel_led::palette_crgb::ColorFromPalette(neopixel_led::palette_crgb::CrgbPalette16 const&, unsigned char, unsigned char, neopixel_led::palette_crgb::BlendType)’
collect2: error: ld returned 1 exit status
*** [.pio/build/nanorp2040connect/firmware.elf] Error 1

I just can’t understand how to use user defined library.

In order to understand better the problem i’ve tried to build a test app:
This is the directory:

The file: test_class.h

#ifndef LIB_TEST_LIB_CLASS_INCLUDE_TEST_ONE_H_
#define LIB_TEST_LIB_CLASS_INCLUDE_TEST_ONE_H_
#include <stdint.h>
#include "test_one.h"

namespace test_lib {
  class TestOne {
    public:
      TestOne();
      ~TestOne();
      void call();
      void kill(uint16_t time);
      uint32_t get_time();
    private:
      uint32_t time = 0;
  };
}
#endif

The file test_class.cc:

#include "test_class.h"


namespace test_lib {

  TestOne::TestOne() {
    time = 0;
  }

  TestOne::~TestOne() {
    time = 0;
  }

  void TestOne::call() {
    test_one_call();
    time++;
  }

  void TestOne::kill(uint16_t time) {
    time = time;
  }

  uint32_t TestOne::get_time() {
    return time;
  }
}

The file test_one.h:

#ifndef LIB_TEST_LIB_INCLUDE_TEST_ONE_H_
#define LIB_TEST_LIB_INCLUDE_TEST_ONE_H_

#include <stdint.h>

namespace test_lib {
  uint32_t time = 0;

  void test_one_call();
  void test_one_kill(uint16_t time);
  uint32_t test_one_get_time();
}

#endif

The file test_one.cc:

#include "test_one.h"

namespace test_lib {
  void test_one_call() {
    time++;
  }

  void test_one_kill(uint16_t time) {
    time = time;
  }

  uint32_t test_one_get_time() {
    return time;
  }
}

The main.cc:

#include <Arduino.h>
#include "test_one.h"
#include "test_class.h"

void setup() {
  test_lib::TestOne test_one;
  test_one.call();
  test_one.kill(10);
  test_one.get_time();

  test_lib::test_one_call();

}

void loop() {
}

I’am hopeless, i’ve been tring to solve this linking problem since i started this project any help is really appreciated. Thank you.

It’s likely due to a typo: uitl_crgb vs util_crgb.

My hypotesis is that i am doing something wrong withe the library structure because after studing the platformIO documentation about user-defined library i could not find anything about multiple files. In anycase this is the directory:

Also if you are so kind on lookup the repo here it is: https://github.com/MRn0b0dy0/Neopixel-led.

Thank you and have a great day.

Please fix the typo in multiple files and try again.

Ok i’ve corrected the typo but i’m still getting a linking error and new error:

src/main.cc:14:39: error: expected constructor, destructor, or type conversion before '(' token
   14 | neopixel_led::util_crgb::FillCrgbArray(leds, 16, color2);
      |                                       ^
*** [.pio/build/nanorp2040connect/src/main.cc.o] Error 1

i also pushed the new commit.

This is not the right folder structure at all. The README of the lib/ folder says you need to create a new library folder in lib/, for example, lib/MyGreateLibrary/<src/, include/>.

Ok but i have multiple file in my real project: https://github.com/MRn0b0dy0/Neopixel-led. but anyway even if a change the test like this its still not working:


Error:

collect2: error: ld returned 1 exit status
*** [.pio/build/nanorp2040connect/firmware.elf] Error 1

I can’t make this think work with multiple file.

That folder structure does look good now. Can you show / upload the exact code for them?

For future reference, that error message is not the one you’d want to show because it says the least. It just says “linking error :(”. The lines above that error are the critcial details on what exactly is wrong.

Ok thanks, all the files are above, but this was just a test. Because i had this same problem on my main repo: GitHub - MRn0b0dy0/Neopixel-led

After I move the files in lib/ into a new folder in the same way shown above and move a function call that was attempted in a global scope rather than inside a functino, it compiles with 0 errors for me.

Maximium Sketch size: 2093056 EEPROM start: 0x101ff000 Filesystem start: 0x101ff000 Filesystem end: 0x101ff000
Checking size .pio\build\nanorp2040connect\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [==        ]  15.4% (used 41500 bytes from 270336 bytes)
Flash: [          ]   0.2% (used 4050 bytes from 2093056 bytes)
Building .pio\build\nanorp2040connect\firmware.bin
======================================================================================= [SUCCESS] Took 4.12 seconds =======================================================================================

Ok so, the fillcrgbarray now works but i still get this linking error:

Linking .pio/build/nanorp2040connect/firmware.elf
/Users/francesco/.platformio/packages/toolchain-gccarmnoneeabi/bin/…/lib/gcc/arm-none-eabi/9.2.1/…/…/…/…/arm-none-eabi/bin/ld: .pio/build/nanorp2040connect/src/main.cc.o: in function setup': main.cc:(.text.setup+0x8): undefined reference to neopixel_led::util_crgb::FillCrgbArray(neopixel_led::Crgb*, unsigned short, neopixel_led::Crgb const&)’
/Users/francesco/.platformio/packages/toolchain-gccarmnoneeabi/bin/…/lib/gcc/arm-none-eabi/9.2.1/…/…/…/…/arm-none-eabi/bin/ld: .pio/build/nanorp2040connect/src/main.cc.o: in function _GLOBAL__sub_I_color': main.cc:(.text.startup._GLOBAL__sub_I_color+0x14): undefined reference to neopixel_led::util_crgb::FillCrgbArray(neopixel_led::Crgb*, unsigned short, neopixel_led::Crgb const&)’
/Users/francesco/.platformio/packages/toolchain-gccarmnoneeabi/bin/…/lib/gcc/arm-none-eabi/9.2.1/…/…/…/…/arm-none-eabi/bin/ld: main.cc:(.text.startup._GLOBAL__sub_I_color+0x20): undefined reference to neopixel_led::palette_crgb::ColorFromPalette(neopixel_led::palette_crgb::CrgbPalette16 const&, unsigned char, unsigned char, neopixel_led::palette_crgb::BlendType)' /Users/francesco/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: main.cc:(.text.startup._GLOBAL__sub_I_color+0x3c): undefined reference to neopixel_led::NeopixelLedStrip::NeopixelLedStrip(unsigned short, unsigned char)’

Can you show the current folder setup? I feel like the .h files are in their right place but the .cpp files are not.

Here it is:

But the library is still not in its own folder?

That’s what it should look like

Ok thank you so much now it works. i’m dumb.

But why it doesn’t work if i use it in a global scope.

Which is correct. It’s a function call, you can’t call it in the global scope. That is, if we’re talking about

neopixel_led::NeopixelLedStrip strip(16, 2);

Yes it was that thank you.