{"id":25208269,"url":"https://github.com/1010code/c--shared-library-tutorial","last_synced_at":"2025-04-05T04:19:40.886Z","repository":{"id":118676015,"uuid":"529881215","full_name":"1010code/c--shared-library-tutorial","owner":"1010code","description":"c++-shared-library-tutorial","archived":false,"fork":false,"pushed_at":"2024-04-14T07:00:52.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T12:19:10.566Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/1010code.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-08-28T14:09:48.000Z","updated_at":"2022-09-26T12:11:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"1bab782d-1371-4aff-a5fd-56ac15b0c52d","html_url":"https://github.com/1010code/c--shared-library-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fc--shared-library-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fc--shared-library-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fc--shared-library-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fc--shared-library-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/1010code","download_url":"https://codeload.github.com/1010code/c--shared-library-tutorial/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247285745,"owners_count":20913862,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-10T12:19:12.643Z","updated_at":"2025-04-05T04:19:40.872Z","avatar_url":"https://github.com/1010code.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 建立函式庫\n建立標頭檔，定義所需的函式。\n```c\n# mylib.h\n#include\u003cstring\u003e\n#include\u003cvector\u003e\n#include\u003ciostream\u003e\n\nusing namespace std;\n\nvoid say(string message);\nvoid say(vector\u003cstring\u003e vec);\n```\n\n實作函式庫內容。\n```c\n# mylib.cpp\n#include\u003cstring\u003e\n#include\u003cvector\u003e\n#include\u003ciostream\u003e\n\nusing namespace std;\n\nvoid say(string message){\n  cout \u003c\u003c message \u003c\u003c endl;\n}\nvoid say(vector\u003cstring\u003e vec){\n  for(int c=0; c\u003cvec.size(); c++){\n    cout \u003c\u003c vec[c] \u003c\u003c endl;\n  }\n}\n```\n\n產生 Dynamic Link Library `libmylib.dylib`(也可以.so)。\n\n```sh\ng++ --verbose -dynamiclib -o libmylib.dylib mylib.cpp\n```\n\nmain 檔執行編譯時，`-L` 連結 shared libraries 資料夾路徑，編譯時會找所有的 `so, dll,dylib` 檔也。最後要透過 `-l` 連結確切的 `mylib` Shared Library。\n```sh\ng++ -o main ./main.cpp -L./ -lmylib -std=c++11\n```\n\n或是直接連結到 dylib(so) 也行。\n\n```sh\ng++ -o main ./main.cpp libmylib.dylib -std=c++11\n```\n\n\n### 不同資料夾\n`-I`目的是要讓 `main.cpp` 知道要去哪裡抓標頭檔。`-L` 是連結動態函式庫。執行檔同個資料夾下必須要有`libmylib.dylib`才能正確地被執行。\n\n```sh\ng++ -o main ./main.cpp -I./test -L./test -lmylib -std=c++11\n```\n\n\u003e -I{include dirs} to inform gcc where to look when you #include something.\n\u003e -L tells gcc about paths to look into for libraries, and -l{libname} links against a library.\n\n另外也可以將編譯好的 object file `.o` 檔，轉換成 `.so`:\n\n```sh\ngcc hello.o -shared -o libhello.so\n```\n\n## 其他方法\n### 直接在編譯時給予外部函式庫路徑\n\n```sh\ng++ -o main main.cpp ./mylib.cpp -std=c++11 \n```\n\n### 或是先編譯成 o 檔\ncompile those source files to object files, first:\n\n```sh\ng++ -c -o mylib.o ./mylib.cpp\n```\n\u003e -c tells gcc not to try and link things together\n\n```sh\ng++ -o main mylib.o main.cpp -std=c++11\n```\n\n\n## 重點整理\nSearch Path and Library Linking Flags.\n- -l[linalg]\n  - =\u003e 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.\n- -L[/path/to/shared-libraries]\n  - =\u003e Add search path to shared libraries, directory containing *.so, *.dll or *.dlyb files such as libLinearAlgebra.so depending on the current operating system.\n- -I[/path/to/header-files]\n  - Add search path to header files (.h) or (.hpp).\n\n## Reference\n- [C++ Creating Shared Library on macOS](https://www.youtube.com/watch?v=PRUR_bN3r-E).\n- [[stackoverflow] Properly Link Libraries in the Command Line](https://stackoverflow.com/questions/33110118/properly-link-libraries-in-the-command-line)\n- [CPP/C++ Compiler Flags and Options](https://caiorss.github.io/C-Cpp-Notes/compiler-flags-options.html)\n- [什麼是SO檔案格式?](https://template.city/so/)\n- [Static vs Dynamic Libraries](https://www.bogotobogo.com/cplusplus/libraries.php)\n- [windows dll 和 Linux so 的異同](https://www.796t.com/content/1546146567.html)\n- [Linux靜態庫與共享庫的使用](https://jasonblog.github.io/note/linked_and_loader/linux_jing_tai_ku_yu_gong_xiang_ku_de_shi_yong.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1010code%2Fc--shared-library-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F1010code%2Fc--shared-library-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1010code%2Fc--shared-library-tutorial/lists"}