Objective
In the previous article Bo Master tried to write a simple program in C + + on OpenWrt test, feasible.
The blogger has learned about c++11 two days later, and the new features inside are very exciting. For example, shared_ptr, lambda, auto are very useful features. [Click to learn c++11]
Today, Bo said that the Lord has tried a bit.
Body1. Check the GCC version
GCC is said to have supported C++11 after the 4.8 release. Let's check the version of the cross compiler first.
$ CD SDK #进入OpenWrt的SDK路径 $ CD./staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uclibc-0.9.33.2/bin/$./ MIPS-OPENWRT-LINUX-UCLIBC-GCC--VERSIONMIPS-OPENWRT-LINUX-UCLIBC-GCC (Openwrt/linaro gcc 4.8-2014.04 r45222) 4.8.3Copyright (C) free software Foundation, inc.this are free software; See the source for copying conditions. There is nowarranty; Not even to merchantability or FITNESS for A particular PURPOSE.
It seems to be supportive, but we have to try before we know.
2. Edit the file
Build the Cpp11-demo directory under sdk/package/and create the following file structure:
[sdk]$ Tree package/cpp11-demo/package/cpp11-demo/|--Makefile '--src |--main.cpp '--Makefile1 directory, 3 files
2.1 Src/main.cpp
#include <iostream> #include <map>using namespace std;int main () { cout << "Hello, it ' s work!" << endl; auto i = 1; auto d = 0.57; auto str = "Hello"; //-------- --------------------------------------- map<string, int> m {{"A", 1 }, {"B",2}}; for (const auto &p : m) { cout << p.first << "=" << p.second << endl; } //-------------------- --------------------------- int count = 0; auto print_num = [&count] (Int num) { cout << "num : " < < num << endl; count += num; }; print_num ( print_num); cout << "count=" &NBSP;<<&NBSP;COUNT&NBSP;<<&NBSP;ENDL;}
Inside the main.cpp, I used the new features in the C++11: auto, for, Lambda.
2.2 Src/makefile
target:=cpp11-demoobjects=$ (subst cpp,.o,$ (wildcard *.cpp)) Cxxflags + =-std=gnu++11$ (target): $ (objects) $ (CXX)-O [ Email protected] $^clean: [email protected] $ (target) [email protected] $ (objects)
Recently, after learning makefile, Makefile wrote a lot of concise. For makefile, please visit [Makefile Study notes]
The objects of line 2nd is the inferred MAIN.O file.
Where cxxflags specifies-std=gun++11, which represents the feature that C++11 is turned on during compilation.
2.3 Makefile
Include $ (topdir)/rules.mkpkg_name:=cpp11-demopkg_release:=1pkg_build_dir:=$ (build_dir)/$ (PKG_NAME) include $ ( Include_dir)/package.mkdefine Package/cpp11-demo section:=utils category:=utilites TITLE:=$ (PKG_NAME) DEPENDS: =+libstdcppendefdefine Build/prepare $ (MKDIR)-P $ (pkg_build_dir) $ (CP)./src/* $ (pkg_build_dir) Endefdefine Package/ Cpp11-demo/install $ (Install_dir) $ (1)/usr/bin $ (install_bin) $ (pkg_build_dir)/$ (pkg_name) $ (1)/usr/binendef$ (eval $ (call Buildpackage,cpp11-demo))
Packaging-related makefile do not require special modifications.
3. Package Installation Execution
3.1 Compiling packages
There is nothing special about it, it is make v=s, normal pass.
3.2 Installation
The resulting IPK file is transferred to the OpenWrt target machine using SCP.
[sdk]$ SCP bin/ar71xx/packages/base/cpp11-demo_1_ar71xx.ipk [email protected]:[email protected] ' s password: CPP11-DEMO_1_AR71XX.IPK 100% 3528 3.5kb/s 00:00
and have opkg command installed on the target machine
[Email protected]:~# opkg Install CPP11-DEMO_1_AR71XX.IPK installing Cpp11-demo (1) to root ... Collected errors: * Satisfy_dependencies_for:cannot satisfy the following dependencies for Cpp11-demo: * libstdcpp * * Opkg_install_cmd:cannot Install package Cpp11-demo.
Because it is a C + + program that relies on libstdcpp, we need to install the Libstdcpp installation package as a prerequisite.
The Libstdcpp installation package is under the trunk path where we compiled the OpenWrt. If we have compiled the OpenWrt successfully, then there should be a Libstdcpp installation package. If not, you can search by command:
$ cd trunk$ find-name "*.IPK" | grep LIBSTDCPP./BIN/AR71XX/PACKAGES/BASE/LIBSTDCPP_4.8-LINARO-1_AR71XX.IPK
Install it on the OpenWrt directory machine.
After installing the Libstdcpp, the installation of Cpp11-demo is successful.
3.3 Execution Results
[Email protected]:~# cpp11-demo Hello, It ' s work!a=1b=2num:12num:32count=44
Instructions for the program to run properly!
Summarize
It's very exciting to be able to develop with c++11 on OpenWrt! It sure can.
But this is not openwrt support c++11 bar, it should be said that the GCC support, and the operating system is not much of a relationship.
If you want to learn new technology with me, do not care about me.
Support for the development of OpenWrt--c++11