gcc編譯so和可執行文件
gcc -o libfun1.so fun1.cpp -shared -fPIC
就可以得到文件名爲libfun1.so的文件了。
其中 -shared選項說明編譯成的文件爲動態鏈接庫,不使用該選項相當於可執行文件
-fPIC 表示編譯爲位置獨立的代碼,不用此選項的話編譯後的代碼是位置相關的。
加載so編譯可執行文件
gcc -std=c++11 -o main main.cpp -I./fun1/ -lfun1 -L./fun1/
C++版本設置
The default mode is C++98 for GCC versions prior to 6.1, and C++14 for GCC 6.1 and above. You can use command-line flag -std to explicitly specify the C++ standard. For example,
-std=c++98, or -std=gnu++98 (C++98 with GNU extensions)
-std=c++11, or -std=gnu++11 (C++11 with GNU extensions)
-std=c++14, or -std=gnu++14 (C++14 with GNU extensions), default mode for GCC 6.1 and above.
-std=c++17, or -std=gnu++17 (C++17 with GNU extensions), experimental.
-std=c++2a, or -std=gnu++2a (C++2a with GNU extensions), experimental.
頭文件路徑,庫目錄,庫名稱路徑設置
-I, -L and -l, 如
-I/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include
-L/lib
-lgcc_s // libgcc_s.a
如果加載的庫還依賴其它庫,要把路徑加載到
export LD_LIBRARY_PATH=xxdir:$LD_LIBRARY_PATH
參考
https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
https://blog.csdn.net/gaoxiang__/article/details/38094525