2020/9/30

CMake cross-build -- served

用 CMake 來做 cross-build
-- 其實是因為大多使用 CMake 的 project 都是 host - target 同一系統,所以 detect, config 都是自動。但是現在要 build for target,就要讓 CMake 可以做 cross build.

看起來,是要用 -DCMAKE_TOOLCHAIN_FILE=maytoolchain.cmake
然後寫 mytoolchain.cmake
裡面指定toolchain path, name 和一堆 flag, library 等...

然後goole CMAKE_TOOLCHAIN_FILE 可以找一堆 sample,,



cross build served..

準備好 toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(TOOLCHAIN_PATH /usr/local/linaro-aarch64-2018.08-gcc8.2)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/bin/aarch64-linux-gnu-g++)

run cmake....
served.build$ cmake -DCMAKE_TOOLCHAIN_FILE=~/toolchain.cmake ../served
Error:
CMake Error at /usr/share/cmake-3.10/Modules/FindBoost.cmake:1947 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.65.1

  Boost include path: /usr/include

  Could not find the following Boost libraries:

          boost_system

  No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
  directory containing Boost libraries or BOOST_ROOT to the location of
  Boost.

in case cross-build boost: .. 結果沒用到,target system 的 boost 1.66.0 也可以用。所以不用 port boost

依照Error message,把 target system 的 boost package folder copy 到/tmp
要安排一下,把lib 和 header(include) 依照下面方式放好..
/tmp/boost/include/boost
/tmp/boost/lib
並且把 修改 toolchain.cmake,加入
set(BOOST_ROOT /tmp/boost)
一樣..
served.build$ cmake -DCMAKE_TOOLCHAIN_FILE=~/toolchain.cmake ../served
-- The C compiler identification is GNU 8.2.1
-- The CXX compiler identification is GNU 8.2.1
-- Check for working C compiler: /usr/local/linaro-aarch64-2018.08-gcc8.2/bin/aarch64-linux-gnu-gcc
-- Check for working C compiler: /usr/local/linaro-aarch64-2018.08-gcc8.2/bin/aarch64-linux-gnu-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/local/linaro-aarch64-2018.08-gcc8.2/bin/aarch64-linux-gnu-g++
-- Check for working CXX compiler: /usr/local/linaro-aarch64-2018.08-gcc8.2/bin/aarch64-linux-gnu-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at /usr/share/cmake-3.10/Modules/FindBoost.cmake:801 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  /usr/share/cmake-3.10/Modules/FindBoost.cmake:907 (_Boost_COMPONENT_DEPENDENCIES)
  /usr/share/cmake-3.10/Modules/FindBoost.cmake:1558 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:71 (FIND_PACKAGE)


-- Boost version: 1.66.0
-- Found the following Boost libraries:
--   system
-- Could NOT find RAGEL (missing: RAGEL_EXECUTABLE) 
-- Could NOT find RAGEL (missing: RAGEL_EXECUTABLE) 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Performing Test COMPILER_SUPPORTS_CXX11
-- Performing Test COMPILER_SUPPORTS_CXX11 - Success
-- Performing Test COMPILER_SUPPORTS_CXX0X
-- Performing Test COMPILER_SUPPORTS_CXX0X - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /home/charles-chang/githubgitlab/served.build
成功了...

到 served/bin/ 下 check file ,都是 ARM aarch64 ELF 沒錯。

放到 target board 上 run ..
  • 把 served/lib/ 下的so copy 到 /lib/ 下
  • 有用到 boost_system.so,所以要記得把 target build system 的 so 也 copy 到 /lib/ 下

RESTful server (library) in C++

直接link 到 main program 好像比較方便,不用再透過 interface call programm..
所以找 C++ library


有附example 和用 curl 的測試方法。
依照說明clone 下來,用 cmake 產生 Makefile 後 make 舊可以。
在 clone 的folder 下的bin 會有所有 example 的 執行檔,可以一一執行。
同時會印出 用 curl 的測試 command

example 的 main( ) 內容也很簡單。

build 好的 library 只有一個。
include 檔整個和 source code 合在一起。

剩下的問題就是 cmake 系統 porting 到 aarch64 的問題。




這個應該是最有名的,但是 example 看起來好像比剛剛的 served 複雜一些。


其他還有 microsoft 的 c++ rest api,這個 example 看起來更複雜:


上面的 library 比較簡單,所以沒有提供 authentication (login) 功能。
要有 login 的話,要用...

這個就有 authentication,同時支援 http 和 https,但是 library 比較大,使用起來也比較複雜。



這個也有支援 basic 跟 token authentication.
也有 https
也是依照callback, url hanelder 寫程式。


其他

cuda in docker

因為這一篇 說,照一般安裝也可以用。
當然,他提供了三種方法。
所以想試試看直接安裝的方法。
--- 上次在docker 中使用 nvidia ffmpeg,是用 nvidia 的docker。

follow 這一篇,無腦的裝了。

test 有 Error, 要 follow 這一篇 加上 nvidia 的 source list
才能裝
$ sudo apt install nvidia-container-toolkit
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libnvidia-container-tools libnvidia-container1
The following NEW packages will be installed:
  libnvidia-container-tools libnvidia-container1 nvidia-container-toolkit
0 upgraded, 3 newly installed, 0 to remove and 51 not upgraded.
Need to get 850 kB of archives.
After this operation, 2,623 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
重新啟動 docker
systemctl restart docker
然後在 container 舊可以正確run nvidia-smi
因為 host 是 10.2:
~/gpuindocker$ cat Dockerfile 
FROM nvidia/cuda:10.2-base
CMD nvidia-smi

build image and run
~/gpuindocker$ docker build . -t nvidia-test

~/gpuindocker$ docker run --gpus all nvidia-test
Thu Oct 29 08:07:02 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.100      Driver Version: 440.100      CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  TITAN RTX           Off  | 00000000:01:00.0 Off |                  N/A |
| 30%   46C    P0    31W / 280W |      0MiB / 24218MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
這樣在container 中也能正確 access gpu

原來是docker 19.03 版之後,可以不用 nvidia-docker2 來 access gpu
可以直接支援 gpu


2020/9/28

memo .. about nmea parsing..

stringstream 的特性..
先用 getline( whole, sub, ',') 取出以 ',' 區分的字串。
然後用 sub >> float type 直接舊可以轉成 floating type 了。

讀取的部份,還是只能一次一次的,把 char buffer 讀進來,再轉 stringstream。

不行,stringstream getline 雖然會記住processing 的位置,但是 stringstream 本身不會消失,所以一直 append 的話,會越來越大,必須要刪除掉處理過的部份。


ref:

改用 string,find $ 之後找 '\n' 完成一個substring,assign 給stringstream。
然後再reassgin 剩下的substring 給自己。

read : char[]
find : string
parsing : stringstream

bookmarks: veracrypt , open source volume encrypting tool for Linux, android and Windows