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 8778567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:34:21+00:00 2026-06-13T19:34:21+00:00

There is a linker error in my SW. I am using the following structure

  • 0

There is a linker error in my SW. I am using the following structure based on h, hpp, cpp files. Some classes are templatized, some not, some have function templates. The SW contains hundreds of included classes…

Declaration:

test.h
#ifndef TEST_H
#define TEST_H

class Test
{
   public: 
        template <typename T>
        void foo1();

        void foo2 ();
};

#include "test.hpp" 

#endif

Definition:

test.hpp
#ifndef TEST_HPP
#define TEST_HPP

template <typename T>
void Test::foo1() {}

inline void Test::foo2() {} //or in cpp file

#endif

CPP file:

test.cpp
#include "test.h"

void Test::foo2() {} //or in hpp file as inline

I have the following problem. The variable vars[] is declared in my h file

test.h
#ifndef TEST_H
#define TEST_H

char *vars[] = { "first", "second"...};

class Test
{
    public: void foo();
};

#include "test.hpp"

#endif

and used as a local variable inside foo() method defined in hpp file as inline.

test.hpp
#ifndef TEST_HPP
#define TEST_HPP


inline void Test::foo() {
    char *var = vars[0];   //A Linker Error
}

#endif

However, the following linker error occurs:

Error   745 error LNK2005: "char * * vars" (?vars@@3PAPADA) already defined in main.obj

How and where to declare vars[] to avoid linker errors? After including

#include "test.hpp"

it is late to declare it…

As I wrote, the software contains a lot of cpp, and hpp files included each other (all includes have been checked). It is not possible to send the whole example…

The main.obj represents a file which contains the main class.

  • 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-13T19:34:23+00:00Added an answer on June 13, 2026 at 7:34 pm

    Declare vars in the header with extern linkage

    extern const char* vars[];
    

    and define it in exactly one source file

    const char* vars[] = {"foo", "bar"};
    

    Note the const, the conversion from string literals to char* is deprecated. The way you have it now, you’re violationg the One definition rule (You’re redefining vars in every translation unit your header is included in).

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

Sidebar

Related Questions

Consider following source files 1.cpp #include <iostream> using namespace std; struct X { X()
Using g++ and having linker errors. I have a simple program in split into
Is there a gcc flag to skip resolution of symbols by the compile-time linker
Is there any reliable and simple priority queue (linked list preferred, not necessary) implementation
find whether there is a loop in a linked list. Do you have other
Suppose you have two linked servers called Local and Remote respectively. Is there a
I have a AspxGridView which is linked to a DataSource. Below the GridView there
I'm using MinGw on Windows 7. The following simple program compiles fine, but the
I'm slightly confused with template specialization. I have classes Vector2 , Vector3 which have
In an old projet using symfony 1.4 There are an object Product linked to

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.