交叉编译zeromq

一:查看官网,是否支持arm32位,官方没有明确说明支持也没有明确说明不支持。我们暂且当做支持

二:源码zeromq-4.1.6.tar.gz放入虚拟机并解压

1.首先要阅读的就是README,然后看一下INSTALL文件,这个文件里编译源码包的步骤。
2.基本三步走:configure,make,make install。
3../configure配置
   使用./configure -h查看文档。
   对于交叉编译,主要就是有三个参数:--build --host --target
   --build参数是指你编译源码包的系统环境,可以通过查看系统的环境变量来获取,执行set命令可以看到  MACHTYPE=i686-pc-linux-gnu,则--build=i686-pc-linux-gnu 
  --host参数指的是编译出来的工具运行在什么系统上
 --target参数指的是编译出的工具所要操作的系统,譬如说在虚拟机上编译gdb,这个gdb需要查看的是arm开发板的core文件(在虚拟机上查看),这个必须要理解明白,比如说我编译出来的gdb要在我的电脑上运行,但是要调试交叉编译出来的arm指令集的程序,这个时候呢,--target就要是arm-linux,但是--host是i686-pc-linux-gnu。

 

三:配置编译

./configure --build=i686-pc-linux-gnu --host=arm-linux --target=arm-linux --prefix=/opt/arm-zeromq/ --without-libsodium
make  && make  install

编译成功,目标目录为:/opt/arm-zeromq

 

四:编译官网提供的测试代码

 下载服务端service_test.cpp,客户端client_test.cpp,在虚拟机中编译:

在虚拟机中下载官网提供的service和client测试代码,编译:

服务端测试代码为:

#include
#include
#include
#include
#include

int main (void)
{

    //  Socket to talk to clients
    void *context = zmq_ctx_new ();

    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://*:5555");
    //assert (rc == 0);
    if(0 != rc)
    {
    printf("bind error :%d\n",rc);
        return -1;
    }

    while (1) {
        char buffer [10];
        zmq_recv (responder, buffer, 10, 0);
        printf ("Received Hello\n");
        sleep (1);          //  Do some 'work'
        zmq_send (responder, "World", 5, 0);
    }

    return 0;
}
 

客户端测试代码为:

#include
#include
#include
#include

int main (void)
{
    printf ("Connecting to hello world server…\n");
    void *context = zmq_ctx_new ();
    void *requester = zmq_socket (context, ZMQ_REQ);
    zmq_connect (requester, "tcp://localhost:5555");

    int request_nbr;
    for (request_nbr = 0; request_nbr != 10; request_nbr++) {
        char buffer [10];
        printf ("Sending Hello %d…\n", request_nbr);
        zmq_send (requester, "Hello", 5, 0);
        zmq_recv (requester, buffer, 10, 0);
        printf ("Received World %d\n", request_nbr);
    }
    zmq_close (requester);
    zmq_ctx_destroy (context);
    return 0;
}
 

编译:

$CC service_test.cpp -I/opt/arm-zeromq/include -L /opt/arm-zeromq/lib -L /opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/arm-linux-gnueabihf/lib -lzmq -lstdc++ -g -o service_test  (2014版本)
$CC client_test.cpp -I/opt/arm-zeromq/include -L /opt/arm-zeromq/lib -L /opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/arm-linux-gnueabihf/lib -lzmq -lstdc++ -g -o client_test(2014版本

 

五:开发板运行

1.虚拟机上readelf -d libzmq.so查看依赖库

  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [librt.so.1]
 0x00000001 (NEEDED)                     Shared library: [libpthread.so.0]
 0x00000001 (NEEDED)                     Shared library: [libstdc++.so.6]
 0x00000001 (NEEDED)                     Shared library: [libm.so.6]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 0x00000001 (NEEDED)                     Shared library: [libgcc_s.so.1]

0x0000000e (SONAME)                     Library soname: [libzmq.so.5]
 

将交叉编译器/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/arm-linux-gnueabihf中的依赖库拷贝到目标开发板:/opt/depend(为什么要考动态库,是因为开发板的动态库和工具链里的动态库可能小版本不同..防止运行异常...)。

需要拷贝动态库为:

[root@HTNICE dependLib]# ls -al
-rwxrwxrwx    1 root     root       8743098 Apr 27 16:01 libc-2.19-2014.07.so
lrwxrwxrwx    1 root     root            20 Apr 27 16:03 libc.so.6 -> libc-2.19-2014.07.so
-rwxrwxrwx    1 root     root       1435767 Apr 27 16:01 libgcc_s.so.1
-rwxrwxrwx    1 root     root       1500157 Apr 27 16:01 libm-2.19-2014.07.so
lrwxrwxrwx    1 root     root            20 Apr 27 16:04 libm.so.6 -> libm-2.19-2014.07.so
-rwxrwxrwx    1 root     root        846513 Apr 27 16:01 libpthread-2.19-2014.07.so
lrwxrwxrwx    1 root     root            26 Apr 27 16:04 libpthread.so.0 -> libpthread-2.19-2014.07.so
-rwxrwxrwx    1 root     root        171285 Apr 27 16:01 librt-2.19-2014.07.so
lrwxrwxrwx    1 root     root            21 Apr 27 16:04 librt.so.1 -> librt-2.19-2014.07.so
lrwxrwxrwx    1 root     root            19 Apr 27 16:06 libstdc++.so -> libstdc++.so.6.0.20
lrwxrwxrwx    1 root     root            19 Apr 27 16:05 libstdc++.so.6 -> libstdc++.so.6.0.20
-rwxrwxrwx    1 root     root       5409675 Apr 27 16:01 libstdc++.so.6.0.20
lrwxrwxrwx    1 root     root            15 Apr 27 16:08 libzmq.so -> libzmq.so.5.0.2
lrwxrwxrwx    1 root     root            15 Apr 27 16:07 libzmq.so.5 -> libzmq.so.5.0.2
-rwxrwxrwx    1 root     root       4851523 Apr 27 16:06 libzmq.so.5.0.2  //为本次编译成功的libzmq.so

拷贝dependLib文件夹(虚拟机编译环境库)到开发板的opt目录,形成/opt/dependLib

(为什么这么多依赖库,这里因为libzmq.so本身加载还依赖同文件夹下的库文件,所以要设置路径)


vi /etc/profile
加入export LD_LIBRARY_PATH=/opt/dependLib,从新连接shell,直接运行测试代码即可。

执行

./service_test

./client_test

 

服务端:

[root@HTNICE 2014]# ./service_test 
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello

 

[root@HTNICE 2014]# ./client_test 
Connecting to hello world server…
Sending Hello 0…
Received World 0
Sending Hello 1…
Received World 1
Sending Hello 2…
Received World 2
Sending Hello 3…
Received World 3
Sending Hello 4…
Received World 4
Sending Hello 5…
Received World 5
Sending Hello 6…
Received World 6
Sending Hello 7…
Received World 7
Sending Hello 8…
Received World 8
Sending Hello 9…
Received World 9

你可能感兴趣的:(ARM嵌入式开发,交叉编译环境)