Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare global variable in C?

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

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

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

...

How to declare the global variable so it is visible in the included file? Can you help to fix it?

like image 312
John Boe Avatar asked Jan 06 '15 09:01

John Boe


People also ask

Can you declare global variables in C?

The C compiler recognizes a variable as global, as opposed to local, because its declaration is located outside the scope of any of the functions making up the program. Of course, a global variable can only be used in an executable statement after it has been declared.

What is global variable in C with example?

The variables that are declared outside the given function are known as global variables. These do not stay limited to a specific function- which means that one can use any given function to not only access but also modify the global variables.

How do you define a global variable?

Global variables, as the name implies, are variables that are accessible globally, or everywhere throughout the program. Once declared, they remain in memory throughout the runtime of the program. This means that they can be changed by any function at any point and may affect the program as a whole.

How do you declare a variable in C?

type variableName = value; Where type is one of C types (such as int ), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to the variable.


1 Answers

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

Later make sure you define in (exactly) one of the compilation units.

/* something.c */
int globali = 42;  /* Definition for linking */
like image 197
Mohit Jain Avatar answered Oct 13 '22 11:10

Mohit Jain