查看: 4136|回复: 0

OpenWRT对C++11的支持

[复制链接]
  • TA的每日心情
    奋斗
    2016-8-15 09:28
  • 签到天数: 222 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2017-7-13 09:47:21 | 显示全部楼层 |阅读模式
    分享到:
    1. 检查gcc版本 据说,gcc在4.8版本之后就支持c++11了。我们先检查一下交叉编译器的版本。
    1. $ cd SDK    #进入OpenWrt的SDK路径
    2. $ cd ./staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/
    3. $ ./mips-openwrt-linux-uclibc-gcc --version
    4. mips-openwrt-linux-uclibc-gcc (OpenWrt/Linaro GCC 4.8-2014.04 r45222) 4.8.3
    5. Copyright (C) 2013 Free Software Foundation, Inc.
    6. This is free software; see the source for copying conditions.  There is NO
    7. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    复制代码
    看来是可以支持的,不过我们还是要试了才知道。

    2. 编辑文件
    在 SDK/package/ 下建立 cpp11-demo 目录,并创建以下文件结构:
    1. [SDK]$ tree package/cpp11-demo/
    2. package/cpp11-demo/
    3. |-- Makefile
    4. `-- src
    5.     |-- main.cpp
    6.     `-- Makefile

    7. 1 directory, 3 files
    复制代码
    2.1 src/main.cpp
    1. #include <iostream>
    2. #include <map>

    3. using namespace std;

    4. int main()
    5. {
    6.     cout << "Hello, It's work!" << endl;

    7.     auto i = 1;
    8.     auto d = 0.57;
    9.     auto str = "Hello";

    10.     //-----------------------------------------------
    11.     map<string, int> m {{"a",1}, {"b",2}};
    12.     for (const auto &p : m) {
    13.         cout << p.first << "=" << p.second << endl;
    14.     }

    15.     //-----------------------------------------------
    16.     int count = 0;
    17.     auto print_num = [&count] (int num) {
    18.         cout << "num : " << num << endl;
    19.         count += num;
    20.     };
    21.     print_num(12);
    22.     print_num(32);
    23.     cout << "count=" << count << endl;
    24. }
    复制代码
    在main.cpp里面,我用到了C++11里的新特性:auto, for, lambda。

    2.2 src/Makefile
    1. target:=cpp11-demo
    2. objects=$(subst .cpp,.o,$(wildcard *.cpp))

    3. CXXFLAGS += -std=gnu++11

    4. $(target):$(objects)
    5.     $(CXX) -o $@ $^

    6. clean:
    7.     -@rm $(target)
    8.     -@rm $(objects)
    复制代码
    最近学习了Makefile写法之后,Makefile写起来也简洁了不少。关于Makefile,请访问 [Makefile学习笔记]

    第2行的 objects 为推算出的 main.o 文件。

    其中,CXXFLAGS 指定了 -std=gun++11 或 -std=c++11,表示在编译过程中开启C++11的特性

    2.3 Makefile
    1. include $(TOPDIR)/rules.mk

    2. PKG_NAME:=cpp11-demo
    3. PKG_RELEASE:=1
    4. PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)

    5. include $(INCLUDE_DIR)/package.mk

    6. define Package/cpp11-demo
    7.     SECTION:=utils
    8.     CATEGORY:=Utilites
    9.     TITLE:=$(PKG_NAME)
    10.     DEPENDS:=+libstdcpp
    11. endef

    12. define Build/Prepare
    13.     $(MKDIR) -p $(PKG_BUILD_DIR)
    14.     $(CP) ./src/* $(PKG_BUILD_DIR)
    15. endef

    16. define Package/cpp11-demo/install
    17.     $(INSTALL_DIR) $(1)/usr/bin
    18.     $(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/bin
    19. endef

    20. $(eval $(call BuildPackage,cpp11-demo))
    复制代码
    打包相关的Makefile不需要做特殊修改。

    3.打包安装执行

    3.1 编译打包 与其它没有什么特殊之处,都是 make V=s,正常通过。


    3.2 安装 将生成的ipk文件用scp传送到 OpenWrt目标机上。
    1. [SDK]$ scp bin/ar71xx/packages/base/cpp11-demo_1_ar71xx.ipk root@192.168.1.2:
    2. root@192.168.1.2's password:
    3. cpp11-demo_1_ar71xx.ipk                       100% 3528     3.5KB/s   00:00
    复制代码
    并在目标机上有opkg命令安装
    1. root@OpenWrt:~# opkg install cpp11-demo_1_ar71xx.ipk
    2. Installing cpp11-demo (1) to root...
    3. Collected errors:
    4. * satisfy_dependencies_for: Cannot satisfy the following dependencies for cpp11-demo:
    5. *     libstdcpp *
    6. * opkg_install_cmd: Cannot install package cpp11-demo.
    复制代码
    由于是C++程序,依赖libstdcpp,我们需要前提安装libstdcpp的安装包。
    libstdcpp安装包是在我们编译OpenWrt的trunk路径下。如果我们曾经编译成功了OpenWrt,那么应该会有libstdcpp的安装包。如果不好找,可以用命令搜一下:
    1. $ cd trunk
    2. $ find -name "*.ipk" | grep libstdcpp
    3. ./bin/ar71xx/packages/base/libstdcpp_4.8-linaro-1_ar71xx.ipk
    复制代码
    把它安装到OpenWrt目录机上。

    安装了libstdcpp之后,再安装cpp11-demo就成功了。

    3.3 执行结果
    1. root@OpenWrt:~# cpp11-demo
    2. Hello, It's work!
    3. a=1
    4. b=2
    5. num : 12
    6. num : 32
    7. count=44
    复制代码
    说明程序正常运行!

    总结 能在OpenWrt上用C++11进行开发是一件非常令人兴奋的事!它果然可以。
    不过这不算是OpenWrt支持C++11吧,应该说是gcc支持,与操作系统没太大的关系。



    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-5-20 02:37 , Processed in 0.095532 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.