How do I declare a global variable in C?

I am starting to use C. I have a problem to define a global variable. For example. platformID is used in install.c. I declared in main.c but still I got the error:

install.c | 64 | error: "platformID" uneclared (use in this function first) |

main.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>

      

...

install.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 OpenCL device\n");
        return false;
    }

      

...

How do I declare a global variable so that it is visible in the included file? Can you help fix this?

+3


source to share


4 answers


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

      

Later, make sure you define (exactly) one of the compilation blocks.



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

      

+6


source


Add one line

extern cl_platform_id platformID;

      



before referring to platformID

install.c.

+2


source


Use extern before using this variable in install.c. Then compile both files at the same time.

extern cl_platform_id platformID;

      

+2


source


add this to install.c before using this variable

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

      

+2


source







All Articles