FAQ Database Discussion Community


How to use global variables in multiple .cpp files?

c++,global-variables,extern,multiple-files
I have this simple program which tries to print my global variable in a separate file. I'm using the Visual Studio 2013 professional IDE. print.h #ifndef PRINT_H_ #define PRINT_H_ void Print(); #endif print.cpp #include "print.h" #include <iostream> void Print() { std::cout << g_x << '\n'; } source.cpp #include <iostream> #include...

Use of global variables in C++ application

c++,extern,globals
I’ve used global variables without having any noticeable problems but would like to know if there are potential problems or drawbacks with my use of globals. In the first scenario, I include const globals into a globals.h file, I then include the header into various implementation files where I need...

how do I declare constexpr extern

c++,extern,constexpr
is it possible to declare a variable extern constexpr and define it in another file? I tried it but the compiler gives error: declaration of constexpr variable 'i' is not a definition in .h extern constexpr int i; in .cpp constexpr int i = 10; ...

How to declare an extern array of template class objects with a forward declared template class in C++?

c++,templates,extern,forward-declaration
Consider the following existing code (which compiles and executes as expected): /* File foo.h */ extern const struct Foo bar[]; /* Definition in foo.cpp */ struct Foo { Foo(int i) : Foo(bar[i]) {} int x; }; I now want to change Foo to a template class, such that: template <typename...

How to declare and use global external variable across multiple source files?

c++,global-variables,extern
I want a global variable (object) that I can easily access whenever and wherever I wish. The problem is that my source code has multiple *.hpp and *.cpp files that share the same variable! I've searched and found similar questions here but somehow they didn't solve my problem. With keyword...

Is it legal / good to put function prototype of a function defined in a different source (not header) file?

c++,static,extern,linkage,function-prototypes
I'm not sure whether my description describes the issue appropriately. I found this when I try to understand external linkage and internal linkage. Say I have a project containing 2 files: //A.cpp #include <iostream> void doSomething(); int main() { doSomething(); return 0; } //B.cpp #include <iostream> void doSomething() { std::cout...

CUDA6.5 Can't extern the value of texture

cuda,textures,extern
I had written a program follow the JackOlantem's answer in CUDA extern texture declaration but my result dosen't print the value of extern texture declaration P/s: how to add -rdc = true to enable external linkage? Result of the program ! http://i.stack.imgur.com/aGh3U.png Thanks for your help!!. kernel.cu compilation unit #include...

“extern” keyword usage

c++,extern
I have the following code snippet: #include<iostream> int main() { extern int a; printf("a = %d", a) } int a = 10; Above sample code print 10. My guess was it should print garbage value. Can someone explain this ?...

Linker error when calling a C function from C++ code in different VS2010 project

c++,c,visual-studio-2010,extern,lnk2001
I'm trying to include some C code I found in our C++ project. The function is defined like this in the C file. #ifdef __cplusplus extern "C" { #endif extern char *dtoa(double, int, int, int *, int *, char **); extern char *g_fmt(char *, double); extern void freedtoa(char*); #ifdef __cplusplus...

Program which behavior changes depending on classes it is linked against

c++,plugins,linker,extern
I do not think what I attempt is fancy enough to deserve the term "plugin" but here what I am trying to do: Given files a.h, a.cpp and main.cpp, I would like to create other files such as: g++ -o test main.cpp a.cpp b.cpp results in a test program doing...

Passing a struct to a template with extern const. What is the extern for?

c++,templates,struct,const,extern
I am asking myself why the following code works and what the specifier extern does when instantiating baz_instance: struct baz { int value; }; extern const baz baz_instance = {3}; template<baz const& b> int foo(){ return b.value; } int main(){ foo<baz_instance>(); return 1; } Why does the above code compile...

extern variable and array declare issue c++

c++,arrays,global-variables,const,extern
I have a problem with an extern variable and an array declaration with it. How to declare an array with global variable which located not in the declarable file. file1.cpp const int size = 10; mainfile.cpp extern const int size; void main() { int mas[size]; } int mas[size]; This line...

Linking an extern static array of structs is not working correctly

c,arrays,struct,extern,static-array
I am trying to link a statically defined array of structs. I am using the extern modifier to do so. When I print out the memory address of my extern struct, it differs from the location that it appears to be in the executable. Here is what I have: type.h:...

error: expected unqualified-id on extern “C”

c++,c,clang,extern
I have a cpp code in which I want to call a c function. Both compile well to .o files, but when the clang++ is executing for compilation, I receive the following error: file.cpp:74:12: error: expected unqualified-id extern "C" ^ The code in the cpp file is the following: void...

How to declare extern class pointers in C++?

c++,pointers,header,extern
The following is declared variable in the cpp file but I get an error, so I have conducted a research and I found that I need to declare it in the header file. therefore how can I declare and an extern class pointer in the header file extern AnimationController* g_pAnimationController;...

How does extern work in namespaces?

c++,namespaces,global-variables,const,extern
I'm running a simple program similar to what I found here. It's meant to reduce code bloat when including constants in multiple files. It does this by using const global variables within a namespace with their respective extern forward declarations. globals.h #ifndef GLOBALS_H_ #define GLOBALS_H_ namespace Constants { // forward...

Using “extern” command in c for sharing a variable [duplicate]

c,header,extern
This question already has an answer here: How do I use extern to share variables between source files in C? 13 answers I have the following codes: (1) extern_test.h: extern int give_something; (2) extern_test.c: #include <stdio.h> #include "extern_test.h" int give_something = 10; (3) extern_test2.c: #include <stdio.h> #include "extern_test.h" int...

.h file supposed to be interface with extern functions; how to define in .cc file that includes the header?

c++,extern
I have a function in my header file 1t.h that looks like the following: extern int dthreads_libinit(dthreads_func_t func, void *arg); I then want to implement that function in a separate file which includes 1t.h: int dthreads_libinit(dthreads_funct_t func, void* arg) { //Do something here... } I get these errors though: ‘int...

Objective C Global Variable

objective-c,global-variables,extern
Can anyone tell me where I'm going wrong here please. I have created an NSobject called BeaconData. The header file is: #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> #import <CoreBluetooth/CoreBluetooth.h> @interface BeaconData : NSObject @property (nonatomic, strong) NSMutableArray * jsonArray; @property (nonatomic, retain) NSString * bMajor; @property (nonatomic, retain) NSString * bMinor; @property...

using extern keyword for user defined types in c++

c++,extern
I want to use extern keyword for user defined types. This means I've declared object in one file and defined it in other file. I've read that extern keyword is used to declare the variable without defining it. extern keyword is useful when program is split into multiple source files...

extern variable in namespace c++

c++,namespaces,global-variables,extern
I have a question about the extern variable in namespace c++. Here is the .h file of the CBVR class namespace parameters { class CBVR { private: std::string database; public: CBVR(void); void initialize(const std::string &fileName); static void printParameter(const std::string &name, const std::string &value); }; extern CBVR cbvr; } The .cpp...

C - extern, static, include

c,static,extern,scoping
Scoping in C is confusing as hell. I have a variable: "int qwe". This var should be visible in one or more files - f1.c in this case, but not the another f2.c . Say i have: main.c, f1.c, f2.c, header.h main: call f1(); call f2(); header: #ifndef HEADER_INCLUDED #define...

Can't understand the declaration #3 in the Example of [basic.link]/6 C++14

c++,declaration,language-lawyer,c++14,extern
[basic.link]/6 The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace...

define a `extern` var in C language, xcode send a `Undefined symbols for architecture x86_64`

xcode,extern
This question is hard to describe, I'll try my best. Ok, here is the detail: I have a public head file, named header.h; define a extern int a; in header.h; create test1.c and include header.h; use var a in test1.c, but xcode give an error:Undefined symbols for architecture x86_64; I...