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
https://colobu.com/2018/08/28/15-Most-Frequently-Used-GCC-Compiler-Command-Line-Options/
https://blog.csdn.net/gatieme/article/details/21389603
https://blog.csdn.net/fengbingchun/article/details/80960916