/****showcoor.cpp****/ #include "showcoor.h" //显示某一坐标 int showcoor(int x) { cout<<"coordinate:"<<x<<endl; return 0; } /****showcoor.h****/ #include <iostream> using namespace std; int showcoor(int x); /****showpoint.cpp****/ #include "showpoint.h" //显示点坐标 int showpoint(int x,int y,int z) { showcoor(x); showcoor(y); showcoor(z); return 0; } /****showpoint.h****/ #include "showcoor.h" int showpoint(int x,int y,int z); /****main.cpp****/ #include "showpoint.h" int main() { showpoint(1,2,3); return 0; }
g++ -c showcoor.cpp g++ -c showpoint.cpp g++ -c main.cpp
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls *.o main.o showcoor.o showpoint.o [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>mkdir temp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>cp showpoint.cpp showpoint.h ./temp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>cd ./temp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call/temp>g++ -c showpoint.cpp In file included from showpoint.cpp:1: showpoint.h:1:22: error: showcoor.h: No such file or directory showpoint.cpp: In function ‘int showpoint(int, int, int)‘: showpoint.cpp:4: error: ‘showcoor‘ was not declared in this scope
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call/temp>g++ -I../ -c showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call/temp>ls showpoint.cpp showpoint.h showpoint.o
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -o main main.o showpoint.o showcoor.o [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>./main coordinate:1 coordinate:2 coordinate:3
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -o main main.o showpoint.o showpoint.o: In function `showpoint(int, int, int)‘: showpoint.cpp:(.text+0x17): undefined reference to `showcoor(int)‘ showpoint.cpp:(.text+0x21): undefined reference to `showcoor(int)‘ showpoint.cpp:(.text+0x2b): undefined reference to `showcoor(int)‘ collect2: ld returned 1 exit status
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -o main main.o showcoor.o main.o: In function `main‘: main.cpp:(.text+0x14): undefined reference to `showpoint(int, int, int)‘ collect2: ld returned 1 exit status
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -o main showpoint.o showcoor.o /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../lib64/crt1.o: In function `_start‘: (.text+0x20): undefined reference to `main‘ collect2: ld returned 1 exit status [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -c showcoor.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ar -cr libshowcoor.a showcoor.o [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls *.a libshowcoor.a
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ar -cr libshowcoor.a showcoor.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls *.a libshowcoor.a
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>gcc -shared -fPIC -o libshowpoint.so showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls *.so libshowpoint.so
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -c showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -shared -fPIC -o libshowpoint.so showpoint.o /usr/bin/ld: showpoint.o: relocation R_X86_64_32 against `.bss‘ can not be used when making a shared object; recompile with -fPIC showpoint.o: could not read symbols: Bad value collect2: ld returned 1 exit status
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_callg++ -fPIC -c showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_callg++ -shared -fPIC -o libshowpoint.so showpoint.op [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls *.so libshowpoint.so
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ main.cpp -L./ -lshowpoint .//libshowpoint.so: undefined reference to `showcoor(int)‘ collect2: ld returned 1 exit status 注:-L后面跟的是要连接的库的路径
[billing_dx@bmcs1 nest_call]$ g++ -o main main.cpp -L./ -lshowpoint -lshowcoor [billing_dx@bmcs1 nest_call]$ ./main coordinate:1 coordinate:2 coordinate:3
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -fPIC -c showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>gcc -shared -fPIC -o libshowpoint.so showpoint.o -L./ -lshowcoor /usr/bin/ld: .//libshowcoor.a(showcoor.o): relocation R_X86_64_32 against `.rodata‘ can not be used when making a shared object; recompile with -fPIC .//libshowcoor.a: could not read symbols: Bad value collect2: ld returned 1 exit status [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -fPIC -c showcoor.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ar -cr libshowcoor.a showcoor.o [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -fPIC -c showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -shared -fPIC -o libshowpoint.so showpoint.o -L./ -lshowcoor [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls *.so libshowpoint.so
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ main.cpp -L./ -lshowpoint [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>./main coordinate:1 coordinate:2 coordinate:3
[billing_dx@bmcs1 nest_call]$ g++ -fPIC -c showpoint.cpp [billing_dx@bmcs1 nest_call]$ g++ -shared -fPIC -o libshowpoint.so showpoint.o [billing_dx@bmcs1 nest_call]$ g++ -fPIC -c showcoor.cpp [billing_dx@bmcs1 nest_call]$ g++ -shared -fPIC -o libshowcoor.so showcoor.o [billing_dx@bmcs1 nest_call]$ ls *.so libshowcoor.so libshowpoint.so [billing_dx@bmcs1 nest_call]$ g++ -o main main.cpp -L./ -lshowcoor -lshowpoint [billing_dx@bmcs1 nest_call]$ ./main coordinate:1 coordinate:2 coordinate:3 [billing_dx@bmcs1 nest_call]$ g++ -o main main.cpp -L./ -lshowpoint -lshowcoor [billing_dx@bmcs1 nest_call]$ ./main coordinate:1 coordinate:2 coordinate:3
[billing_dx@bmcs1 nest_call]$ g++ -c showcoor.cpp [billing_dx@bmcs1 nest_call]$ ar -cr libshowcoor.a showcoor.o [billing_dx@bmcs1 nest_call]$ g++ -c showpoint.cpp [billing_dx@bmcs1 nest_call]$ ar -cr libshowpoint.a showpoint.o [billing_dx@bmcs1 nest_call]$ ls *.a libshowcoor.a libshowpoint.a [billing_dx@bmcs1 nest_call]$ g++ -o main main.cpp -L./ -lshowpoint -lshowcoor [billing_dx@bmcs1 nest_call]$ ./main coordinate:1 coordinate:2 coordinate:3
[billing_dx@bmcs1 nest_call]$g++ -o main main.cpp -L./ -lshowcoor -lshowpoint .//libshowpoint.a(showpoint.o): In function `showpoint(int, int, int)‘: showpoint.cpp:(.text+0x17): undefined reference to `showcoor(int)‘ showpoint.cpp:(.text+0x21): undefined reference to `showcoor(int)‘ showpoint.cpp:(.text+0x2b): undefined reference to `showcoor(int)‘ collect2: ld returned 1 exit status
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>nm -C libshowpoint.so |grep show 0000000000000a5f t global constructors keyed to showcoor.cpp 00000000000009ba t global constructors keyed to showpoint.cpp 00000000000009d0 T showcoor(int) 000000000000093c T showpoint(int, int, int)
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -fPIC -c showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>gcc -shared -fPIC -o libshowpoint.so showpoint.o [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>nm -C libshowpoint.so |grep show 000000000000076a t global constructors keyed to showpoint.cpp U showcoor(int) 00000000000006ec T showpoint(int, int, int)
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>gcc -shared -fPIC -o libshowpoint.so showpoint.cpp showcoor.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -o main main.cpp -L./ -lshowpoint [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>./main coordinate:1 coordinate:2 coordinate:3 [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ldd main |grep show libshowpoint.so => /account/work/ymm/test/library/nest_call/libshowpoint.so (0x00002ba77c1ad000) [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>
[billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>gcc -shared -fPIC -o libshowpoint.so showpoint.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>gcc -shared -fPIC -o libshowcoor.so showcoor.cpp [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ls *.so libshowcoor.so libshowpoint.so [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>g++ -o main main.cpp -L./ -lshowpoint -lshowcoor [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>./main coordinate:1 coordinate:2 coordinate:3 [billing_dx@bmcs1]:/account/work/ymm/test/library/nest_call>ldd main|grep show libshowpoint.so => /account/work/ymm/test/library/nest_call/libshowpoint.so (0x00002abf7504a000) libshowcoor.so => /account/work/ymm/test/library/nest_call/libshowcoor.so (0x00002abf7524b000)
原文:http://blog.csdn.net/yang15225094594/article/details/19558189