如何使用standalone toolchain移植含STL与线程的程序

I try to build my program without jni ndk-build , but by a standalne arm-kind cross compliers way .
However ,after I write a simple sample , using STL and thread function with my own Makefile , and I do make command, I encount all kind of weird program .

  • error 0: in a dynamic library way
    [dengwei@localhost ~]$ make
    arm-marvell-linux-gnueabi-g++ hi.cpp -o hi1234 -lpthread
    [dengwei@localhost platform-tools]$ adb push ~/hi1234 /data/
    [dengwei@localhost platform-tools]$ adb shell
    # /data/hi1234
    /data/hi1234: not found

    analyse: libpthread.so may not found.

  • error 1: in a static library way
    arm-marvell-linux-gnueabi-g++ -static hi.cpp -o hi1234 -lpthread
    # /data/hi1234
    terminate called after throwing an instance of '__gnu_cxx::__concurrence_broadcast_error' what(): __gnu_cxx::__concurrence_broadcast_error
    [1] Aborted /data/hi1234

    analyse: libpthread.a doesn't may real match and other unknow reason
    some say add "-Wl,--whole-archive -lpthread -Wl,--no-whole-archive -lc " in complie command will fix the problem ,but it does NOT work for me.


I change cross compliers to use standalone cross compliers : ref to $(NDK)/docs/STANDALONE-TOOLCHAIN.html

==============thread succeed this time===============================

$make
arm-linux-androideabi-g++ hi.o -static -L /home/dengwei/standalone-toolchain/sysroot/usr/lib/libthread_db.a -L /home/dengwei/standalone-toolchain/arm-linux-androideabi/lib/libsupc++.a -lstdc++ -DTHREAD -o hi1234

---------------result:------------------------------

# ./hi1234
hello world
thread begin
thread end
#
# cat /mnt/sdcard/345tt abcedfg#


But STL still can NOT work. I found solution here in stackoverflow

"I think "APP_STL:=stlport_static" must be in Application.mk file. Create a "Application.mk" file and write "APP_STL:=stlport_static" in it."

#include <cerrno>
#include <cstddef>
#include <iostream>
using namespace std;
int main(void)
{
    string sss;
    const char *hs= "this is a string";
    sss.assign(hs, strlen(hs));
    printf("%s\n", sss.c_str());
    return 0;
}


[dengwei@localhost jni]$ ndk-build
Compile++ thumb : test-libstl <= test-libstl.cpp
Executable : test-libstl
Install : test-libstl => libs/armeabi/test-libstl

But this sample is still build in JNI ,NOT a standalone version .
I begin to find I did NOT fully unstand configure and Makefile

Here comes the solution:

$export PATH=/home/dengwei/standalone-toolchain/bin:$PATH && export CC=arm-linux-androideabi-gcc && export CXX=arm-linux-androideabi-g++ 
Makefile:
STANDALONE_PATH=/home/dengwei/standalone-toolchain
LDFLAGS= -static 
LIBS+= -L /home/dengwei/standalone-toolchain/sysroot/usr/lib/libthread_db.a
LIBS+= -L /home/dengwei/standalone-toolchain/arm-linux-androideabi/lib/libsupc++.a


INCLUDES= -I $(STANDALONE_PATH)/arm-linux-androideabi/include/c++/4.6/
INCLUDES+= -I $(STANDALONE_PATH)/arm-linux-androideabi/include/c++/4.6/arm-linux-androideabi


CXXFLAGS=  -DTHREAD
CXXFLAGS+=  -DUSE_STL
CXXFLAGS+= $(INCLUDES) 

#CXXFLAGS+= -Wl,--whole-archive -lpthread -Wl,--no-whole-archive -lc

OBJS=hi.o
PROGRAM=hi1234

$(PROGRAM):$(OBJS)
         $(CXX)  $(OBJS) $(LDFLAGS) $(LIBS) $(CXXFLAGS) -o $@
clean:
        rm $(OBJS) -rf
        rm $(PROGRAM) -rf
INCLUDES= -I $(STANDALONE_PATH)/arm-linux-androideabi/include/c++/4.6/
INCLUDES+= -I $(STANDALONE_PATH)/arm-linux-androideabi/include/c++/4.6/arm-linux-androideabi

above are using for find iostream and bit/c++_config.h

LIBS+= -L /home/dengwei/standalone-toolchain/sysroot/usr/lib/libthread_db.a
LIBS+= -L /home/dengwei/standalone-toolchain/arm-linux-androideabi/lib/libsupc++.a

above are using for static complie

==============stl succeed this time===============================

[dengwei@localhost testNSA]$ make
arm-linux-androideabi-g++  hi.o -static  -L /home/dengwei/standalone-toolchain/sysroot/usr/lib/libthread_db.a -L /home/dengwei/standalone-toolchain/arm-linux-androideabi/lib/libsupc++.a -DTHREAD  -DUSE_STL -I /home/dengwei/standalone-toolchain/arm-linux-androideabi/include/c++/4.6/ -I /home/dengwei/standalone-toolchain/arm-linux-androideabi/include/c++/4.6/arm-linux-androideabi  -o hi1234
# /data/hi1234
hello world
this is a string type
thread begin
thread end
#
# cat /mnt/sdcard/345tt

abcedfg#  

 
OK, I'm done here.
So , I need to write articles about configure and Makefile.

 EOF

原文地址:https://www.cnblogs.com/no7dw/p/2704411.html