Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:59:38+00:00 2026-06-06T21:59:38+00:00

I have global variables that I define in the Utility namespace. This utility is

  • 0

I have global variables that I define in the Utility namespace. This utility is included in several files and it looks like this:

#ifndef _UT_
#define _UT_
namespace UT {
  std::string PLATFORM_LINUX_NAME = "linux";
  std::string  PLATFORM_MACOSX_NAME = "macosx";
  std::string  PLATFORM_WINDOWS_NAME = "windows";
  #if defined(OS_WIN)
    int PLATFORM = OSTYPE::PLATFORM_WINDOWS;
  #elif defined(OS_LINUX)
    int PLATFORM = PLATFORM_LINUX;
  #elif defined(OS_APPLE)
    int PLATFORM = PLATFORM_MACOSX;
  #endif
};

When I include this file in for example files A.h and B.h and C.h, I’m getting a compiler warning that says:

warning LNK4006: "int UT::PLATFORM" (?PLATFORM@UT@@3HA) already defined in A.obj; second definition ignored
warning LNK4006: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > UT::PLATFORM_LINUX_NAME" (?PLATFORM_LINUX_NAME@UT@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in A.obj; second definition ignored

What is the best way that does not involve creating a class for solving this?
Or is creating a UT class the only way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:59:41+00:00Added an answer on June 6, 2026 at 9:59 pm

    Define the variables in a single .cpp file and declare them in the .h file. In UT.h:

    namespace UT
    {
        extern const std::string PLATFORM_LINUX_NAME;
        extern const std::string PLATFORM_MACOS_NAME;
        extern const std::string PLATFORM_WINDOWS_NAME;
        extern const int PLATFORM;
    }
    

    in UT.cpp:

    namespace UT
    {
        const std::string PLATFORM_LINUX_NAME   = "linux";
        const std::string PLATFORM_MACOS_NAME   = "macosx";
        const std::string PLATFORM_WINDOWS_NAME = "windows";
    
        #if defined(OS_WIN)
        const int PLATFORM = OSTYPE::PLATFORM_WINDOWS;
        #elif defined(OS_LINUX)
        const int PLATFORM = PLATFORM_LINUX;
        #elif defined(OS_APPLE)
        const int PLATFORM = PLATFORM_MACOSX;
        #endif
    
    
    }
    

    I added const qualifier as these appear to be constant values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many js methods that uses global variables like: function DoSomething() { var
I have a global variables that I am using as a default variable. Depending
I have read in various places that having variables with global scope, i.e. a
Suppose I have a variables in the global scope. Suppose I wish to define
I have: lib/ lib/__init__.py lib/game.py In __init__.py I'd like to define a variable that
I have a 3rd party JavaScript application that declares several global functions and needs
We have an HTML page with some global JavaScript variables. We have some utility
I have some global variables (classes) that as I understand have created before main()
I have a global variable that is an instance of my custom class. How
I have a global variable averagel that I am trying to manipulate in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.