建立標頭檔,定義所需的函式。
# mylib.h
#include<string>
#include<vector>
#include<iostream>
using namespace std;
void say(string message);
void say(vector<string> vec);
實作函式庫內容。
# mylib.cpp
#include<string>
#include<vector>
#include<iostream>
using namespace std;
void say(string message){
cout << message << endl;
}
void say(vector<string> vec){
for(int c=0; c<vec.size(); c++){
cout << vec[c] << endl;
}
}
產生 Dynamic Link Library libmylib.dylib
(也可以.so)。
g++ --verbose -dynamiclib -o libmylib.dylib mylib.cpp
main 檔執行編譯時,-L
連結 shared libraries 資料夾路徑,編譯時會找所有的 so, dll,dylib
檔也。最後要透過 -l
連結確切的 mylib
Shared Library。
g++ -o main ./main.cpp -L./ -lmylib -std=c++11
或是直接連結到 dylib(so) 也行。
g++ -o main ./main.cpp libmylib.dylib -std=c++11
-I
目的是要讓 main.cpp
知道要去哪裡抓標頭檔。-L
是連結動態函式庫。執行檔同個資料夾下必須要有libmylib.dylib
才能正確地被執行。
g++ -o main ./main.cpp -I./test -L./test -lmylib -std=c++11
-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
:
gcc hello.o -shared -o libhello.so
g++ -o main main.cpp ./mylib.cpp -std=c++11
compile those source files to object files, first:
g++ -c -o mylib.o ./mylib.cpp
-c tells gcc not to try and link things together
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).