创建dll

在制作dll的时候,如果全局变量不导出,而函数调用中,包含了全局变量,那么会出现全局变量没有值的问题。

add.c

#pragma once

//强制无签名utf-8
#include "add.h"

pStu student;//全局变量

int add(int a, int b){
    return a + b;
}

void test1(pStu p){

    if (p == NULL){
        printf("------1------
");
    }
    //全局变量怎么处理
    if (student == NULL){
        printf("------2------
");
    }
    else{
        printf("ok
");
    }
}
View Code

add.h

#ifndef _ADD
#define _ADD
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef SAMPLEDLL_EXPORTS
#define DLL_DECLARATION __declspec(dllexport) 
#else
#define DLL_DECLARATION __declspec(dllimport)
#endif
#define  aMAX(a, b)  ((a)>(b) ? (b) : (a))
    DLL_DECLARATION int add(int a, int b);
    DLL_DECLARATION void dis();
    

    int num;
    typedef struct {
        int age;
        char *p;
    }STU,*pStu;
    extern  pStu DLL_DECLARATION student;//导出全局变量
    DLL_DECLARATION void test1(pStu p);
#ifdef __cplusplus
}
#endif


#endif
View Code
 
调用:
test.c
#include <stdio.h>

#include "add.h"
#pragma comment(lib,"makeADll.lib")

int func(int n){
    printf("%d
", num);
    return n;
}

int main(){
    num=add(1, 1);
    int a = func(num);
    student = malloc(sizeof(STU));
    student->age = 10;
    test1(student);
}
View Code
 
具体参照:http://www.cnblogs.com/lidabo/p/3501358.html
下面导出类,还没有实践。
类:在dll中定义:
在定义的时候用 class _declspec(dllexport) classname{
}
在类中引用的时候用
加入类定义头文件:#include “classname.h”
Class _declspec(dllimport) classname 来导入类
 
http://blog.csdn.net/candyliuxj/article/details/7853938
 
原文地址:https://www.cnblogs.com/yuguangyuan/p/5923097.html