繁体   English   中英

如何在C中声明全局变量?

[英]How to declare global variable in C?

我开始使用 C。我在定义全局变量时遇到问题。 例如,在install.c使用了platformID 我在main.c声明,但仍然出现错误:

install.c|64|error: 'platformID' undeclared (first use in this function)

主文件:

#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

 cl_int err;//the openCL error code/s
 cl_platform_id platformID;//will hold the ID of the openCL available platform
 cl_uint platformsN;//will hold the number of openCL available platforms on the machine
 cl_device_id deviceID;//will hold the ID of the openCL device
 cl_uint devicesN; //will hold the number of OpenCL devices in the system

#include "include/types.h"
#include "include/gaussian.h"
#include "include/args.h"
#include "include/files.h"
#include "refu/Time/rfc_timer.h"
#include <stdio.h>

...

安装文件

#include "include/types.h"
#include "include/gaussian.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <sys/types.h> // mkdir
#include <sys/stat.h> // mkdir

#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

#define MAX_SOURCE_SIZE (1048576) //1 MB
#define MAX_LOG_SIZE    (1048576) //1 MB

// Install: build kernel
bool buildKernels(char ** path,FILES * files, int count )
{

    int c; // number of file

    if(clGetPlatformIDs(1, &platformID, &platformsN) != CL_SUCCESS)
    {
        printf("Could not get the OpenCL Platform IDs\n");
        return false;
    }
    if(clGetDeviceIDs(platformID, CL_DEVICE_TYPE_DEFAULT, 1,&deviceID, &devicesN) != CL_SUCCESS)
    {
        printf("Could not get the system's OpenCL device\n");
        return false;
    }

...

如何声明全局变量使其在包含的文件中可见? 你能帮忙解决吗?

/* a.h */
extern int globali;  /* Declaration for compilation */
/* Visible here */

稍后确保您在(完全)一个编译单元中定义。

/* something.c */
int globali = 42;  /* Definition for linking */

在 install.c 中使用该变量之前使用 extern。 之后同时编译这两个文件。

extern cl_platform_id platformID;

添加一行

extern cl_platform_id platformID;

在 install.c 中引用platformID之前。

在使用该变量之前在 install.c 中添加它

 // external declaration for compilation
 extern cl_platform_id platformID; // It is defined in main.c file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM