boost::program_options demo

A Simple Demo

  1. //
  2. // Created by Water on 2018/6/9.
  3. //
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <boost/program_options.hpp>
  8. namespace  bpo = boost::program_options;
  9.  
  10. int main(int argc, char const *argv[])
  11. {
  12.     bpo::options_description opts("all options");
  13.     bpo::variables_map vm;
  14.  
  15.     opts.add_options()
  16.             ("filename", bpo::value<std::string>(), "the file name which want to be found")
  17.             ("help", "this is a program to find a specified file");
  18.  
  19.     try{
  20.         bpo::store(bpo::parse_command_line(argc, argv, opts), vm);
  21.     }
  22.     catch(...){
  23.         std::cout << "undefined option!\n";
  24.         return 0;
  25.     }
  26.  
  27.     if(vm.count("help") ){
  28.         std::cout << opts << std::endl;
  29.     }
  30.     if(vm.count("filename") ){
  31.         std::cout << "find " << vm["filename"].as<std::string>() << std::endl;
  32.     }
  33.     if(vm.empty() ){
  34.         std::cout << "no options found \n";
  35.     }
  36.     return 0;
  37. }

Test

How to Set Default Value

  1. //
  2. // Created by Water on 2018/6/9.
  3. //
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <boost/program_options.hpp>
  9. namespace bpo = boost::program_options;
  10.  
  11. int main(int argc, char const *argv[])
  12. {
  13.     std::vector<std::string> v_para;
  14.     std::string port;
  15.     int baud = 0;
  16.     int timeout = 0;
  17.     bpo::options_description opt("all options");
  18.  
  19.     opt.add_options()
  20.             ("v_para,v", bpo::value<std::vector<std::string> >()->multitoken(), "Serial v_para")
  21.             ("port,p", bpo::value<std::string>(&port)->default_value("COM1"), "Serial Port")
  22.             ("baud,b", bpo::value<int>(&baud)->default_value(115200), "Serial Baud")
  23.             ("timeout,t", bpo::value<int>(&timeout)->default_value(5000), "Timeout")
  24.             ("help", "a demo ( default value )");
  25.  
  26.     bpo::variables_map vm;
  27.  
  28.     try{
  29.         bpo::store(parse_command_line(argc, argv, opt), vm);
  30.     }
  31.     catch(...){
  32.         std::cout << "undefined option!\n";
  33.         return 0;
  34.     }
  35.  
  36.     bpo::notify(vm);
  37.  
  38.     if(vm.count("help") ){
  39.         std::cout << opt << std::endl;
  40.         return 0;
  41.     }
  42.     if(vm.count("v_para") ){
  43.         std::cout << "v_para:";
  44.         for(auto& str : vm["v_para"].as<std::vector<std::string> >() )
  45.             std::cout << str << " ";
  46.         std::cout << std::endl;
  47.     }
  48.     std::cout << "port:" << port << std::endl;
  49.     std::cout << "baud:" << baud << std::endl;
  50.     std::cout << "timeout:" << timeout << std::endl;
  51.     return 0;
  52. }

Test

Apache Axis2/C Installation Guide (Windows)

First

http://archive.apache.org/dist/ws/axis2/c/1_6_0/axis2c-bin-1.6.0-win32.zip
ftp://ftp.zlatkovic.com/libxml/libxml2-2.7.8.win32.zip
ftp://ftp.zlatkovic.com/libxml/iconv-1.9.2.win32.zip
ftp://ftp.zlatkovic.com/libxml/zlib-1.2.5.win32.zip
http://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-openssl-0.9.8y.msi

Install Axis2/C

  1. Extract the binary distribution to a folder of your choice.(example: C:\axis2c-bin-1.6.0-win32)
  2. Copy the iconv.dll, libxml2.dll and zlib1.dll to C:\axis2c-bin-1.6.0-win32\lib
  3. Set the variable AXIS2C_HOME to the deploy folder (C:\axis2c-bin-1.6.0-win32)
  4. Add the path to lib directory to the PATH variable (%AXIS2C_HOME%\lib)

Install Apache 2.2

  1. Copy C:\axis2c-bin-1.6.0-win32\lib\mod_axis2.dll C:\Apache22\modules\mod_axis2.so
  2. Edit the Apache2’s configuration file (generally httpd.conf) and add the following directives
  3. LoadModule axis2_module modules/mod_axis2.so
    Axis2RepoPath C:/axis2c-bin-1.6.0-win32
    Axis2LogFile C:/axis2c-bin-1.6.0-win32/logs
    Axis2MaxLogFileSize 40
    Axis2LogLevel warn
  4. Restart apache2 and test whether mod_axis2 module is loaded by typing the URL http://localhost/axis2/services in your Web browser

QT5 International

Create Project & Generate .ts

  1. lupdate TestInternational.pro -ts zh_CN.ts

Translate with Linguist

Save & Release to Generate .qm

Add Code to Choose Language

  1. #include "widget.h"
  2. #include <QApplication>
  3.  
  4. #include <QTranslator>
  5.  
  6. #include <QDebug>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication a(argc, argv);
  11.  
  12.     QTranslator trans;
  13.      if(trans.load(a.applicationDirPath() + "/../../../zh_CN.qm"))
  14.         a.installTranslator(&trans);
  15.  
  16.     Widget w;
  17.     w.show();
  18.  
  19.     return a.exec();
  20. }

The Result

Google ProtoBuf C++ Example

SerializeToString & SerializeToArray

  1. syntax = "proto3";
  2.  
  3. package test;
  4.  
  5. message People {
  6.   string name = 1;
  7.   int32 age = 2;
  8.   int32 steps = 3;
  9. }
protoc Usage :
  1. water@waters-MacBook-Pro:~/Documents/GitHub/CppQt5/TestProtobuf$ protoc --cpp_out=. people.proto
  2. water@waters-MacBook-Pro:~/Documents/GitHub/CppQt5/TestProtobuf$ ls people.p*
  3. people.pb.cc people.pb.h  people.proto
  1. #include <iostream>
  2.  
  3. #include "people.pb.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     test::People pep;
  8.     pep.set_name("heheda");
  9.     pep.set_age(5);
  10.     pep.set_steps(1304);
  11.  
  12.     std::string buff;
  13.     pep.SerializeToString(&buff);
  14.  
  15.     test::People pep2;
  16.     pep2.ParseFromString(buff);
  17.  
  18.     std::cout << pep2.name() << std::endl;
  19.     std::cout << pep2.age() << std::endl;
  20.     std::cout << pep2.steps() << std::endl;
  21.  
  22.     int data_len = pep.ByteSize();
  23.     unsigned char data[1024] = "\0";
  24.     pep.SerializeToArray(data, data_len);
  25.     for(auto i = 0; i < data_len; i ++)
  26.         printf("%02X ", data[i]);
  27.     printf("\n");
  28.  
  29.     test::People pep3;
  30.     pep3.ParseFromArray(data, data_len);
  31.  
  32.     std::cout << pep3.name() << std::endl;
  33.     std::cout << pep3.age() << std::endl;
  34.     std::cout << pep3.steps() << std::endl;
  35.  
  36.     return 0;
  37. }

How to build Google’s protobuf in MacOS

Build

  1. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ ./autogen.sh
  2. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ ./configure
  3. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ make
  4. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ make install
  5. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ protoc --version
  6. libprotoc 3.5.1

How to build Google’s protobuf in Windows using MinGW

CMAKE Config


mingw32-make & install

  1. c:\protobuf-3.5.1build>mingw32-make
  2. Scanning dependencies of target libprotobuf-lite
  3. [  0%] Building CXX object CMakeFiles/libprotobuf-lite.dir/C_/protobuf-3.5.1/src/google/protobuf/arena.cc.obj
  4. [  1%] Building CXX object CMakeFiles/libprotobuf-lite.dir/C_/protobuf-3.5.1/src/google/protobuf/arenastring.cc.obj
  5. [  1%] Building CXX object CMakeFiles/libprotobuf-lite.dir/C_/protobuf-3.5.1/src/google/protobuf/extension_set.cc.obj
  6. [  2%] Building CXX object CMakeFiles/libprotobuf-lite.dir/C_/protobuf-3.5.1/src/google/protobuf/generated_message_table_driven_lite.cc.obj
  7. ......
  8.  
  9. c:\protobuf-3.5.1build>mingw32-make install
  10. [ 12%] Built target libprotobuf-lite
  11. [ 52%] Built target libprotobuf
  12. [ 52%] Built target js_embed
  13. [ 99%] Built target libprotoc
  14. [100%] Built target protoc
  15. Install the project...
  16. -- Install configuration: ""
  17. -- Installing: C:/protobuf-3.5.1-qt5.9/lib/libprotobuf-lite.a
  18. -- Installing: C:/protobuf-3.5.1-qt5.9/lib/libprotobuf.a
  19. -- Installing: C:/protobuf-3.5.1-qt5.9/lib/libprotoc.a
  20. -- Installing: C:/protobuf-3.5.1-qt5.9/bin/protoc.exe
  21. ......

Cross compile with Node.js C++ Addons to ARM

Set ENV

  1. export HOST=arm-linux-gnueabihf
  2. export CPP="${HOST}-gcc -E"
  3. export STRIP="${HOST}-strip"
  4. export OBJCOPY="${HOST}-objcopy"
  5. export AR="${HOST}-ar"
  6. export RANLIB="${HOST}-ranlib"
  7. export LD="${HOST}-ld"
  8. export OBJDUMP="${HOST}-objdump"
  9. export CC="${HOST}-gcc"
  10. export CXX="${HOST}-g++"
  11. export NM="${HOST}-nm"
  12. export AS="${HOST}-as"
  13. export LD="$CXX"
  14. export LINK="$CXX"
  15. export GYP_DEFINES="armv7=0"

Build

  1. node-gyp --arch arm configure build

Test

  1. root@imx6ul7d:~# node hello.js      
  2. world

TIP 0

  1. {
  2.   "targets": [
  3.     {
  4.       "target_name": "addon",
  5.       "sources": [ "test_api.cc" ],
  6. 	  "include_dirs" : ["sdk/inc"],
  7. 	  "link_settings": {"libraries": [ "-L/mnt/hgfs/tmp0511/sdk/lib"] },
  8. 	  "libraries": [ "-lpos", "-lpthread", "-lrt", "-lpng", "-liconv", "-lfreetype", "-lz" ],
  9.     }
  10.   ]
  11. }

TIP 1

  1. using v8::FunctionCallbackInfo;
  2. using v8::Isolate;
  3. using v8::Local;
  4. using v8::Object;
  5. using v8::String;
  6. using v8::Value;
  7. using v8::Number;
  8. using v8::Exception;
  9.  
  10. void js_Exchange(const FunctionCallbackInfo<Value>& args) {
  11.   Isolate* isolate = args.GetIsolate();
  12.  
  13.   int slot = args[0]->NumberValue();
  14.  
  15.   String::Utf8Value param1(args[1]->ToString());  
  16.   char *szApdu = *param1;  
  17.  
  18.   uint8_t apdu[300] = "\0";
  19.   MyAHex((uint8_t *)szApdu, apdu, strlen(szApdu));
  20.  
  21.   uint8_t rpdu[300] = "\0";
  22.   uint32_t rpdu_len = 0;
  23.   char szResult[300] = "\0";
  24.  
  25.   int ret = Exchange(slot, apdu, strlen(szApdu) / 2, rpdu, &rpdu_len);
  26.   if(0 == ret) {
  27.     MyHexA(rpdu, (uint8_t *)szResult, rpdu_len);
  28.     args.GetReturnValue().Set(String::NewFromUtf8(isolate, szResult));
  29.   } else {
  30.     args.GetReturnValue().Set(String::NewFromUtf8(isolate, ""));
  31.   }
  32. }

Node.js C++ Addons

Install node-gyp

  1. npm install -g node-gyp

Prepare SourceCode

  1. [root@localhost test_nodejs]# ls
  2. binding.gyp  hello.cc  hello.js

hello.cc

  1. #include <node.h>
  2.  
  3. namespace demo {
  4.  
  5. using v8::FunctionCallbackInfo;
  6. using v8::Isolate;
  7. using v8::Local;
  8. using v8::Object;
  9. using v8::String;
  10. using v8::Value;
  11.  
  12. void Method(const FunctionCallbackInfo<Value>& args) {
  13.   Isolate* isolate = args.GetIsolate();
  14.   args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
  15. }
  16.  
  17. void init(Local<Object> exports) {
  18.   NODE_SET_METHOD(exports, "hello", Method);
  19. }
  20.  
  21. NODE_MODULE(NODE_GYP_MODULE_NAME, init)
  22.  
  23. }  // namespace demo

hello.js

  1. // hello.js
  2. const addon = require('./build/Release/addon');
  3.  
  4. console.log(addon.hello());
  5. // Prints: 'world'

binding.gyp

  1. {
  2.   "targets": [
  3.     {
  4.       "target_name": "addon",
  5.       "sources": [ "hello.cc" ]
  6.     }
  7.   ]
  8. }

Compile & Test

  1. [root@localhost test_nodejs]# node-gyp configure
  2. gyp info it worked if it ends with ok
  3. gyp info using node-gyp@3.6.2
  4. gyp info using node@8.11.1 | linux | x64
  5. gyp info spawn /usr/bin/python2
  6. gyp info spawn args [ '/usr/local/node-v8.11.1-linux-x64/lib/node_modules/node-gyp/gyp/gyp_main.py',
  7. gyp info spawn args   'binding.gyp',
  8. gyp info spawn args   '-f',
  9. gyp info spawn args   'make',
  10. gyp info spawn args   '-I',
  11. gyp info spawn args   '/mnt/hgfs/linux_tmp/test_nodejs/build/config.gypi',
  12. gyp info spawn args   '-I',
  13. gyp info spawn args   '/usr/local/node-v8.11.1-linux-x64/lib/node_modules/node-gyp/addon.gypi',
  14. gyp info spawn args   '-I',
  15. gyp info spawn args   '/root/.node-gyp/8.11.1/include/node/common.gypi',
  16. gyp info spawn args   '-Dlibrary=shared_library',
  17. gyp info spawn args   '-Dvisibility=default',
  18. gyp info spawn args   '-Dnode_root_dir=/root/.node-gyp/8.11.1',
  19. gyp info spawn args   '-Dnode_gyp_dir=/usr/local/node-v8.11.1-linux-x64/lib/node_modules/node-gyp',
  20. gyp info spawn args   '-Dnode_lib_file=/root/.node-gyp/8.11.1/<(target_arch)/node.lib',
  21. gyp info spawn args   '-Dmodule_root_dir=/mnt/hgfs/linux_tmp/test_nodejs',
  22. gyp info spawn args   '-Dnode_engine=v8',
  23. gyp info spawn args   '--depth=.',
  24. gyp info spawn args   '--no-parallel',
  25. gyp info spawn args   '--generator-output',
  26. gyp info spawn args   'build',
  27. gyp info spawn args   '-Goutput_dir=.' ]
  28. gyp info ok 
  29. [root@localhost test_nodejs]# 
  30. [root@localhost test_nodejs]# ls
  31. binding.gyp  build  hello.cc  hello.js
  32. [root@localhost test_nodejs]# ls build/
  33. addon.target.mk  binding.Makefile  config.gypi  Makefile
  34.  
  35. [root@localhost test_nodejs]# node-gyp build
  36. gyp info it worked if it ends with ok
  37. gyp info using node-gyp@3.6.2
  38. gyp info using node@8.11.1 | linux | x64
  39. gyp info spawn make
  40. gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  41. make: Entering directory `/mnt/hgfs/linux_tmp/test_nodejs/build'
  42.   CXX(target) Release/obj.target/addon/hello.o
  43.   SOLINK_MODULE(target) Release/obj.target/addon.node
  44.   COPY Release/addon.node
  45. make: Leaving directory `/mnt/hgfs/linux_tmp/test_nodejs/build'
  46. gyp info ok 
  47.  
  48. [root@localhost test_nodejs]# ls build/
  49. addon.target.mk  binding.Makefile  config.gypi  Makefile  Release
  50. [root@localhost test_nodejs]# ls build/Release/
  51. addon.node  obj.target
  52.  
  53. [root@localhost test_nodejs]# node hello.js
  54. world