libstdc++.so: error adding symbols: File in wrong format

Jinho Yoo picture Jinho Yoo · Jan 15, 2015 · Viewed 27k times · Source

I'm trying to build my own library. It worked on x86 linux so I wanna build for MIPS Linux (Little endian.)

I'm using sourcery codebench in Mento Graphics and buildroot and CMake.

I configured build_all.sh like below.

#!/bin/bash -ev
export TARGETROOT="/usr/mipsel-buildroot-linux-gnu/sysroot"

mkdir -p mips_build
cd mips_build

cmake  -DCMAKE_SYSTEM_NAME="Linux" \
    -DCMAKE_C_COMPILER="${CROSS_COMPILE}gcc" \
    -DCMAKE_CXX_COMPILER="${CROSS_COMPILE}g++" \
    -DCMAKE_AR="${CROSS_COMPILE}ar" \
    -DCMAKE_C_FLAGS="-EL -c -g  -O2 -fPIC --sysroot=$TARGETROOT "  \
    -DCMAKE_CXX_FLAGS="-EL  -c -g  -O2 -fPIC --sysroot=$TARGETROOT " \
    ../

make

cd .. 

Where $CROSS_COMPILE=/home/vagrant/bd1/mips-2014.05/bin/mips-linux-gnu-

And CMakeFiles.txt is like below.

make_minimum_required (VERSION 2.6)

set(EMSG_INCLUDE_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/../../src/eagle_msg/include )
set(EMSG_LIB_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/../../lib )

set (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set (PROJECT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set (PROJECT_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib)


set(LIBRARIES  
    libemsg.a 
    libzmq.a  
    libprotobuf.a
    libprotobuf-c.a
    libpthread.a
    libstdc++.a
     )

#For controller : Client 
SET(EXECUTABLE test_controller)
project (${EXECUTABLE})
include_directories(
        ${PROJECT_INCLUDE_DIR}
        ${EMSG_INCLUDE_DIR}
        $ENV{TARGETROOT}/usr/include
)

link_directories( 
    ${PROJECT_LIB_DIR} 
    ${EMSG_LIB_DIR}
    $ENV{TARGETROOT}/usr/lib

)

set(SRCS 
    test_controller.cpp
)
add_executable( ${EXECUTABLE}  ${SRCS})
target_link_libraries( ${EXECUTABLE} ${LIBRARIES} )

Then it makes the error like below.

[ 40%] Built target emsg
Linking CXX executable ../../../bin/test_controller
/usr/mipsel-buildroot-linux-gnu/sysroot/usr/lib/libstdc++.so: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
make[2]: *** [../bin/test_controller] Error 1
make[1]: *** [test/emsg_test/CMakeFiles/test_controller.dir/all] Error 2

So I checked the format of libstdc++.so. Then it's ELF 32-bit LSB shared object, MIPS, MIPS32. That's the right version. Then what can I do to solve?

/usr/mipsel-buildroot-linux-gnu/sysroot# file /usr/mipsel-buildroot-linux-gnu/sysroot/usr/lib/libstdc++.* 
/usr/mipsel-buildroot-linux-gnu/sysroot/usr/lib/libstdc++.a:         current ar archive
/usr/mipsel-buildroot-linux-gnu/sysroot/usr/lib/libstdc++.so:        symbolic link to `libstdc++.so.6.0.19'
/usr/mipsel-buildroot-linux-gnu/sysroot/usr/lib/libstdc++.so.6:      symbolic link to `libstdc++.so.6.0.19'
/usr/mipsel-buildroot-linux-gnu/sysroot/usr/lib/libstdc++.so.6.0.19: ELF 32-bit LSB shared object, MIPS, MIPS32 rel2 version 1 (SYSV), dynamically linked, with unknown capability 0xf41 = 0x756e6700, not stripped

New information

It seems to be the problem of buildroot. I checked the sysroot value of mips-linux-gnu-gcc.

This is the result before installing buildroot.

$ mips-linux-gnu-gcc --print-sysroot
/home/vagrant/bd1/mips-2014.05/bin/../mips-linux-gnu/libc

This is the result after installing buildroot.

$ mips-linux-gnu-gcc --print-sysroot
/usr/usr/mipsel-buildroot-linux-gnu/sysroot/soft-float/el

I also found the post about similar problem. But it's old issue.

Answer

Jinho Yoo picture Jinho Yoo · Jan 16, 2015

I found the reason. The main reason is sysroot path. Buildroot organizes all toolchain into $BUILDROOT/output/host/. So you should change the PATH environment like below.

HOST_BINARY="$BUILDROOT/output/host/usr/bin"
PATH="${PATH}:${HOST_BINARY}" 

Where $BUILDROOT is the folder where buildroot is extracted.

You should use toolchain below $BUILDROOT/output/host/usr/bin.