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

标签 c global-variables

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

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

ma​​in.c:

#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>

...

安装.c

#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 */

关于c - 如何在C中声明全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27796048/

相关文章:

C语言: passing info through a File . csv到使用链表的结构

crystal-reports - Crystal 报表中的全局变量

dataframe - 如何在 Julia 中全局使用 for 循环中的数组?

javascript - Javascript 的全局变量 (Acrobat Reader)

c - 无符号整数的舍入除法,无溢出

c - 通过串口发送文件

c - 设置语言环境时 Windows C 运行时 toupper 变慢

javascript - 在 JavaScript 中访问原始全局变量和属性

perl - 是否有可能欺骗 Perl 使用变量 %INC 并仍然保持全局 %INC 不变?

c - 按位与符号字符