Declare variable in namespace, define it in main, make it visible to all other files

A K

Using C++14, I'm trying to define a variable in a namespace where commonly used variables are stored (App::Common). The main function should be the one that sets it, since it gets set to argv[0]. Meanwhile I need the variable to be visible by all other classes/files. But I get the linker error shown below. Also, ideally I would like the variable to be const where only the main function would set it once.

common.hpp

#pragma once
#include <string>
namespace App{
    namespace Common{
        extern std::string appPath;
    }
}

main.cpp

#include "common.hpp"
#include "client.hpp"
#include <string>
int main() {
    App::Common::appPath = argv[0];
}

client.hpp

#include "common.hpp"
class Client {
    public:
    void printAppPath();
};

client.cpp

#include <iostream>
#include <string>
#include "common.hpp"
#include "client.hpp"
void Client::printAppPath() {
    std::cout << App::Common::appPath << std::endl;
}

I get the following error by the linker:

ld: main.o: in function `main':
main.cpp:(.text.startup.main+0x25): undefined reference to `App::Common::appPath[abi:cxx11]'
ld: Client.o: in function `Client::printAppPath()':
Client.cpp:(.text...): undefined reference to `App::Common::appPath[abi:cxx11]'
Vlad from Moscow

This

#pragma once
#include <string>
namespace App{
    namespace Common{
        extern std::string appPath;
    }
}

contains only declaration of the variable appPath without its definition.

Here

#include "common.hpp"
#include "client.hpp"
#include <string>
int main() {
    App::Common::appPath = argv[0];
}

there is used the assignment operator to assign a value tp the variable appPath as if it were already defined. However actually its definition does not yet exist.

You can define the variable in any module in any enclosing namespace of the namespace Common or inside the namespace. For example you could define it in client.cpp like

std::string App::Common::appPth;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Declare a variable across all files in Python

How to declare a global variable and access it in all the other modules in ReactJS?

how to make variable visible from other function in Django

How to define/declare xaml namespace in WPF?

Define variable before main

define val variable visible on catch

Django make variable visible

declare global namespace variable from TypeScript

How to make [std::operator""s] visible in a namespace?

Define & Iniitialise flash variable in main

Declare and define inside namespace - Not sure if it works because it's correct or by accident

The differences between initialize, define, declare a variable

can we declare a variable in #define directive in c?

Declare variable in lighthouse graphql files

Make files in a directory not visible to anyone

Swift how to make a variable global in a file, but not share to other files

How to make a global variable or be able to access it in other files

How do I define a variable in a namespace with "using namespace"?

Is there any other way to declare global variable in Extjs?

Python define variable with type of other variable

Define Gurobi variable based on other variable

How to make an array of objects visible to other functions

On item click, make other items visible

Using JToggleButton in java to make other buttons visible

codenameone downloading files/images visible to other apps

How to declare a constant that is visible to all modules' build.gradle file?

Is it possible to dynamically declare a default namespace from an external variable?

How to declare a dynamic routes scope/namespace variable in Rails?

Does Kubernetes have the option to have a main manifest file which references all the other manifest files