Collect2.exe linker error with: undefined reference to third party library function

Hi all,

I am trying to include a third party compiled library file (.a file) into my project, but I am running into a collect2.exe linker error with: undefined reference to third party library function. I am compiling and building using PlatformIO CLI on Windows.

Below is the file structure for the project. Please note I have simplified the structure for illustration.

|--lib
|  |--RTE_OFS
|    |--OFS.a
|    |--OFS.h
|--src
|  |--main.cpp
|--platformio.ini

Where:

//OFS.h
...
void OFS_Main(void);
uint32_t OFS_GetVersion(void);
...

and

//main.cpp
#include <OFS.h>

int main()
{
...
    OFS_Main();
    OFS_GetVersion();
...
}

With platformio.ini file:

[env]
src_filter =
  +<*>
  -<.git/>

[env:release]
platform = nordicnrf52
framework = mbed
board = nrf52840_dk
build_flags = 
  ${env.build_flags}
  -IC:/Users/saaketh.chittajallu/Documents/Repos/Core_Sensors-2/Door/lib/RTE_OFS
  -LC:/Users/saaketh.chittajallu/Documents/Repos/Core_Sensors-2/Door/lib/RTE_OFS
  -l:OFS.a.a

With this structure, i get the following error:

Linking .pio\build\release\firmware.elf
.pio/build/release/src/main.o: In function `main':
C:\Users\saaketh.chittajallu\Documents\Repos\Core_Sensors-2\Door/src/main.cpp:356: undefined reference to `OFS_Main()'
C:\Users\saaketh.chittajallu\Documents\Repos\Core_Sensors-2\Door/src/main.cpp:357: undefined reference to `OFS_GetVersion()'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\release\firmware.elf] Error 1

I am not sure how to proceed with resolving this linker error so any help would be greatly appreciated.

Note: At the moment, getting access to OFS.cpp and compiling the library myself is not an option.

build_flags = 
  ${env.build_flags}
  -Llib/RTE_OFS
  -lOFS

and rename lib/RTE_OFS/OFS.a to lib/RTE_OFS/libOFS.a.

Hi @maxgerhardt, thanks for your quick reply.

I made the changes you recommended but still get the same undefined reference error:

Linking .pio\build\release\firmware.elf
.pio/build/release/src/main.o: In function `main':
C:\Users\saaketh.chittajallu\Documents\Repos\Core_Sensors-2\Door/src/main.cpp:356: undefined reference to `OFS_Main()'
C:\Users\saaketh.chittajallu\Documents\Repos\Core_Sensors-2\Door/src/main.cpp:357: undefined reference to `OFS_GetVersion()'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\release\firmware.elf] Error 1

Any further thoughts?

Is the library written in C? Then try avoiding C++ name mangling.

extern "C" { 
#include <OFS.H>
}

Instead of the old include.

Ahh yes you are correct. I had completely forgotten that the library was written in C. The undefined reference error is resolved now.
Thanks so much!