20121030
ocs8:/home/panpan/C++>ls
hello.cpp hello.h main.cpp Makefile
ocs8:/home/panpan/C++>cat -n hello.cpp
1 #include <iostream>
2 using namespace std;
3 void hello(const char*name)
4 {
5
6 cout<<"hello"<<name<<endl;
7 }
ocs8:/home/panpan/C++>cat -n hello.h
1 //#ifndef HELLO_H
2 //#define HELLO_H
3
4 extern void hello(const char *name);
5
6 //#endif
7
8
ocs8:/home/panpan/C++>cat -n main.cpp
1 #include "hello.h"
2 #include <iostream>
3 using namespace std;
4 int x=1;
5 int main()
6 {
7 cout<<x<<endl;
8 hello("everyone");
9 return 0;
10
11 }
ocs8:/home/panpan/C++>g++ -c hello.cpp
ocs8:/home/panpan/C++>ls
hello.cpp hello.h hello.o main.cpp Makefile
ocs8:/home/panpan/C++>ar -crv libhello.a hello.o
a - hello.o
ocs8:/home/panpan/C++>ls
hello.cpp hello.h hello.o libhello.a main.cpp Makefile
ocs8:/home/panpan/C++>g++ -o hello main.cpp -L. -lhello
ocs8:/home/panpan/C++>ls
hello hello.cpp hello.h hello.o libhello.a main.cpp Makefile
ocs8:/home/panpan/C++>hello
1
helloeveryone
ocs8:/home/panpan/C++>g++ main.cpp -fPIC -shared -o libhello.so
ocs8:/home/panpan/C++>ls
hello hello.cpp hello.h hello.o libhello.a libhello.so main.cpp Makefile
ocs8:/home/panpan/C++>g++ main.cpp -o hel -L. -lhello
/tmp/ccTvdYvd.o: In function `main':
main.cpp:(.text+0x93): undefined reference to `hello(char const*)'
collect2: ld returned 1 exit status
ocs8:/home/panpan/C++>ls
hello hello.cpp hello.h hello.o libhello.a libhello.so main.cpp Makefile
ocs8:/home/panpan/C++>rm -r libhello.a
ocs8:/home/panpan/C++>g++ main.cpp -o hel -L. -lhello
/tmp/cc5zXm2q.o: In function `main':
main.cpp:(.text+0x93): undefined reference to `hello(char const*)'
collect2: ld returned 1 exit status
ocs8:/home/panpan/C++>
posted on 2012-10-30 16:00 angelpan1014 阅读(316) 评论(0) 编辑 收藏 举报