Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/1010code/c--shared-library-tutorial
c++-shared-library-tutorial
https://github.com/1010code/c--shared-library-tutorial
Last synced: 5 days ago
JSON representation
c++-shared-library-tutorial
- Host: GitHub
- URL: https://github.com/1010code/c--shared-library-tutorial
- Owner: 1010code
- Created: 2022-08-28T14:09:48.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-14T07:00:52.000Z (10 months ago)
- Last Synced: 2024-04-28T05:22:51.139Z (10 months ago)
- Language: C++
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 建立函式庫
建立標頭檔,定義所需的函式。
```c
# mylib.h
#include
#include
#includeusing namespace std;
void say(string message);
void say(vector vec);
```實作函式庫內容。
```c
# mylib.cpp
#include
#include
#includeusing namespace std;
void say(string message){
cout << message << endl;
}
void say(vector vec){
for(int c=0; c -I{include dirs} to inform gcc where to look when you #include something.
> -L tells gcc about paths to look into for libraries, and -l{libname} links against a library.另外也可以將編譯好的 object file `.o` 檔,轉換成 `.so`:
```sh
gcc hello.o -shared -o libhello.so
```## 其他方法
### 直接在編譯時給予外部函式庫路徑```sh
g++ -o main main.cpp ./mylib.cpp -std=c++11
```### 或是先編譯成 o 檔
compile those source files to object files, first:```sh
g++ -c -o mylib.o ./mylib.cpp
```
> -c tells gcc not to try and link things together```sh
g++ -o main mylib.o main.cpp -std=c++11
```## 重點整理
Search Path and Library Linking Flags.
- -l[linalg]
- => Links to shared library or shared object - Specifically, it links to linalg.dll on Windows, liblinalg.so (on Unix-like oses like Linux, BSD, AIX, …) or linalg.dylib on MacOSX.
- -L[/path/to/shared-libraries]
- => Add search path to shared libraries, directory containing *.so, *.dll or *.dlyb files such as libLinearAlgebra.so depending on the current operating system.
- -I[/path/to/header-files]
- Add search path to header files (.h) or (.hpp).## Reference
- [C++ Creating Shared Library on macOS](https://www.youtube.com/watch?v=PRUR_bN3r-E).
- [[stackoverflow] Properly Link Libraries in the Command Line](https://stackoverflow.com/questions/33110118/properly-link-libraries-in-the-command-line)
- [CPP/C++ Compiler Flags and Options](https://caiorss.github.io/C-Cpp-Notes/compiler-flags-options.html)
- [什麼是SO檔案格式?](https://template.city/so/)
- [Static vs Dynamic Libraries](https://www.bogotobogo.com/cplusplus/libraries.php)
- [windows dll 和 Linux so 的異同](https://www.796t.com/content/1546146567.html)
- [Linux靜態庫與共享庫的使用](https://jasonblog.github.io/note/linked_and_loader/linux_jing_tai_ku_yu_gong_xiang_ku_de_shi_yong.html)