Makefile篇---动态库编译和测试动态库

编写code

结构如下:

在这里插入图片描述

非主目录

(下面三个代码 SoTest.h SoTest.cpp test.cpp 是在001的文件夹下,非主目录)

SoTest.h

#ifndef INC_0303_SOTEST_H
#define INC_0303_SOTEST_H


class SoTest {
    public:
        void func1();
        virtual void func2();
        virtual void func3()=0;
};


#endif //INC_0303_SOTEST_H

SoTest.cpp

#include<iostream>
#include "SoTest.h"

void SoTest::func1(){
    printf("func1\n");
};

void SoTest::func2(){
    printf("func2\n");
};

test.cpp

#include <iostream>
#include "SoTest.h"

class Test:public SoTest{

public:
    void func2(){
        printf("test-func2\n");
    }


    void func3(){
        printf("test-func3\n");
    }
};


int main() {

    Test t1;
    t1.func1();
    t1.func2();
    t1.func3();

    return 0;
}

编译动态库

g++ -shared -fPIC SoTest.cpp -o libSoTest.so

在这里插入图片描述

测试动态库

 g++ test.cpp -lSoTest -L./ -o test

在这里插入图片描述

主目录

(主目录)

SoTest.h

#ifndef INC_0303_SOTEST_H
#define INC_0303_SOTEST_H


class SoTest {
    public:
        void func1();
        virtual void func2();
        virtual void func3()=0;
};


#endif //INC_0303_SOTEST_H

main.cpp

#include <iostream>
#include "SoTest.h"

class MyTest:public SoTest{
public:
    void func2(){
        printf("main-func2\n");
    }

    void func3(){
        printf("main-func3\n");
    }
};

int main() {
    MyTest t1;
    t1.func1();
    t1.func2();
    t1.func3();

    return 0;
}

编译 main.cpp 后 运行 发现找不到 libSoTest.so的动态库
编译过程:

g++ main.cpp -lSoTest -L./001 -o main

解决方案如下:

1 动态库编译完成之后要发布,否则程序运行时找不到

Linux 默认动态库路径配置文件
/etc/ld.so.conf     /etc/ld.so.conf.d/*.conf

/usr/lib
/usr/local/lib

2 运行时手动指定动态库目录
//mac
DYLD_LIBRARY_PATH=./001
export DYLD_LIBRARY_PATH

//linux
LD_LIBRARY_PATH=./001
export LD_LIBRARY_PATH

实践证明 自己生成的动态库需要 复制到 ubuntu 的 /usr/lib 下才会识别, 在 /usr/local/lib 下不能

第一种: 放在系统内可识别位置

在这里插入图片描述

第二种:手动指定

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

心惠天意

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

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

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

打赏作者

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

抵扣说明:

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

余额充值