2024年8月16日 星期五

Ubuntu 20.04 安裝 ASUS TUF Gaming B650m PLUS WiFi 驅動程式

簡單來說

這張板子安裝了Ubuntu 20.04 後,找不到 wifi 選項

幸好插上實體網路線還能用

一般來說就是沒驅動程式

上網 google 這板子用哪個 wifi 晶片還找不到

一直到 ASUS 的驅動程式下載網頁才看到晶片名稱

https://www.asus.com/tw/motherboards-components/motherboards/tuf-gaming/tuf-gaming-b650m-plus-wifi/helpdesk_download?model2Name=TUF-GAMING-B650M-PLUS-WIFI


Realtek RTL8852BE WiFi driver V6001.15.124.0 For Windows 10/11 64-bit.
版本 6001.15.124.0
6.15 MB
2022/09/13

Please install the corresponding driver according to the WIFI/BT chip on your motherboard.

知道晶片名稱再來下關鍵字就簡單多了

找到下面兩篇

https://blog.csdn.net/qq_75220669/article/details/137251306

https://ubuntuforums.org/showthread.php?t=2484689

為避免以後連結失效

這裡將需要的步驟複製貼上

sudo apt update
sudo apt install git make gcc bc

查看核心版本

uname -r

低於 5.18 => https://github.com/HRex39/rtl8852be.git

高於 5.18=> https://github.com/HRex39/rtl8852be.git -b dev


git clone https://github.com/HRex39/rtl8852be.git
cd rtl8852be
make    /*  如果要 multi-thread build 的話,輸入 make -j$(nproc)  */
sudo make install
sudo modprobe 8852be


走完步驟 WiFi 就可以使用了


2024年6月18日 星期二

pyinstaller 打包 mmcv 相關的 exe 可能會遇到的問題

打包完後要執行時遇到了一個問題如下

FileNotFoundError: [Errno 2] No such file or directory: '/home/XXX/tmp-dir/_MEIkGapxs/yapf_third_party/_ylib2to3/Grammar.txt'

上網查到了這篇

https://blog.csdn.net/weixin_44243859/article/details/131890088

為了怕以上連結失效,這邊稍微重複一下內容

說是 pyinstaller 沒有自帶該第三方庫文件的hook的時候,就會導致這個包文件不被打包進來,解決辦法,寫個 hook,然後放進 pyinstaller 的 hooks 裡面,hook 文件的命名規範為: hook-yapf_third_party.py

from PyInstaller.utils.hooks import collect_data_files datas = collect_data_files("yapf_third_party")

以上就是這個 .py 的內容

但具體來說我還是不知道怎麼做

於是再查到了這篇

https://blog.csdn.net/cliffordl/article/details/138065845

看了這篇之後就明白很多

如果我要打包 main.py,然後我要讓他去 hook 一些 library

那我就在 main.py 旁邊開一個 hooks 資料夾

然後把上面的 hook-yapf_third_party.py 丟進這個資料夾

(其中 hook-xxx.py 的 xxx 就是 library 名稱)

然後在打包時加入 --additional-hooks-dir ./hooks

所以整個打包的指令就變成 

pyinstaller -F -c --additional-hooks-dir ./hooks main.py

這個問題這樣就解決了,然後接下來又遇到

ModuleNotFoundError: No module named 'mmcv._ext'

繼續上網查,發現這篇

https://blog.csdn.net/gc5218112/article/details/125172123

OK,他說要在 hiddenimports 加入 'mmcv', 'mmcv._ext'

找到 main.spec,照著加進去,然後執行

pyinstaller main.spec

問題也確實解決了,但這樣很不方便,我希望可以不要去改 .spec

所以這個命令可以寫成

pyinstaller -F -c --additional-hooks-dir ./hooks --hidden-import mmcv --hidden-import mmcv._ext main.py

這樣全部的問題都解決了,大功告成


2024年6月4日 星期二

ubuntu 20.04 安裝 CUDA 11.8 for RTX 4090

 先上個連結

https://gist.github.com/MihailCosmin/affa6b1b71b43787e9228c25fe15aeba?permalink_comment_id=4665431

上面這連結是在講 ubuntu 22.04 怎麼裝 CUDA 11.8

他的最後更新時間是 Oct 29, 2023

而現在是 June 04, 2024

NV Driver 版號又不太一樣了,在這紀錄一下踩坑紀錄

====================先照貼原本的內容====================

#!/bin/bash ### steps #### # verify the system has a cuda-capable gpu # download and install the nvidia cuda toolkit and cudnn # setup environmental variables # verify the installation ### ### to verify your gpu is cuda enable check lspci | grep -i nvidia ### If you have previous installation remove it first. sudo apt purge nvidia* -y sudo apt remove nvidia-* -y sudo rm /etc/apt/sources.list.d/cuda* sudo apt autoremove -y && sudo apt autoclean -y sudo rm -rf /usr/local/cuda* # system update sudo apt update && sudo apt upgrade -y # install other import packages sudo apt install g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev # first get the PPA repository driver sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt update # find recommended driver versions for you ubuntu-drivers devices # install nvidia driver with dependencies sudo apt install libnvidia-common-515 libnvidia-gl-515 nvidia-driver-515 -y # reboot sudo reboot now # verify that the following command works nvidia-smi sudo wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600 sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /" # Update and upgrade sudo apt update && sudo apt upgrade -y # installing CUDA-11.8 sudo apt install cuda-11-8 -y # setup your paths echo 'export PATH=/usr/local/cuda-11.8/bin:$PATH' >> ~/.bashrc echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc source ~/.bashrc sudo ldconfig # install cuDNN v11.8 # First register here: https://developer.nvidia.com/developer-program/signup CUDNN_TAR_FILE="cudnn-linux-x86_64-8.7.0.84_cuda11-archive.tar.xz" sudo wget https://developer.download.nvidia.com/compute/redist/cudnn/v8.7.0/local_installers/11.8/cudnn-linux-x86_64-8.7.0.84_cuda11-archive.tar.xz sudo tar -xvf ${CUDNN_TAR_FILE} sudo mv cudnn-linux-x86_64-8.7.0.84_cuda11-archive cuda # copy the following files into the cuda toolkit directory. sudo cp -P cuda/include/cudnn.h /usr/local/cuda-11.8/include sudo cp -P cuda/lib/libcudnn* /usr/local/cuda-11.8/lib64/ sudo chmod a+r /usr/local/cuda-11.8/lib64/libcudnn* # Finally, to verify the installation, check nvidia-smi nvcc -V # install Pytorch (an open source machine learning framework) pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

====================以下是要更改的內容====================

# install nvidia driver with dependencies sudo apt install libnvidia-common-535 libnvidia-gl-535 nvidia-driver-535 -y

因為 driver 版本更新,這個時間點已經找不到 515 了

所以這邊把 515 改成 535


# verify that the following command works nvidia-smi sudo wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600 sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /"

這邊講的都是 ubuntu 22.04

我要的版本是 20.04,可以去下面這個網址

https://developer.nvidia.com/cuda-11-8-0-download-archive

選擇 Linux -> x86_64 -> Ubuntu -> 20.04 -> deb(network)

他會列出 Installation Instructions 如下

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.0-1_all.debsudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda

注意上面最後這句還有坑

# installing CUDA-11.8 sudo apt install cuda-11-8 -y

如果不指定版本的話他預設裝的版本一直安裝失敗

後來找到有人說其實可以指定 driver 版本

可以寫成下列的樣子

# installing CUDA-11.8 sudo apt install cuda-11-8 cuda-drivers=535.161.08-1

後面的步驟就沒有不同了

照著複製貼上即可成功安裝


2023年5月2日 星期二

Find all Chinese text in a string using Python and Regex

參考網址

https://stackoverflow.com/questions/2718196/find-all-chinese-text-in-a-string-using-python-and-regex


python的字串都是unicode

而全部中文的範圍在0x4e00 ~ 0x9fff之間

所以一個字串你想要取其中的中文可以這樣寫
def get_chinese(self, text):
    result = re.findall(r'[\u4e00-\u9fff]+', text)
    output = "".join(result)
    return output

補充
這一個字串你想寫入cp950編碼的檔案
但有一些字不符合該怎麼做?

with open("cp950.txt", mode="w", encoding="cp950") as f:
    f.write(text)

如果都不處理的話你會得到
UnicodeEncodeError: 'cp950' codec can't encode character '\uff6d' in position 98: illegal multibyte sequence


其實只要像下面這樣做就可以了
encode_text = text.encode("cp950", "ignore")
decode_text = encode_text.decode("cp950", "ignore")
with open("cp950.txt", mode="w", encoding="cp950") as f:
    f.write(decode_text)

2022年12月27日 星期二

How to Fix Apple Magic Mouse 2 scroll does not work on win10, 解決Apple Magic Mouse 2在win10上滾輪不能用

首先,我的 Magic Mouse 2 型號是A1657

如果你有上網搜尋的話

很多人會叫你裝Boot Camp

譬如下面這個連結

https://support.apple.com/kb/DL1836?locale=zh_TW

但我裝了之後發現還是沒用

仔細查之後才發現這裡面的MouseDriver版本是4.0.0.1

所以我們要想辦法拿更新的 Boot Camp 6.x

到這個頁面

https://github.com/timsutton/brigadier/releases

下載他的brigadier.exe

然後打開命令列,cd到這個exe的位置

輸入brigadier.exe -m MacBookPro14,1

然後他就會下載相對應的 Boot Camp

我在這裡面找到新的MouseDriver是6.0.6200

裝了就能用了

你也可以參考下面這個網址

https://www.youtube.com/watch?v=tV5nfQA_8Ec&ab_channel=FilipLiter%C3%A1k









2022年3月28日 星期一

github + sourcetree window10

記錄一下 github 搭配 sourcetree 常遇到的問題






 之前就搞了一次ssh因為某天開始就不支援傳統帳號密碼存取

沒想到最近又來一次

即使是ssh,RSA key也不支援

所以又要重弄一次








首先就去建立一個新的ssh key吧

如上圖,在 sourcetree 裡面就可以

打開後記得下面不能選RSA

如下圖這裡是選ECDSA










按下Generate之後稍微滑動一下滑鼠就可以建立key

Public key就自己找個記事本複製貼上存起來

(我選Save public key存起來的檔案反而不能給github用)

Private key的話他會建議你加個密碼然後選Save private key存起來

然後去github網頁,右上角個人圖示,再選設定









選New ssh key,然後把剛才public key複製貼上後save

再來到電腦右下角找到Pageant






點開後如下圖








點選Add key之後選擇剛才存檔的private key

如果你剛才有設定密碼的話他會要你輸入密碼

到這邊就算是完成了

再來去github網頁選擇你要clone的project






一開始還以為要用ssh那欄,結果要用https的url,sourcetree才會讓我抓

其實這一整套做完也真夠麻煩

好像可以直接用github desktop取代就好了

說不定是github想推他們自家的軟體


2022年3月27日 星期日

打包Container image

 1. 目錄結構


2. 新增Dockerfile

vi Dockerfile

按 i

新增以下內容

FROM centos:centos7.9.2009

COPY ctpn /appc/ctpn/

COPY debug /appc/debug

COPY models /appc/models

COPY yolo /appc/yolo

COPY type_recognizer /appc/

RUN yum -y install libxcb

WORKDIR /appc


3. 打包Image

docker build -t appm3 .

-t    tag,等於替image取個名字,

注意最後面還有一個    " . "  這不能少,應該是表示當前目錄的意思


4. 執行 

docker image ls

docker run -it appm3 bash


5. 在docker中執行httpd

docker run -d -it --privileged {Image ID} /usr/sbin/init

















2022年3月22日 星期二

如何複製資料到container裡面

1. 列出正在運行的container

docker ps

example

[root@DESKTOP-B31T57O dist]# docker ps

CONTAINER ID   IMAGE                   COMMAND   CREATED             STATUS             PORTS     NAMES

ddf6ad58f1ea   centos:centos7.9.2009   "bash"    About an hour ago   Up About an hour             kind_perlman

2.  查詢完整container id

docker inspect -f   '{{.Id}}'  SHORT_CONTAINER_ID-or-CONTAINER_NAME

example

[root@DESKTOP-B31T57O dist]# docker inspect -f   '{{.Id}}' kind_perlman

ddf6ad58f1eab9186e761afac45890bc2543e7a353d7cec3d323560b02d960a0

3. 從host copy 檔案到container

sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE
example
docker cp type_recognizer e0746a7a15e56c9b906919c909d27644d4a7d900e1d8a12478e8c74bf064b89f:/type_recognizer
這樣會copy到container的根目錄下面
如果要copy到container的home下面
docker cp type_recognizer e0746a7a15e56c9b906919c909d27644d4a7d900e1d8a12478e8c74bf064b89f:/home/type_recognizer


4. 直接開一個互相連結的資料夾

docker volume create --name appa

而在host的位置是

/var/lib/docker/volumes/appa/_data/

在啟動時加一個 -v 參數,就可以指定 volume 要跟容器內哪一個資料夾連通

docker run -v appa:/appb -it centos:centos7.9.2009 bash

上面的appa表示在host時的名稱

而/appb表示在container裡面的資料夾路徑

也可以寫成/ox/appb,這樣在根目錄下就會有一個ox資料夾裡面放著appb資料夾

而appb資料夾裡面放的東西就會是在本機appa資料夾裡面的東西









2022年3月17日 星期四

Docker/Container基本操作

 

先切換到 root

1. 安裝 yum-utils

yum install -y yum-utils device-mapper-persistent-data lvm2


2. 設定 repository

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo


3. 安裝 Docker CE

yum install docker-ce

出現error

打開 etc/yum.repos.d/docker-ce.repo

將 $releasever全部改7

例如

baseurl=https://download.docker.com/linux/centos/$releasever/source/stable

改成

baseurl=https://download.docker.com/linux/centos/7/source/stable

重新輸入

yum install docker-ce

檢查docker版本

docker version

4. 啟動 Docker

systemctl start docker

5. 測試 Docker

docker run hello-world

6. 設定開機自動啟動 Docker

systemctl enable docker

停止

systemctl stop docker

重啟

systemctl restart docker

狀態

systemctl status docker


7. 錯誤處理

重開機後遇到

Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running?

解答

You can try out this: 

systemctl start docker

It worked fine for me.


P.S.: after if there is commands that you can't do without sudo, try this:

gpasswd -a $USER docker

8. 下載、執行Image

docker pull centos

上面那行會抓到centos 8
如果要指定版本7.9

docker pull centos:centos7.9.2009

列出 image list

docker image ls

執行

docker run -it centos:centos7.9.2009 bash

印出目前執行版本

cat /etc/*release










2021年4月30日 星期五

Qt creator 5.x cannot run compiler 'g++'

 一查果然如此,这个大病毒居然就是“Anaconda”。 为毛就QT有问题呢,因为QT要用cmd去调各种命令,然后去捕获返回值,这个“系统找不到指定路径”显然不是它能够预料的,所以出错了。因此,我们需要把这个劫持解除了:【解决方法】

打开注册表编辑器
找到HKEY_CURRENT_USER\Software\Microsoft\Command Processor
可以看到一个AutoRun的字段,字段的内容有Anaconda相关的路径,就是这个路径找不到
删除这个AutoRun字段,打开qt creator,Bingo。

2019年10月7日 星期一

C/C++ 中的 static, extern 的變數

原文:https://medium.com/@alan81920/c-c-%E4%B8%AD%E7%9A%84-static-extern-%E7%9A%84%E8%AE%8A%E6%95%B8-9b42d000688f

C/C++ 中的 static, extern 的變數

李松錡
以前在大學了時候計程學的是 C++,但因為課程長度的關係,所以有很多比較複雜的觀念並沒有上到或是沒有弄得很清楚,最近因為在改一個 C++ 的 open source 的專案,所以就遇到了這個問題,於是去翻了一些資料,順手就把學到的東西記錄下來。

完整的 Compile 過程

首先如果你以前跟我ㄧ樣,都是在 windows 上用 codeblocks 或 dev c++ 這類的 IDE,那肯定對 C/C++ 的執行檔是怎麼出來的會很沒有概念,因此這邊我們不討論這些 IDE,我們下面會用 gcc/g++,了解 IDE 到底在編譯成執行檔的過程中經過哪些步驟。首先一般的編譯過程其實是長下面這個樣子的
我們人類用高階語言撰寫了各種 source file,也就是各種的 .h 和 .cpp 檔,然後每個獨立的 .h 和 .cpp 檔都應該要可以各自被 compile 成 .o 檔而不報錯,也就是說下列的問題可以被檢查出來:
  • 語法錯誤
  • 不存在或沒宣告過的變數,如果有變數是透過 include 而來的,在檢查過程中也會去參考其他有 include 到的檔案該變數是否被宣告過
  • 變數型態錯誤
我們直接使用以下範例來解釋:
main.cpp:
#include <iostream>
#include "module1.h"
using namespace std;int main() {
    greeting();
    return 0;
}
module1.h
#ifndef MODULE_1_H
#define MODULE_1_Hvoid greeting();#endif
module1.cpp
#include <iostream>
#include "module1.h"
using namespace std;void greeting() {
    cout << "Hello World!" << endl;
}
從上面例子我們可以看到 greeting() 這個 function 在 module1.h 檔案裡面只有 function signature, 但沒有實作的部分,但是我們可以使用 g++ 針對 main.cpp 先進行 compile:
g++ -c main.cpp
此時是可以成功產出一個 mian.o 這個中間過程檔的。同樣的,我們一樣可以對 module1.cpp 進行 compile:
g++ -c module1.cpp
此時也可以產出一個中間過程檔 module1.o。最後我們只要把兩個 .o 檔 Link 在一起,就可以產出一個能運行的 exectuable file:
g++ -o main main.o module1.o
從上面這個例子我們也可以看出,有一些東西即使沒把實作寫出來,每個獨立的 .cpp 檔都應該要可以被 compile 成 .o 檔,因為在 coimplie 成 .o 檔的過程中要的只有定義而已。從這裡我們也可以了解一件事,就是在 Link 的過程中兩個 .o 檔才有了互動,此時也會有更進一步的檢查。而這些繁瑣的步驟,就是 IDE 幫我們處理掉的部分。

在對 compile 有所了解以後,我們就可以來探討更複雜的問題,extern 和 static 的用途是什麼。

extern

現在我們知道每個 .cpp 檔都可以獨自被 compile,假如今天我們有一個變數要在多個檔案之間共用,該怎麼處理呢? 這就是 extern 的作用。extern 告訴 compiler 這個變數的存在,但是並不是由這個這個檔案做宣告。我們沿用上一個範例,加入更多的東西:
main.cpp:
#include <iostream>
#include "module1.h"
using namespace std;int main() {
    greeting();
    cout << "In main, a = " << a << endl;
    a = 0;
    cout << "In main, a = " << a << endl;
    return 0;
}
module1.h
#ifndef MODULE_1_H
#define MODULE_1_Hextern int a;
void greeting();#endif
module1.cpp
#include <iostream>
#include "module1.h"
using namespace std;int a = 1;
void greeting() {
    cout << "Hello World!" << endl;
    cout << "In greeting, a = " << a << endl;
}
我們增加了一個變數 a,在 module1.h 檔中有 extern int a,我們很清楚的告訴了會 include module1.h 的人,這個模組裡面有一個 int a 變數可以用,但是這個變數的宣告並不在此,而是有某個 include 了 module1.h 的人會去做宣告,這個工作在這裡我們讓 module1.cpp 做。同時我們也修改了 greeting,會把變數 a 的值印出來。在 main 裡面我們也可以取用變數 a。實際運行一次以後可以得到如下輸出:
Hello World!
In greeting, a = 1
In main, a = 1
In main, a = 0
Hello World!
In greeting, a = 0
同時我們可以看到在 main 裡面看到的 a 跟在 module1.cpp 裡面看到的 a 是同一個,因此在 main 裡面修改 a 的值,module1 裡面拿到的 a 也就改變了。

static

看完了 extern,接下來就要來解釋 static 了。相比之下 extern 算是很好理解的了,static 則比較混亂一點。static 之所以混亂,是因為他出現在不同地方,他的意義就會不同,也就是說 static 會被 overload,但每個單獨的定義其實也都很好了解。在 C 裡面因為沒有 class,所以 static 只會有兩種定義,而在 C++ 中因為多了 class,所以會再多兩種定義。static 的意義就是 “被修飾的東西,會從程式一開始執行就存在,且不會因為離開 scope 就消失,會一直存在到程式結束”。 static 出現在哪裏及用什麼定義如下:
  • static 出現在 variable 之前,且該 variable 宣告在某個 function 之中 (C/C++)
  • static 出現在 variable 之前,且該 variable 並不是宣告在某個 function 中(C/C++)
  • static 出現在 class 的 member variable 之前 (C++ only)
  • static 出現在 class 的 member function 之前 (C++ only)
大致也就是這四種定義,因此看到或想有剛好的應用情境時,可以根據上面的列表來區分。

static 出現在 variable 之前,且該 variable 宣告在某個 function 之中

這個算是非常好理解的,一般我們寫 funcion 時,裡面宣告的變數在 funcion 結束之時也會跟著消失。但如果我們在某個變數之前加上 static,該變數就不會因為 function 結束而消失。最經典的例子大概就是要計算這個 function 被呼叫幾次:
void greeting() {
    static int counter = 0;
    ++counter;
    cout << "Greeting function has been called " 
         << counter << "times" << endl;
}
每次呼叫 greeting 時,counter 就會加一,且 greeting 結束時 counter 還存在,因此可以用於計算 greeting 到底被呼叫幾次。

static 出現在 variable 之前,且該 variable 並不是宣告在某個 function 中

在我們解釋 extern 的範例中,我們會遇到變數在不同檔案中要共用,只要 include 某個檔案以後,就可以使用其中的變數。但這會導致一個比較麻煩的問題,假設今天有兩個檔案 a.cpp 和 b.cpp (應該不需要用 Alice.cpp 跟 Bob.cpp 來解釋吧 XD),各自有各自的 .h 檔。a.cpp include 了 b.h,但 a.cpp 跟 b.cpp 裡面各自宣告了一個在 function 外 的 bool debug 的變數,分別是用來啟動或關閉 a 和 b 的 debug 功能。兩個 compile 後的 .o 檔在 link 的過程中就會因為名字相同而產生衝突的錯誤。對不存在在 funciton 外的變數來說,基本上都是 global variable,static 的用意就是要讓這樣的 global 只限定在該檔案內,而不是整個程式中。因此,如果我們把 a.cpp 和 b.cpp 中的 debug 在宣告時前面都加上 static,這樣 compiler 在處理前期替換掉的名字就會不同,因此 link 的過程中也不會有名字衝突的問題。
因為覺得上面只用這個例子解釋並不好理解,所以我補上一個例子。首先我們有:
  • main.cpp
  • a.h 和 a.cpp
  • b.h 和 b.cpp
main.cpp
#include "a.h"
#include "b.h"int main() {
    show_debug_in_a();
    show_debug_in_b();
    return 0;
}
a.h
#ifndef _A_H_
#define _A_H_void show_debug_in_a();#endif
a.cpp
#include <iostream>
using namespace std;static bool debug = true;void show_debug_in_a() {
    cout << debug << endl;
}
b.h
#ifndef _B_H_
#define _B_H_void show_debug_in_b();#endif
b.cpp
#include <iostream>
using namespace std;static bool debug = false;void show_debug_in_b() {
    cout << debug << endl;
}
然後可以 compile 成 bin 檔:
$ g++ -c main.cpp // 產出 main.o
$ g++ -c a.cpp // 產出 a.o
$ g++ -c b.cpp // 產出 b.o
$ g++ -o main main.o a.o b.o // 把 main.o, a.o, b.o link 成 main
其中 main.cpp include a.h 和 b.h,當中所有的實作以及 debug 這個 static 的變數宣告都寫在 a.cpp 和 b.cpp 中,如此一來 a和 b 中的 debug 就不是同一個,也不會互相污染到。為什麼要這麼麻煩不直接 include a.cpp 和 b.cpp 呢?這是因為如果 include 某個檔案後,裡面所有的東西對於 include 的人就通通都看得到了,也因此如果直接 include a.cpp 和 b.cpp,對於 main.cpp 來說,他就看到有兩個地方都宣告了 debug 這個變數,所以 compile 的時候直接就會被檢查到而失敗。
那你可能會問,咦,我只能 include .h 檔,又沒有 include .cpp 檔,那 main.cpp 不就還是沒看到兩個 cpp 裡的 debug,那為什麼還要用 static 呢? 這是因為如果沒寫 static,則在 -c 這個 compile 的過程中產生的 .o 檔裡面會帶有這個檔案轉化後的資訊,而所有不在 function 等等 scope 的變數通通會被視為是 global variable。也因此,即便 main.cpp 在 compile 的最終只看到 show_debug_in_a 和 show_debug_in_b 兩個變數,但在把 main.o, a.o 和 b.o link 的過程中,compiler 看到 a.o 和 b.o 裡面都有一個 debug 的變數,就會報錯了。

static 出現在 class 的 member variable 之前

static 出現在 class 的 member variable 的意思是該 variable 並不屬於某個 instance,他屬於這個 class,所有以此 class 生成出來的 instance 都共用這個 variable。最經典的範例就是用來計數這個 class 總共生成了多少個 instance。

static 出現在 class 的 member function 之前

static 出現在 member function 的意思是該 function 並不屬於某個 instance,他也屬於這個 class,所有以此 class 生成出來的 instance 都共用這個 function。也因此,即便我們沒有產生 instance 出來,我們也隨時可以取用這個 function。

2019年6月26日 星期三

How to use Android Studio build static library

使用版本 Android Studio 3.1.3





















AndroidManifest.xml
CMakeLists.txt
都不用改
直接找最右邊的選項


























如果不會產生 library 就 clean































最後產生的檔案位置


2018年8月30日 星期四

Android debug keystore

參考網址:https://stackoverflow.com/questions/16622528/android-studio-debug-keystore

一般使用android run app時都會是跑debug build
如果要切換成release build跑時就會跳出app no signed的警告

因為懶得申請一個新的keystore
我是拿android幫你產生的debug keystore來用

它的位置在
~/.android/debug.keystore

它要的資訊可以按照下面的輸入
Key Alias => androiddebugkey
Key Password => android
Store File => ~/.android/debug.keystore
Store Password => android