vs中建立动态链接库的步骤

本文详细介绍了如何在Visual Studio (VS) 中使用C和C++创建动态链接库(DLL)的步骤,包括创建测试系统工程、建立CDLL和CPPLDL工程,涉及添加源文件、配置库目录、编译和测试过程。
摘要由CSDN通过智能技术生成

探索C语言 、cpp 建立动态链接库的步骤。
无原理,看步骤。

1 建立“测试系统”工程

新建解决方案
控制台应用程序

1.1 在DynamicLinkLibrary 工程中添加testdll.cpp

testdll.cpp

1.2 配置库目录

所有配置
库目录-编辑

$(SolutionDir)$(Configuration)\

库目录

2 建立cdll 工程

cdll

2.1 添加cdll.h 文件

#ifdef DLLCDLL
#else
#define DLLCDLL __declspec(dllimport)
#endif

#ifndef __CDLL_H__
#define __CDLL_H__
#ifdef __cplusplus
extern "C"{
#endif
    DLLCDLL int add(int x, int y);
#ifdef __cplusplus
}
#endif
#endif

2.2 添加cdll.cpp 文件

#define DLLCDLL __declspec(dllexport)
#include"cdll.h"

int add(int x, int y)
{
    return x + y;
}

2.3 编译cdll 工程

编译
编译结果

2.4 测试cdll

编辑testdll.cpp 添加

#include"../cdll/cdll.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma comment(lib,"cdll.lib")
void testcdll()
{
    int res = 0;
    res = add(10, 11);
    printf("%d\n", res);
    system("pause");
}

void main()
{
    testcdll();
}

运行程序

运行结果

3 建立cppdll 工程

3.1 添加Person 类

Person.h

#ifdef DLLCDLL
#else
#define DLLCDLL __declspec(dllimport)
#endif
#pragma once
class DLLCDLL Person
{
public:
    Person();
    Person(const char *name, int age);
    void sayhello();
    ~Person();
private:
    char name[32];
    int age;
};

Person.cpp

#define DLLCDLL __declspec(dllexport)
#include "Person.h"
#include<string.h>
#include<stdlib.h>
#include<stdio.h>


Person::Person()
{
}

Person::Person(const char *name, int age)
{
    strcpy(this->name, name);
    this->age = age;
}
Person::~Person()
{
}


void Person::sayhello()
{
    printf(" name: %s,age: %d\n", this->name, this->age);
}

3.2 编译

3.3 测试cppdll

编辑testdll.cpp

#include"../cdll/cdll.h"
#include"../cppdll/Person.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma comment(lib,"cdll.lib")
#pragma comment(lib,"cppdll.lib")
void testcdll()
{
    int res = 0;
    res = add(10, 11);
    printf("%d\n", res);
    system("pause");
}

void testcppdll()
{
    Person p = { "zz", 25 };
    p.sayhello();
    system("pause");
}

void main()
{
    //testcdll();
    testcppdll();

}

运行

cppdll运行结果

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值