gcc  -g -O2 -Werror=implicit-function-declaration -ffile-prefix-map=/builds/debian-phototools-team/aview/debian/output/source_dir=. -fstack-protector-strong -Wformat -Werror=format-security -I/usr/include  -Wl,-z,relro -o aview image.o main.o shrink.o ui.o  -L/usr/lib/i386-linux-gnu -laa
/usr/bin/ld: shrink.o: undefined reference to symbol 'floor@@GLIBC_2.0'
/usr/bin/ld: /lib/i386-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status''

This happens only when compiling under i386 architecture on Debian.   Amd64 architectures is fine.
ChatGPT:
ChatGPT
The issue occurs because the linker (ld) cannot find the floor function, which is defined in the math library (libm). The reason it works on amd64 and not on i386 is likely due to subtle differences in the linker behavior or default options between architectures.

To fix this, ensure that the math library (-lm) is explicitly linked. On some architectures, the linker may not automatically include libm for functions like floor.

Update your compilation command to include -lm at the end of the link line:

bash
￼Code kopieren
gcc -g -O2 -Werror=implicit-function-declaration -ffile-prefix-map=/builds/debian-phototools-team/aview/debian/output/source_dir=. -fstack-protector-strong -Wformat -Werror=format-security -I/usr/include -Wl,-z,relro -o aview image.o main.o shrink.o ui.o -L/usr/lib/i386-linux-gnu -laa -lm
Why this works:
The floor function is part of the math library (libm). On some systems or architectures, linking against libm is not automatic, and you must explicitly include -lm in the linker command.
The order of libraries matters. The -lm option must be specified after the objects (*.o) and any other libraries that depend on it.
After making this change, your program should link correctly on i386.

￼
￼
￼
￼
￼
Du:
Do you see any reason why -lm is missing for i386 but not for amd64?
ChatGPT:
ChatGPT
Yes, the discrepancy between i386 and amd64 architectures in requiring -lm can often be traced to subtle differences in how the linker and compiler handle implicit library dependencies or symbol resolution:

Architecture-Specific Defaults:

On amd64, the linker might automatically pull in libm when it detects usage of math functions like floor. This behavior is not guaranteed across all architectures.
On i386, the linker may not perform this implicit linking due to different ABI conventions or historical reasons.
Toolchain Variances:

The gcc toolchain on amd64 might have been configured differently, possibly with a default linking of libm for certain common math functions. On i386, it may rely more strictly on explicit linking.
Differences in Library Dependencies:

The way libraries are built and packaged on i386 versus amd64 might differ. For example, a library on amd64 might explicitly link libm, satisfying the dependency automatically, while the same library on i386 does not.
Historical Legacy:

i386 has been around longer and sometimes carries legacy configurations. In older systems, it was more common to require explicit -lm. As the ecosystem evolved, amd64 toolchains might have adopted more user-friendly defaults.
Dynamic Linking Behavior:

If some dependencies on amd64 dynamically link against libm, the math symbols may already be resolved during the linking process. On i386, those dependencies might not link against libm, leaving the math symbols unresolved.
Debugging the Root Cause
If you want to pinpoint why the behavior differs:

Check Implicit Dependencies: Run the command gcc -v for both architectures to see the default libraries being linked.
Inspect the Linked Libraries: Use ldd on your built binaries to compare which libraries are being used on amd64 and i386.
Look for libm Usage in Dependencies: Use nm or objdump on .o and .a files to see if any dependency is explicitly or implicitly linking libm.
While this difference in behavior may seem surprising, it's not uncommon for such architecture-specific quirks to arise. Explicitly adding -lm ensures your build process is robust and portable across architectures.
