g++编译运行c++代码步骤以及动态库静态库的创建与使用
发布时间:2021-12-05 14:22:31 所属栏目:教程 来源:互联网
导读:一、g++ 编译运行hello world 1编写hello world 代码 #includeiostream using namespace std; int main() { cout hello world! endl; return 0; } 2 编译及运行 自动生成了a.out可执行的文件 linuxidc@shenyong-Opt790:~/work/cpp$ g++ hello.cpp linuxidc@s
一、g++ 编译运行hello world 1编写hello world 代码 #include<iostream> using namespace std; int main() { cout << "hello world!" << endl; return 0; } 2 编译及运行 自动生成了a.out可执行的文件 linuxidc@shenyong-Opt790:~/work/cpp$ g++ hello.cpp linuxidc@shenyong-Opt790:~/work/cpp$ ls a.out hello.cpp linuxidc@shenyong-Opt790:~/work/cpp$ ./a.out hello world! 二、程序运行步骤 预处理 .i .ii文件 gcc -E hello.cpp -o hello.i 编译 .s文件 gcc -S hello.cpp -o hello.s 汇编 .o .obj文件 gcc -c hello.cpp -o hello.o 链接 .lib .a文件1234 1 预处理 vim hello.i ...上面很长省略了.. extern wostream wclog; static ios_base::Init __ioinit; } # 2 "hello.cpp" 2 using namespace std; int main() { cout << "hello world!" << endl; return 0; } 2 编译 vim hello.s .file "hello.cpp" .local _ZStL8__ioinit .comm _ZStL8__ioinit,1,1 .section .rodata .LC0: .string "hello world!" .text .globl main .type main, @function main: .LFB971: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $.LC0, %esi movl $_ZSt4cout, %edi call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi movq %rax, %rdi 3 汇编 vim hello.o ^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^A^@>^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ð^A^@^@^@^@^@^@^@^@^@^@@^@^@^@^@^@@^@^O^@^L^@UH<89>å¾^@^@^@^@¿^@^@^@^@è^@^@^@^@¾^@^@^@^@H<89>Çè^@^@^@^@¸^@^@^@^@]ÃUH<89>åH<83>ì^P<89>}ü<89>uø<83>}ü^Au'<81>}øÿÿ^@^@u^^¿^@^@^@^@è^@^@^@^@º^@^@^@^@¾^@^@^@^@¿^@^@^@^@è^@^@^@^@ÉÃUH<89>å¾ÿÿ^@^@¿^A^@^@^@è°ÿÿÿ]Ãhello world!^@^@^@^@^@^@^@^@^@^@^@^@GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4^@^@^@^@^@^T^@^@^@^@^@^@^@^AzR^@^Ax^P^A^[^L^G^H<90>^A^@^@^^@^@^@^^@^@^@^@^@^@^@'^@^@^@^@A^N^P<86>^BC^M^Fb^L^G^H^@^@^@^^@^@^@<^@^@^@^@^@^@^@=^@^@^@^@A^N^P<86>^BC^M^Fx^L^G^H^@^@^@^^@^@1 4 链接 三、多文件编译链接 1 编写add.h add.cpp main.cpp代码 add.h 头文件 #ifndef _ADD_H #define _ADD_H extern int add(int a,int b); //extern 表示函数实现在另一个文件中 #endif ~ add.cpp 函数实现 int add(int a,int b) { return (a+b); } main.cpp main函数 #include<iostream> #include"add.h" using namespace std; int main() { int a = 1,b = 2; int c = add(a,b); cout << a << " + " << b << " = " << c << endl; return 0; } 2 编译运行 linuxidc@shenyong-Opt790:~/work/cpp$ g++ add.h add.cpp main.cpp -o main linuxidc@shenyong-Opt790:~/work/cpp$ ls add.cpp add.h main main.cpp linuxidc@shenyong-Opt790:~/work/cpp$ ./main 1 + 2 = 3 四、动态库与静态库 静态库 .a (linux) .lib (windows) 动态库 .so (linux) .dll (windows) gcc时,用-I和-L分别制定库名和库路径 1 静态库的创建与使用 1 创建add.c和add.h,其中main函数中用到了add这个函数,如果直接运行会报错说找不到add这个函数。 2 先add.c编译成二进制文件add.o g++ -c add.c -o add.o 3 再用ar这个工具将add.o做成静态库 ar -r libadd.a add.o 4 这样就可以调用了,即在编译的时候加上库的名字,路径并申明为静态库-static等参数即可。 g++ mian.c -ladd -L/home/linuxidc/work/cpp -static -o main 5 注意要其他地方也要用的话要把add.h也拷贝过去才能用 linuxidc@linuxidc:~/work/cpp$ ls add.c add.h add.o a.out mian.c linuxidc@linuxidc:~/work/cpp$ rm a.out linuxidc@linuxidc:~/work/cpp$ ar -r libadd.a add.o ar: creating libadd.a linuxidc@linuxidc:~/work/cpp$ ls add.c add.h add.o libadd.a mian.c linuxidc@linuxidc:~/work/cpp$ g++ mian.c -llibadd -L./ -static -o main /usr/bin/ld: cannot find -llibadd collect2: error: ld returned 1 exit status linuxidc@linuxidc:~/work/cpp$ g++ mian.c -libadd -L./ -static -o main /usr/bin/ld: cannot find -libadd collect2: error: ld returned 1 exit status linuxidc@linuxidc:~/work/cpp$ pwd /home/linuxidc/work/cpp linuxidc@linuxidc:~/work/cpp$ g++ mian.c -libadd -L/home/linuxidc/work/cpp -static -o main /usr/bin/ld: cannot find -libadd collect2: error: ld returned 1 exit status linuxidc@linuxidc:~/work/cpp$ g++ mian.c -ladd -L/home/linuxidc/work/cpp -static -o main linuxidc@linuxidc:~/work/cpp$ ls add.c add.h add.o libadd.a main mian.c linuxidc@linuxidc:~/work/cpp$ ./main 1+2=3 其他地方也要用的话 linuxidc@linuxidc:~/work$ ls add.h cpp mian.c linuxidc@linuxidc:~/work$ g++ mian.c -ladd -L./cpp -static -o main linuxidc@linuxidc:~/work$ ls add.h cpp main mian.c linuxidc@linuxidc:~/work$ ./main 1+2=3 2 动态函数库的建立与使用 1.首先创建libmyadd.so,即利用add.c这个函数生成动态库 g++ -share -fPIC -o libmyadd.so add.c -share指为共享的,-fPIC表示position independent code位置无关,这是动态库特性 2.指定动态库生成可执行文件,-L.表示当前文件夹,-lmyadd表示去找libmyadd.so这个动态库文件。 g++ main.c -L. -lmyadd 3.直接使用会抱错,找不到动态库,要指定动态库的路径 ./a.out会报错 LD_LIBRARY_PATH=. ./a.out指定当前库的路径后在运行就可以了 4.如果要一直用,可以将.so文件的目录添加到/etc/ld.so.conf里面,然后再执行ldconfig就行了,具体如下面: linuxidc@linuxidc:~/work/cpp$ ls add.c add.h add.o main.c linuxidc@linuxidc:~/work/cpp$ g++ -share -fPIC -o libmyadd.so add.c g++: error: unrecognized command line option ‘-share’ linuxidc@linuxidc:~/work/cpp$ g++ -shared -fPIC -o libmyadd.so add.c linuxidc@linuxidc:~/work/cpp$ ls add.c add.h add.o libmyadd.so main.c linuxidc@linuxidc:~/work/cpp$ g++ main.c -L. -lmyadd linuxidc@linuxidc:~/work/cpp$ ls add.c add.h add.o a.out libmyadd.so main.c linuxidc@linuxidc:~/work/cpp$ ./a.out ./a.out: error while loading shared libraries: libmyadd.so: cannot open shared object file: No such file or directory linuxidc@linuxidc:~/work/cpp$ LD_LIBRARY_PATH=. ./a.out 1+2=3 如果要永久使用的话该怎么办呢? linuxidc@linuxidc:~/work/cpp$ ls add.c add.h add.o a.out libmyadd.so main.c linuxidc@linuxidc:~/work/cpp$ ./a.out 这里直接用会报错 ./a.out: error while loading shared libraries: libmyadd.so: cannot open shared object file: No such file or directory linuxidc@linuxidc:~/work/cpp$ vi /etc/ld.so.conf 在这个文件里面添加.so所在的目录就行了 linuxidc@linuxidc:~/work/cpp$ pwd /home/linuxidc/work/cpp linuxidc@linuxidc:~/work/cpp$ vi /etc/ld.so.conf linuxidc@linuxidc:~/work/cpp$ sudo ldconfig linuxidc@linuxidc:~/work/cpp$ ./a.out 1+2=3 例如,我的添加后的ld.so.conf文件就是这个样子: include /etc/ld.so.conf.d/*.conf /home/linuxidc/work/cpp ![]() (编辑:三明站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐