4

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.

Jinho Yoo
  • 1,352
  • 5
  • 17
  • 28

2 Answers2

3

Please use the CMake toolchain file provided by Buildroot. It is generated in $(HOST_DIR)/usr/share/buildroot/toolchainfile.cmake. Use it this way:

cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/host/usr/share/buildroot/toolchainfile.cmake

And then you can remove all your other -DCMAKE_ variable, since the toolchain file indicates to CMake which compiler to use, which flags, etc.

Thomas Petazzoni
  • 5,636
  • 17
  • 25
  • I found toolchainfile.cmake. I tested it but it didn't work. My cmake version is 2.8.0. It seems to be the problem of Buildroot's script problem. ------------------------------- > string sub-command REPLACE requires at least four arguments. >Call Stack (most recent call first): > /usr/share/cmake-2.8/Modules/CMakeDetermineSystem.cmake:86 > (INCLUDE) – Jinho Yoo Jan 17 '15 at 14:33
  • Please report a bug in the Buildroot bug tracker. The toolchainfile.cmake should work, and has been tested several times already. So please submit a bug report with the detailed steps to reproduce your issue. Complaining on Stack Overflow will not get the bug fixed. – Thomas Petazzoni Jan 27 '15 at 12:56
1

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jinho Yoo
  • 1,352
  • 5
  • 17
  • 28
  • Hi @Jinho Yoo, even I am facing the above error as mentioned in post. But I have a question here. Should I include `/output/host/aarch64-buildroot-linux-gnu/sysroot/usr/bin` this or `/output/host/usr/bin` in PATH? Because sysroot created by Buildroot is present in `/output/host/aarch64-buildroot-linux-gnu/sysroot/`. Can you please let me know what is the correct dir here? – Preeti Mar 30 '22 at 21:40
  • @Preeti I am getting the same error for the aarch64 toolchain. Are you able to resolve this issue? – beparas Sep 30 '22 at 09:55