Please tell me how to deal with errors when linking static libraries

We are developing in mbed of ARM.
NUCLEO-F446RE board.

In mbed, the method of converting functions (archive files, static libraries) is
don’t understand.

There are 3 files below.

main.cpp


#include “mbed.h”
#include “add.h”

#define WAIT_TIME 1000 //msec

DigitalOut led1(LED1);
Serial pc(USBTX, USBRX); // USBシリアルポートのインスタンス
int a,b,c,d;

int main()
{
a = 100;
b = 200;
while (true)
{
led1 = !led1;
thread_sleep_for(WAIT_TIME);
d = add(a,b);
pc.printf(“a + b =%d \n”,d);
a = a + 1;
}
}


add.h


int add(int x,int y);


add.cpp


int add(int x,int y){
int z;
z = x+ y;
return z;
}


Convert add.cpp into .a (archive file, static library) among these three files.

arm-none-eabi-g++ -c add.cpp -o add.o
arm-none-eabi-ar rcs add.a add.o
cp add.a add.ar

Copy the generated add.ar to Mbed Studio.
When I compile using this, the following error is output.


Building project 20230807_func_Test (NUCLEO_F446RE, ARMC6)
Scan: 20230807_func_Test
Link: 20230807_func_Test
[Warning] @0,0: L3912W: Option ‘legacyalign’ is deprecated.
[Error] @0,0: L6366E: ./add.ar(add.o) attributes are not compatible with the provided attributes .
[Error] @0,0: L6242E: Cannot link object ./add.ar(add.o) as its attributes are incompatible with the image attributes.
Warning: L3912W: Option ‘legacyalign’ is deprecated.
Error: L6366E: ./add.ar(add.o) attributes are not compatible with the provided attributes .
Object ./add.ar(add.o) contains Build Attributes that are incompatible with the provided attributes.
Tag_CPU_arch = ARM v4T (=2)
Tag_THUMB_ISA_use = Thumb instructions were permitted to be used (=1)
Tag_ARM_ISA_use = ARM instructions were permitted to be used (=1)
Tag_FP_arch = No use of FP hardware was permitted (=0)
Tag_ABI_HardFP_use = Permitted VFP use is implied by Tag_FP_arch (=0)
Tag_FP_HP_extension = Use of the optional half-precision extension to VFPv3/Advanced SIMDv1 was permitted (=1)
Error: L6242E: Cannot link object ./add.ar(add.o) as its attributes are incompatible with the image attributes.
… wchart-16 clashes with wchart-32.
… arm-isa clashes with m-profile.
Not enough information to list the image map.
Finished: 10 information, 1 warning and 2 error messages.
[ERROR] Warning: L3912W: Option ‘legacyalign’ is deprecated.
Error: L6366E: ./add.ar(add.o) attributes are not compatible with the provided attributes .
Object ./add.ar(add.o) contains Build Attributes that are incompatible with the provided attributes.
Tag_CPU_arch = ARM v4T (=2)
Tag_THUMB_ISA_use = Thumb instructions were permitted to be used (=1)
Tag_ARM_ISA_use = ARM instructions were permitted to be used (=1)
Tag_FP_arch = No use of FP hardware was permitted (=0)
Tag_ABI_HardFP_use = Permitted VFP use is implied by Tag_FP_arch (=0)
Tag_FP_HP_extension = Use of the optional half-precision extension to VFPv3/Advanced SIMDv1 was permitted (=1)
Error: L6242E: Cannot link object ./add.ar(add.o) as its attributes are incompatible with the image attributes.
… wchart-16 clashes with wchart-32.
… arm-isa clashes with m-profile.
Not enough information to list the image map.
Finished: 10 information, 1 warning and 2 error messages.


I think the compiler is complaining about the fact that the .a file was not built for the correct core/architecture. With arm-none-eabi-g++, you need to specify the correct options to tell it what core to build for. These options can be found in the Mbed OS build files, e.g. here for Cortex-M4F.

So, I think that if you did something like

arm-none-eabi-g++ -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp

that should set the right core options.

The other issue, though, is that you’re trying to link together code built by two different compilers: Mbed Studio’s ARM compiler, and arm-none-eabi-gcc. I am unsure if this will work properly. If it does not, you can build Mbed with ARM GCC instead if you use Mbed CLI.

By the way, may I ask, for what reason do you need to build a separate .a library and then link it in with the Mbed project?

Thanks for your advice.
I tried the following.

arm-none-eabi-g++ -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -c add.cpp -o add.o
arm-none-eabi-ar rcs add.a add.o
cp add.a add.ar

When compiling with Mbed Studio using this, the following error was output.


Building project 20230807_func_Test (NUCLEO_F446RE, ARMC6)
Scan: 20230807_func_Test
Link: 20230807_func_Test
[Warning] @0,0: L3912W: Option ‘legacyalign’ is deprecated.
[Error] @0,0: L6242E: Cannot link object ./add.ar(add.o) as its attributes are incompatible with the image attributes.
Warning: L3912W: Option ‘legacyalign’ is deprecated.
Error: L6242E: Cannot link object ./add.ar(add.o) as its attributes are incompatible with the image attributes.
… wchart-16 clashes with wchart-32.
Not enough information to list the image map.
Finished: 2 information, 1 warning and 1 error messages.
[ERROR] Warning: L3912W: Option ‘legacyalign’ is deprecated.
Error: L6242E: Cannot link object ./add.ar(add.o) as its attributes are incompatible with the image attributes.
… wchart-16 clashes with wchart-32.
Not enough information to list the image map.
Finished: 2 information, 1 warning and 1 error messages.


It may not be possible to link with the following two different compilers.
・Mbed Studio
・arm-none-eabi-gcc

How can I create a static library (.a) file using Mbed CLI?
The reason I want to create a .a file is anonymization.

We apologize for the inconvenience, but thank you for your support.

In order to use Mbed CLI with your project, you can follow the guide here. Just make sure to use the GCC_ARM toolchain so that it’s compatible with your .a file.

p.s. If you instead want to set up your project using standard CMake instead of Mbed’s custom build scripts, I help maintain the Mbed CE fork which allows you yo do exactly that. You can even create your .a library as part of the build by making a STATIC library target, ensuring it will be built with the right flags!

Thank you for your comment.
The development environment for Mbed CLI has been prepared.
As shown below, it was possible to compile with one file.
It is also possible to write to the board.


#include “mbed.h”
#include

#define WAIT_TIME_MS 500
DigitalOut led1(LED1);

int main()
{
printf(“This is the bare metal blinky example running on Mbed OS %d.%d.%d.\n”, MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);

while (true)
{
    led1 = !led1;
    thread_sleep_for(WAIT_TIME_MS);
}

}


The executed command is as follows.
mbed import mbed-os-example-blinky
cd mbed-os-example-blinky
mbed compile -m NUCLEO_F446RE -t GCC_ARM

The message during compilation is shown below.


PS C:\mbed-cli\mbed-os-example-blinky> mbed compile -m NUCLEO_F446RE -t GCC_ARM
[mbed] Working path “C:\mbed-cli\mbed-os-example-blinky” (program)
[Warning] @,: Compiler version mismatch: Have 10.3.1; expected version >= 9.0.0 and < 10.0.0
Building project mbed-os-example-blinky (NUCLEO_F446RE, GCC_ARM)
Scan: mbed-os-example-blinky
Compile [100.0%]: main.cpp
Link: mbed-os-example-blinky
Elf2Bin: mbed-os-example-blinky

Module .text .data .bss
[fill] 52(+0) 0(+0) 25(+4)
[lib]\c.a 4992(+0) 2108(+0) 58(+0)
[lib]\gcc.a 772(+0) 0(+0) 0(+0)
[lib]\misc 188(+0) 4(+0) 28(+0)
main.o 64(+16) 0(+0) 28(+28)
mbed-os\cmsis 6610(+0) 168(+0) 5953(+0)
mbed-os\drivers 70(+0) 0(+0) 0(+0)
mbed-os\hal 1014(+0) 4(+0) 58(+0)
mbed-os\platform 4416(+4) 260(+0) 348(+0)
mbed-os\rtos 32(+0) 0(+0) 0(+0)
mbed-os\targets 6962(+0) 8(+0) 598(+0)
Subtotals 25172(+20) 2552(+0) 7096(+32)
Total Static RAM memory (data + bss): 9648(+32) bytes
Total Flash memory (text + data): 27724(+20) bytes

Image: .\BUILD\NUCLEO_F446RE\GCC_ARM\mbed-os-example-blinky.bin


As in this case, an error occurred with the following three files.
main.cpp


#include “mbed.h”
#include “add.h”

#define WAIT_TIME 1000 //msec

DigitalOut led1(LED1);
Serial pc(USBTX, USBRX);
int a,b,c,d;

int main()
{
a = 100;
b = 200;
while (true)
{
led1 = !led1;
thread_sleep_for(WAIT_TIME);
// c = a+b;
d = add(a,b);
pc.printf(“a + b =%d \n”,d);
a = a + 1;
}
}


add.h


int add(int x,int y);


add.cpp


int add(int x,int y){
int z;
z = x+ y;
return z;
}


Error codes are shown below.
“C:\Users\aisin\AppData\Local\Programs\Python\Python311\python.exe” returned error.
Code: 1
Path: “C:\mbed-cli\mbed-os-example-blinky”
Command: “C:\Users\aisin\AppData\Local\Programs\Python\Python311\python.exe -u C:\mbed-cli\mbed-os-example-blinky\mbed-os\tools\make.py -t GCC_ARM -m NUCLEO_F446RE --source . --build .\BUILD\NUCLEO_F446RE\GCC_ARM”
Tip: You could retry the last command with “-v” flag for verbose output


Yours truly

Hmm, I’m not sure I see the actual error message anywhere in what you posted

Sincerest apologies.
Error codes are shown below.


PS C:\mbed-cli\mbed-os-example-blinky> mbed compile -m NUCLEO_F446RE -t GCC_ARM
[mbed] Working path “C:\mbed-cli\mbed-os-example-blinky” (program)
[Warning] @,: Compiler version mismatch: Have 10.3.1; expected version >= 9.0.0 and < 10.0.0
Building project mbed-os-example-blinky (NUCLEO_F446RE, GCC_ARM)
Scan: mbed-os-example-blinky
Exception in thread Thread-2 (_readerthread):
Traceback (most recent call last):
File “C:\Users\aisin\AppData\Local\Programs\Python\Python311\Lib\threading.py”, line 1038, in _bootstrap_inner
self.run()
File “C:\Users\aisin\AppData\Local\Programs\Python\Python311\Lib\threading.py”, line 975, in run
self._target(*self._args, **self._kwargs)
File “C:\Users\aisin\AppData\Local\Programs\Python\Python311\Lib\subprocess.py”, line 1597, in _readerthread
buffer.append(fh.read())
^^^^^^^^^
UnicodeDecodeError: ‘cp932’ codec can’t decode byte 0x83 in position 135: illegal multibyte sequence
Compile [100.0%]: main.cpp
[ERROR] ‘NoneType’ object has no attribute ‘splitlines’
[mbed] ERROR: “C:\Users\aisin\AppData\Local\Programs\Python\Python311\python.exe” returned error.
Code: 1
Path: “C:\mbed-cli\mbed-os-example-blinky”
Command: “C:\Users\aisin\AppData\Local\Programs\Python\Python311\python.exe -u C:\mbed-cli\mbed-os-example-blinky\mbed-os\tools\make.py -t GCC_ARM -m NUCLEO_F446RE --source . --build .\BUILD\NUCLEO_F446RE\GCC_ARM”
Tip: You could retry the last command with “-v” flag for verbose output


Huh, sounds like maybe an input file has an encoding that Python can’t handle? Might need to debug through the build scripts to find out which file it is…

Hmm, so apparently “cp932” is Microsoft’s Japanese code page. It looks like Python is defaulting to it on your system, but then trying to parse a file with non-cp932 text (maybe Unicode). You might try

set PYTHONIOENCODING=utf-8

or

set PYTHONIOENCODING=ASCII

in your command prompt, before running the Mbed OS build commands.

Thank you for your advice.
By commenting out part of the main.cpp file,
I have verified that it compiles.
thank you very much.


#include “mbed.h”
#include “add.h”

#define WAIT_TIME 1000 //msec

DigitalOut led1(LED1);
//Serial pc(USBTX, USBRX);
int a,b,c,d;

int main()
{
a = 100;
b = 200;
while (true)
{
led1 = !led1;
thread_sleep_for(WAIT_TIME);
// c = a+b;
d = add(a,b);
//pc.printf(“a + b =%d \n”,d);
a = a + 1;
}
}


Compilation results are shown below.


PS C:\mbed-cli\mbed-os-example-blinky> mbed compile -m NUCLEO_F446RE -t GCC_ARM
[mbed] Working path “C:\mbed-cli\mbed-os-example-blinky” (program)
[Warning] @,: Compiler version mismatch: Have 10.3.1; expected version >= 9.0.0 and < 10.0.0
Building project mbed-os-example-blinky (NUCLEO_F446RE, GCC_ARM)
Scan: mbed-os-example-blinky
Link: mbed-os-example-blinky
Elf2Bin: mbed-os-example-blinky

Module .text .data .bss
[fill] 52(+0) 0(+0) 21(+0)
[lib]\c.a 4992(+0) 2108(+0) 58(+0)
[lib]\gcc.a 772(+0) 0(+0) 0(+0)
[lib]\misc 188(+0) 4(+0) 28(+0)
main.o 104(+0) 0(+0) 40(+0)
mbed-os\cmsis 6610(+0) 168(+0) 5953(+0)
mbed-os\drivers 70(+0) 0(+0) 0(+0)
mbed-os\hal 1014(+0) 4(+0) 58(+0)
mbed-os\platform 4416(+0) 260(+0) 348(+0)
mbed-os\rtos 32(+0) 0(+0) 0(+0)
mbed-os\targets 6962(+0) 8(+0) 598(+0)
Subtotals 25212(+0) 2552(+0) 7104(+0)
Total Static RAM memory (data + bss): 9656(+0) bytes
Total Flash memory (text + data): 27764(+0) bytes

Image: .\BUILD\NUCLEO_F446RE\GCC_ARM\mbed-os-example-blinky.bin


in this state
Please teach me how to convert add.cpp to add.a.

Yours truly

^ I think that what you had before should work. If you just drop the .a file in the source directory, it should link it.

I was able to resolve it by running the above command.
Thank you very much for your help.

Yours truly