SunOS 5.10 sparc C/C++ 初学者之动态链接库

一、建立目录结构

/data/aifront/cpl/others/demo02
-rw-rw-r-- console.cpp
drwxrwxr-x src /*源文件目录*/
-rw-rw-r-- hello.mk
drwxrwxr-x include /*空目录*/
drwxrwxr-x lib /*空目录*/
-rw-rw-r-- console.mk
drwxrwxr-x tmp /*空目录*/

/data/aifront/cpl/others/demo02/src
-rw-rw-r-- hello.cpp
-rw-rw-r-- hello.h

<<hello.h>>

#ifndef HELLO_H_
#define HELLO_H_

#include <fstream>
#include <string>
using namespace std;

class hello{
	
	public:
		hello();
		~hello();
		void say();
};

#endif

<<hello.cpp>>

#include "hello.h"

hello::hello(){
	cout << "我被创建了!" << endl;
}

hello::~hello(){
	cout << "我被删除了!" << endl;
}

void hello::say(){
	cout << "Hello World !" << endl;
}

<<hello.mk>>

so:cp hello
	CC -G ./tmp/hello.o -o ./lib/libhello.so
hello:
	CC -c ./src/hello.cpp -o ./tmp/hello.o
cp:
	cp ./src/hello.h ./include/hello.h


<<console.cpp>>

#include "hello.h"

int main()
{
	hello he;
	he.say();
	return 0;
}

<<console.mk>>

main:console
	CC -L/data/aifront/cpl/others/demo02/lib -lhello ./tmp/t.o -o ./bin
console:
	CC -I/data/aifront/cpl/others/demo02/include -c console.cpp -o ./tmp/t.o

代码执行过程

[/data/aifront/cpl/others/demo02]make -f hello.mk
cp ./src/hello.h ./include/hello.h
CC -c ./src/hello.cpp -o ./tmp/hello.o
CC -G ./tmp/hello.o -o ./lib/libhello.so
[/data/aifront/cpl/others/demo02]make -f console.mk
CC -I/data/aifront/cpl/others/demo02/include -c console.cpp -o ./tmp/t.o
CC -L/data/aifront/cpl/others/demo02/lib -lhello ./tmp/t.o -o ./bin
[/data/aifront/cpl/others/demo02]export LD_LIBRARY_PATH=/data/aifront/cpl/others/demo02/lib
[/data/aifront/cpl/others/demo02]bin
我被创建了!
Hello World !
我被删除了!
[/data/aifront/cpl/others/demo02]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钩子水手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值