标签:
在制作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------\n"); } //全局变量怎么处理 if (student == NULL){ printf("------2------\n"); } else{ printf("ok\n"); } }
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
#include <stdio.h> #include "add.h" #pragma comment(lib,"makeADll.lib") int func(int n){ printf("%d\n", num); return n; } int main(){ num=add(1, 1); int a = func(num); student = malloc(sizeof(STU)); student->age = 10; test1(student); }
标签:
原文地址:http://www.cnblogs.com/yuguangyuan/p/5923097.html