c程式找不到函示庫 ld錯誤 函示庫各種雜症

signal SIGSEGV: address not mapped to object (fault address: 0x20)

static function不需要被呼叫就有可能發生錯誤

dlopen無法載入函示庫

https://stackoverflow.com/a/2991677/22299707

  1. ldd -r 列出所以找不到的函式庫像這樣

    1
    2
    3
    4
    5
    6
            linux-vdso.so.1 (0x00007ffcfd376000)
    libjson-glib-1.0.so.0 => /lib/x86_64-linux-gnu/libjson-glib-1.0.so.0 (0x00007f210d72c000)
    .....
    undefined symbol: descriptor_table_google_2fprotobuf_2ftimestamp_2eproto (./libnvds_msgconv.so)
    undefined symbol: _ZN6google8protobuf7Message17CopyWithSizeCheckEPS1_RKS1_ (./libnvds_msgconv.so)
    .....
  2. c++filt顯示函式庫名稱,還原函數名稱

    1
    c++filt _ZN6google8protobuf7Message17CopyWithSizeCheckEPS1_RKS1_

演算法練習筆記

  1. 注意題目裡的線索
    例如題目如果出現”寫一個在伺服器長時間重複運作的程式”,你可以想看看快取是不是可以派上用場

  2. 寫出測試範例
    要特別注意自己寫出來的範例是不是特例

  3. 嘗試暴力解

  4. 優化

Check Permutation: Given two strings, write a method to decide if one is a permutation of the other. p.90

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def check_permutation(s1, s2):
if len(s1) != len(s2):
return False
sd1 = {}
for i in s1:
if sd1.get(i):
sd1[i] = sd1[i] + 1
else:
sd1[i] = 1
for j in s2:
if sd1.get(j):
if sd1.get(j) > 0:
sd1[i] = sd1[i] - 1
else:
return False
else:
return False
return True

samples

  • dad, bda
  • abc, bca
  • sss, sss
  • sas, ssa
  • abcd, abc
  • bbba, bbbb

URLify: Write a method to replace all spaces in a string with ‘%20’. You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the “true” length of the string. (Note: If implementing in Java, please use a character array so that you can perform this operation in place.)

  • 錯誤1:
    忘記回傳值

Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palin-drome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import copy

def palindrome(input):
input = input.lower()
char_dict = {}
for c in input:
if c != " ":
if char_dict.get(c):
char_dict.pop(c)
else:
char_dict[c] = 1
if len(char_dict) > 1:
return False
return True

錯誤1: 沒有考慮到大小寫問題

One Away: There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def one_edit(str_a, str_b):
if len(str_a) - len(str_b) == -1:
for c in str_a:
if c not in str_b:
return False
elif len(str_a) - len(str_b) == 1:
for c in str_b:
if c not in str_a:
return False
elif len(str_a) - len(str_b) == 0:
counter = 0
for c in str_a:
if c not in str_b:
counter +=1
if counter > 1:
return False
else:
return False
return True

systemd相關

移除服務

stop [servicename]
1
2
3
4
5
6
7
systemctl disable [servicename]
sudo trash /etc/systemd/system/[servicename]
sudo trash /etc/systemd/system/[servicename] # and symlinks that might be related
sudo trash /usr/lib/systemd/system/[servicename]
sudo trash /usr/lib/systemd/system/[servicename] # and symlinks that might be related
systemctl daemon-reload
systemctl reset-failedbash

https://superuser.com/a/936976

CMake疑難雜症

Failed to detect a default CUDA architecture或是CMAKE_CUDA_ARCHITECTURES must be non-empty if set

直接告訴CMake nvcc的位置,注意舊的build資料夾要先清掉
-DCMAKE_CUDA_COMPILER=/usr/local/cuda-11.7/bin/nvcc

https://www.cnblogs.com/metaz/p/16919028.html

VS code設定cmake的環境變數

在setting.json加入"cmake.environment": {"CUDA_VER":"11.7"},說明書:https://github.com/microsoft/vscode-cmake-tools/blob/main/docs/cmake-presets.md#ignored-settings

CMake presets: https://dominikberner.ch/cmake-presets-best-practices/

CMake 系統環境變數設置

https://www.scivision.dev/cmake-environment-variable-scope/

libcuda.so.1 not found

使用find_package(CUDAToolkit)並且在target_link_libraries 加入CUDA::cuda_driver

CMake find_package() 用法

CMake有許多尋找套件的module,例如FindCUDAToolkit,下面連結條列各種find_package()能使用的module

https://cmake.org/cmake/help/latest/manual/cmake-modules.7.html#manual:cmake-modules(7)

apt相關問題

apt找不到key或重複套件,刪掉有問題的apt key

sudo rm /etc/apt/sources.list.d/*cuda*

查看所有可以安裝的版本

apt-cache madison cuda

https://askubuntu.com/questions/447/how-can-i-see-all-versions-of-a-package-that-are-available-in-the-archive

刪除apt key

  • 顯示所有key
    apt-key list顯示結果如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    /etc/apt/trusted.gpg
    --------------------
    pub rsa4096 2022-05-05 [SCEA]
    8298 D919 610D E70B 90CE DAAE EE38 2EF7 8230 7095
    uid [ unknown] TensorRT <svc_tensorrt@nvidia.com>

    /etc/apt/trusted.gpg.d/flexiondotorg_ubuntu_nvtop.gpg
    -----------------------------------------------------
    pub rsa1024 2011-03-16 [SC]
    824A 27DD 09DE DA33 BF78 3596 2EA8 F357 93D8 809A
    uid [ unknown] Launchpad PPA for Martin Wimpress

    /etc/apt/trusted.gpg.d/graphics-drivers_ubuntu_ppa.gpg
    ------------------------------------------------------
    pub rsa4096 2015-08-12 [SC]
    2388 FF3B E10A 76F6 38F8 0723 FCAE 110B 1118 213C
    uid [ unknown] Launchpad PPA for Graphics Drivers Team
  • 刪掉key
    以下面為例,key ID就是最後8碼82307095sudo apt-key del 82307095就可以刪除

1
2
3
4
5
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2022-05-05 [SCEA]
8298 D919 610D E70B 90CE DAAE EE38 2EF7 8230 7095
uid [ unknown] TensorRT <svc_tensorrt@nvidia.com>

解決Unmet dependencies問題

  • dpkg -P package-name來移除有問題的套件,他會顯示不能移除的原因,然後依序移除即可

  • 直接用dpkg安裝套件,如果有缺少的相依套件,之後使用apt install就會顯示E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).錯誤,必須要手動解決相依性問提

apt install使用local repo

參考: https://wiki.debian.org/DebianRepository/Setup?action=show&redirect=DebianRepository%2FHowTo%2FTrivialRepository#trivialArchive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Quick instructions to create a trivial local archive with apt-ftparchive
We use /var/lib/mydebs as the example location

Create the directory

mkdir -p /var/lib/mydebs
Every time you put new files in the directory, you'll have to scan the directory and update:

cd /var/lib/mydebs
apt-ftparchive packages . > Packages
apt-ftparchive release . > Release
Add this line to /etc/apt/sources.list

deb [allow-insecure=yes] file:/var/lib/mydebs ./
Now you can install the package normally. If apt-get asks "Install these packages without verification?", answer "Y" to install. That's because this local repository is not signed.

apt-get update
apt-get install mydeb

刪除apt gpg key 和repo

https://itslinuxfoss.com/delete-repository-and-gpg-key-ubuntu/#2

查看套件版本

dpkg-query -W tensorrt

用apt 查看可安裝的版本

apt-cache policy <package name>

例如

1
2
3
4
5
6
7
8
$ apt-cache policy gparted
gparted:
Installed: 0.16.1-1
Candidate: 0.16.1-1
Version table:
*** 0.16.1-1 0
500 http://ubuntu.inode.at/ubuntu/ saucy/main amd64 Packages
100 /var/lib/dpkg/status

安裝指定版本
sudo apt-get install <package name>=<version>
例如上面的例子

1
2
3
4
5
6
$ sudo apt-get install gparted=0.16.1-1
Reading package lists... Done
Building dependency tree
Reading state information... Done
gparted is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 265 not upgraded.

CodeInterview

BigO

  • 時間複雜度相加還是相乘
    相加
    1
    2
    3
    4
    5
    6
    7
    8
    Add the Runtimes: 0 (A  +  B) 
    1 for (int a : arrA) {
    2 print(a);
    3 }
    4
    5 for (int b : arrB) {
    6 print(b) ;
    7 }

相乘:

1
2
3
4
5
6
Multiply the Runtimes: O(A* B) 
1 for (int a : arrA) {
2 for (int b : arrB) {
3 print(a + " , " + b);
4 }
5 }
  • 遞迴的時間複雜度