my aosp project fails to link against opencv4android

73 views
Skip to first unread message

jeremy barrett

unread,
Jun 14, 2012, 12:23:42 PM6/14/12
to
i'm trying to build a sample program that uses the opencv4android libraries and am having trouble linking. i've tried various .mk gymnastics to no avail. now i'm at the stage of trying to simply execute g++ from the command line and the linking is failing in the same way. i'm way confused because the files appear to be in the right place(s) and contain the appropriate symbols, but ld is still barfing.

here's the commandline generated by aosp's build process for the linking step:

$ prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/arm-linux-androideabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/LINKED/cvtest -Lout/target/product/maguro/obj/lib -Wl,-rpath-link=out/target/product/maguro/obj/lib -lc -lstdc++ -lm out/target/product/maguro/obj/lib/crtbegin_dynamic.o     out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/cvtest.o    -Wl,--whole-archive  -Wl,--no-whole-archive   -Wl,-z,noexecstack -Wl,--icf=safe -Wl,--fix-cortex-a8  -Wl,--no-undefined /home/jeremy/src/aosp/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/armv7-a/libgcc.a out/target/product/maguro/obj/lib/crtend_android.o
running this command generates a number of undefined reference errors:
target Executable: cvtest (out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/LINKED/cvtest)
/home/jeremy/src/aosp/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/cvtest.o: in function main:external/pvl/cvtest/cvtest.c:13: error: undefined reference to 'cvCreateImage'
collect2: ld returned 1 exit status
make: *** [out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/LINKED/cvtest] Error 1
make: Leaving directory `/home/jeremy/src/aosp'

you'll notice that the build process did not generate any linker directives related to opencv, which is strange, becuase they are listed in the .mk . here's one example i've tried:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE   := cvtest
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES  := cvtest.c
LOCAL_CFLAGS += -L$(LOCAL_PATH)
LOCAL_CFLAGS += -lopencv_core
LOCAL_C_INCLUDES := "$(LOCAL_PATH)/include/" "$(LOCAL_PATH)/include/opencv"
include $(BUILD_EXECUTABLE)

note that i've copied the relevant header and library files to the same working directory as the project code. so i decided to re-run the g++ command and add some linker flags of my own:

$ prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/arm-linux-androideabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/LINKED/cvtest -Lexternal/pvl/cvtest -Lout/target/product/maguro/obj/lib -Wl,-rpath-link=out/target/product/maguro/obj/lib -lc -lstdc++ -lm -lopencv_core out/target/product/maguro/obj/lib/crtbegin_dynamic.o     out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/cvtest.o    -Wl,--whole-archive -Wl,--no-whole-archive -Wl,-z,noexecstack -Wl,--icf=safe -Wl,--fix-cortex-a8  -Wl,--no-undefined /home/jeremy/src/aosp/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/armv7-a/libgcc.a out/target/product/maguro/obj/lib/crtend_android.o

but this fails in exactly the same way:

$ /home/jeremy/src/aosp/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/cvtest.o: in function main:external/pvl/cvtest/cvtest.c:13: error: undefined reference to 'cvCreateImage'
collect2: ld returned 1 exit status
make: *** [out/target/product/maguro/obj/EXECUTABLES/cvtest_intermediates/LINKED/cvtest] Error 1
make: Leaving directory `/home/jeremy/src/aosp'

it's clear that the directives are being processed, because if i remove the -L, ld complains about not being able to find libopencv_core.a . furthermore, libopencv_core.a does appear to have the necessary symbol:

$ nm libopencv_core.a | grep cvCreateImage
00000001 T cvCreateImage
00000001 T cvCreateImageHeader
         U cvCreateImage

so i'm at a loss right now. anyone have experience with similar linker weirdness? or is there more work that i'm not doing to make the link step work?

many thanks,
jeremy

jeremy barrett

unread,
Jun 15, 2012, 12:16:36 PM6/15/12
to android...@googlegroups.com
it seems that any linker directives automatically search for libraries in

<aosp_dir>/out/target/product/<product_flavor>/obj/STATIC_LIBRARIES/<lib_name>_intermediates

whether using LOCAL_LDLIBS, LOCAL_STATIC_LIBRARIES, etc, etc. 

is there any way to modify the library search path? what is the preferred method for integrating opencv4android with aosp projects? should i download the source distribution and inject into the aosp tree so it's built as part of the global build process. or would it be better (at this stage) to simply create the necessary directories by hand and shuffle the library files around?

incidentally, i'm not having this sort of problem with includes. using LOCAL_C_INCLUDES allows me to modify the header search path and append any arbitrary path -- even paths outside the aosp tree. 

i'm baffled as to why i can't do this for libraries.
Reply all
Reply to author
Forward
0 new messages