gacl
gacl

Reputation: 41

Accessing variables from another source file C++?

I'm trying to avoid classes, so keep that in mind.

I want to know how to access a variable from another .cpp file.

For whatever reason when I try to include the variable in my 'codeReferences.h' header file I get LNK 2005 errors. So I'm wondering without defining it within a globally used header file (causes errors) how can I access variables from another .cpp file.

All answers appreciated.

Upvotes: 3

Views: 11865

Answers (3)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145239

Presumably by ".cpp file" you mean "translation unit".

The holy C++ standard couldn't care less how the code is distributed in files, and has no notion of header files versus .cpp.

A variable to be defined in one translation unit and accessed in another, must have extern linkage. This is the default for namespace level non-const variables. However, an ordinary declaration will also define the variable, and then you run into the One Definition Rule that in general requires a single definition of each thing.

A solution is to write e.g.

extern int n_oranges;

in the translation unit where you just want to access the variable. The explicit mention of extern linkage, without an initializer, makes this a pure declaration.

You can have as many pure declarations of something, as you want.

It's best to put such a declaration in a header file, so as to avoid redundant multiple possibly not quite matching declarations. Note that in a header file it will be included as part of each relevant translation unit's text. The C++ compiler doesn't care where it comes from, just that it's there.


Relevant standardese:

C++11 §3.1/2:

A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification (7.5) and neither an initializer nor a function-body, it declares a static data member in a class definition (9.2, 9.4), it is a class name declaration (9.1), it is an opaque-enum-declaration (7.2), it is a template-parameter (14.1), it is a parameter-declaration (8.3.5) in a function declarator that is not the declarator of a function-definition, or it is a typedef declaration (7.1.3), an alias-declaration (7.1.3), a using-declaration (7.3.3), a static_assert-declaration (Clause 7), an attribute-declaration (Clause 7), an empty-declaration (Clause 7), or a using-directive (7.3.4).

Upvotes: 1

LNK2005 says that you have multiple definitions for the same symbol.

You need to understand the difference between a declaration and a definition:

  • When you declare a variable, then you only tell the compiler that there's a name (=symbol) and the type of that symbol.
  • When you assign a concrete value to a variable or provide an implementation for it then you define it (e.g. in case of functions).

You can have an arbitrary number of declarations for a symbol, but you must only have one definition for it. See this thread too: What exactly is One Definition Rule in C++?

If you want to use the same symbol in several .cpp files (referring to the same object), then you need to use the extern keyword in the header file:

Example:

/** header.h **/
extern int i;

You can include this header in any number of .cpp files to introduce the symbol i. You must specify a definition for i only in one .cpp:

/** code.cpp **/
#include "header.h"
int i; // this is a definition, even if you do not initialize here.

Upvotes: 7

Mrkillius24
Mrkillius24

Reputation: 1

Now the first thing is you should be careful you don't define things multiple times.

Second you should at least have read a tutorial on the basics of the c++ programming language.

I know I may sound harsh but a simple tutorial could help you understand the language a lot better than you currently do.

To be able to access variables from a different source file you can include them using their extension. For better practices most developers use headers to define the code , and the source to actually make it function to be able to easily debug and change things without having massive files to go through.

Upvotes: 0

Related Questions