The definition and reference problem of C + + global variables __c++

Source: Internet
Author: User

Recently in busy live555 project; Honestly, this is the first big project I've done. Although it was the first time I had done such a big project for such a long time, I felt that I had made no progress in this year; adding a global variable wasted my half a day of force. No more nonsense, cut to the chase.


Global variables, the earliest I read the C primer Plus is supposed to be called an outsider variable. is always declared with the keyword extern in a header file, the declared variable can then be referenced by any of the. cpp or. c files in the project, and there are two types of references, one is through an extern declaration reference, and one that contains the header file can be used directly. As an example:


Header file Ext.h

#ifdef _ext_h_

#define _ext_h_

extern int data;//Declaration

#endif


B.h

#ifdef _b_h_

#define _b_h_

B_out ();


#endif

File B.C

#include "Ext.h"

#include "B.h"

int data; Definition, you can assign a value, or you can assign a value only once

B_out () {

printf ("B.data =%d\n", data);//Assignment

}

Using File Main.c

#include "Ext.h"

#include "B.h"


int main () {

data=0;//Assign Value

B.out ();

}


It looks very simple, but because of this definition of the problem let me around a lot of detours, because the project is relatively large, I clearly defined, compile the error said no definition, sometimes more will say no statement, several times I suspect is makefile problem, the fact that there are some problems, but not the main problem, After I read a number of blogs, I finally know that the definition of the problem must be defined, although can only be defined once, but not arbitrary definition, but should be defined in a base file, this is the word I created, how to say this base file, for example, you compile a file main.cpp generate a main file, Then this main also relies on another base.cpp compiled generated BASE.O file to compile, then this base.cpp is the base file, according to the software is the dependency package. If you define it in main and then reference in base but not defined, the error is not defined, and if all definitions are repeated, the error is defined.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.