Skip to content

Undefined reference when using std::atomic<unsigned> #59

@Chi-Iroh

Description

@Chi-Iroh

Hi, first of all thank you a lot for your amazing work !

Bug Report

I get linker errors when using std::atomic<unsigned> in my code :

/opt/devkitpro/devkitARM/bin/../lib/gcc/arm-none-eabi/14.1.0/../../../../arm-none-eabi/bin/ld: main.o: in function `std::__atomic_base<unsigned int>::fetch_add(unsigned int, std::memory_order)':
/opt/devkitpro/devkitARM/arm-none-eabi/include/c++/14.1.0/bits/atomic_base.h:631:(.text+0x74): undefined reference to `__atomic_fetch_add_4'
/opt/devkitpro/devkitARM/bin/../lib/gcc/arm-none-eabi/14.1.0/../../../../arm-none-eabi/bin/ld: /opt/devkitpro/devkitARM/arm-none-eabi/include/c++/14.1.0/bits/atomic_base.h:631:(.text+0xc8): undefined reference to `__atomic_fetch_add_4'

I'm compiling with -std=gnu++23 flag, but I also got the error with -std=gnu++20 and -std=gnu++17.
I use Arch Linux and I installed devkitpro using pacman. I haven't modified devkitpro.
My environment variables :

DEVKITARM=/opt/devkitpro/devkitARM
DEVKITPPC=/opt/devkitpro/devkitPPC
DEVKITPRO=/opt/devkitpro
PATH=/opt/devkitpro/tools/bin:/opt/devkitpro/portlibs/nds/bin:/opt/devkitpro/devkitARM/arm-none-eabi/bin:/opt/devkitpro/devkitARM/bin:/opt/devkitpro/tools/bin

Output of arm-none-eabi-g++ -v :

Using built-in specs.
COLLECT_GCC=arm-none-eabi-g++
COLLECT_LTO_WRAPPER=/opt/devkitpro/devkitARM/bin/../libexec/gcc/arm-none-eabi/14.1.0/lto-wrapper
Target: arm-none-eabi
Configured with: ../../gcc-14.1.0/configure --enable-languages=c,c++,objc,lto --with-gnu-as --with-gnu-ld --with-gcc --with-march=armv4t --enable-cxx-flags=-ffunction-sections --disable-libstdcxx-verbose --enable-poison-system-directories --enable-interwork --enable-multilib --enable-threads --disable-win32-registry --disable-nls --disable-debug --disable-libmudflap --disable-libssp --disable-libgomp --disable-libstdcxx-pch --enable-libstdcxx-time=yes --enable-libstdcxx-filesystem-ts --target=arm-none-eabi --with-newlib --with-headers=../../newlib-4.4.0.20231231/newlib/libc/include --prefix=/home/davem/projects/devkitpro/tool-packages/devkitARM/src/build/x86_64-linux-gnu/devkitARM --enable-lto --disable-tm-clone-registry --disable-__cxa_atexit --with-bugurl=http://wiki.devkitpro.org/index.php/Bug_Reports --with-pkgversion='devkitARM release 64' --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --with-gmp= --with-mpfr= --with-mpc= --with-isl= --with-zstd= --with-gmp=/opt/devkitpro/crosstools/x86_64-linux-gnu --with-mpfr=/opt/devkitpro/crosstools/x86_64-linux-gnu --with-mpc=/opt/devkitpro/crosstools/x86_64-linux-gnu --with-isl=/opt/devkitpro/crosstools/x86_64-linux-gnu --with-zstd=/opt/devkitpro/crosstools/x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.1.0 (devkitARM release 64)

I only use libnds in my code, no other dependency.

How to fix it

After googling the undefined reference message, I found this github issue and adding this function solves the problem for me :

extern "C" unsigned __atomic_fetch_add_4(volatile void *ptr, unsigned val, int memmodel) {
	(void)memmodel;
	unsigned tmp = *(unsigned*)ptr;
	*(unsigned*)ptr = tmp + val;
	return tmp;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions