Search code examples
c++linkerandroid-sourceandroid.mk

AOSP - error: undefined reference to <function-name> during build


I need another person with good eyes to tell me what I am doing wrong here.

I've had to upgrade the U-boot bootloader on my devices, and naturally I've had to make things fit again. But currently I can't get my AOSP system to build as it used to. I'll start with the error messages and write down my thought process:

target Symbolic: fw_printenv (out/target/product/board/symbols/system/bin/fw_printenv)
target Strip: fw_printenv (out/target/product/board/obj/EXECUTABLES/fw_printenv_intermediates/fw_printenv)
Install: out/target/product/board/system/bin/fw_printenv
target Executable: test_executer (out/target/product/board/obj/EXECUTABLES/test_executer_intermediates/LINKED/test_executer)
external/utils/production/set_display_orientation.cpp:81: error: undefined reference to 'fw_printenv(int, char**, int, env_opts*)'
external/utils/production/set_display_orientation.cpp:57: error: undefined reference to 'fw_setenv(int, char**, env_opts*)'
external/utils/production/set_display_orientation.cpp:65: error: undefined reference to 'fw_printenv(int, char**, int, env_opts*)'
collect2: error: ld returned 1 exit status

So a linker error. And my code cannot find the functions defined in another module. So let's look at the Android.mk file responsible for set_display_orientation.cpp.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := test_executer
LOCAL_SRC_FILES := test_executer.cpp \
        TestSequences.cpp \
        buzzer_test.cpp \
        rtc_test.cpp \
        AudioTests.cpp \
        rs485_test.cpp \
        rs232.cpp \
        set_display_orientation.cpp \
        gpio.cpp \
        gpio_helper.c \
        ping.cpp \
        usb.cpp \
        mmc.cpp \
        display.cpp \
        touchscreen.cpp \
        productionSector.cpp \
        psoc_uart3.cpp \
        ../../tslib/tests/fbutils.c

LOCAL_MODULE_TAGS := optional

LOCAL_STATIC_LIBRARIES += libfw libeeprom libpsoc_helper

LOCAL_SHARED_LIBRARIES += libts libc libcutils

LOCAL_C_INCLUDES += bootable/bootloader/uboot-imx/tools/env \
                    external/tslib \
                    external/tslib/tests \
                    external/tslib/src \
                    external/utils/eeprom \
                    external/utils/PSoCUpdate
ifneq ($(PTEST_VERSION),)
LOCAL_CFLAGS := -DPTEST_VERSION=$(PTEST_VERSION)
endif


LOCAL_CFLAGS += -DUSE_HOSTCC \
                -DANDROID \
                -isystem bootable/bootloader/uboot-imx/include/ \
                -isystem bootable/bootloader/uboot-imx/arch/arm/include/

include $(BUILD_EXECUTABLE)

Now the error messages say that there is an undefined reference to fw_printenv(), fw_setenv(), and fw_printenv(). But these functions are defined in bootable/bootloader/uboot-imx/tools/env which are included in the LOCAL_C_INCLUDES += bootable/bootloader/uboot-imx/tools/env and they are part of LOCAL_STATIC_LIBRARIES += libfw. For the sake of completeness I will also include the Android.mk file responsible for libfw which is part of U-Boot.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := fw_printenv

LOCAL_SRC_FILES :=  fw_env_main.c

LOCAL_C_INCLUDES += fw_env.h
LOCAL_STATIC_LIBRARIES := libfw

LOCAL_CFLAGS := -DUSE_HOSTCC \
                -DANDROID \
                -isystem$(LOCAL_PATH)/../../include/ \
                -isystem$(LOCAL_PATH)/../../arch/arm/include/


include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE:= libfw


LOCAL_SRC_FILES :=  fw_env.c \
                    ctype.c \
                    crc32.c \
                    env_attr.c \
                    env_flags.c \
                    aes.c
#since alot of duplicated Header files exist in uboot-imx/include/ we use -isystem here
#to search for the correct Headers in bionic first
LOCAL_CFLAGS := -DUSE_HOSTCC \
                -DANDROID \
                -isystem$(LOCAL_PATH)/../../include/ \
                -isystem$(LOCAL_PATH)/../../arch/arm/include/

LOCAL_C_INCLUDES += fw_env.h \
                    external/mtd-utils/new-utils/include/

include $(BUILD_STATIC_LIBRARY)

Could someone please point out where I'm going wrong here. I've been through the online documentation (https://developer.android.com/ndk/guides/android_mk), and up and down stackoverflow. I'm really lost on this one.


Solution

  • As can be seen from the Android.mk for libfw, it is a C-library, whereas your code is C++. C and C++ have different ABIs, so the linker is unable to match the functions you call in your C++ code against the functions defined in the C library. To prevent this issue include the headers using:

    extern "C" {
    #include "header_from_libfw.h"
    }
    

    This will instruct the C++ compiler to use the C-ABI for the functions defined in this header, so that they can be matched against the functions defined in the C-library during linking. Specifically, this disables C++-style name-mangling (reference).