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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:51:47+00:00 2026-06-07T05:51:47+00:00

I have 3 files in a linux static lib project where I’m having a

  • 0

I have 3 files in a linux static lib project where I’m having a link problem with a static field that I want to use in two class method implementation files. I have class1.h, class1main.cpp and class1utils.cpp. class1.h defines a class called class1 and has all static methods and a static field in it called pDb. The .cpp files define the implementation of the class1 methods split into two files.

In class1.h, I declare the static:

class class1    {
public:
    static Database * pDb;
    ...
}

In class1main.cpp I define the actual static at the top:

Database * class1::pDb;

So, I thought I could just do the following in class1utils.cpp:

extern Database * class1::pDb;

But when I make the project and bring in the lib, pDb is multiply defined at link time. Can someone tell me how I can get reference the static in the second source file so I can use it (and it will link)?

If I move the methods in class1utils.cpp back into class1main.cpp, and get rid of class1utils.cpp the link works.

Thanks for any advice.
Corey.

  • 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-07T05:51:49+00:00Added an answer on June 7, 2026 at 5:51 am

    You should simply include class1.h. Nothing else is necessary to declare the variable.

    One reason for this is that if you could declare a static member variable without the class definition being present then the access specifier for the static member would be hidden. The compiler would be unable to check access permissions.

    Besides, you can’t even refer to a member of class1 (as in extern Database *class1::pDb;) without class1 having already been defined. If you try class class1; Database *class1::pDb; you should get an error that you’re using an undefined type class1. So the extern declaration is redundant.


    On another note, if you have a class with no non-static members that’s an indication you probably shouldn’t be using a class. C++ isn’t like Java where everything has to be in a class. In C++ you can simply declare free functions and variables, in a namespace if you want.

    In this case, then you’d use extern to declare the variable in class1.h instead of using static:

    // class1.h
    namespace class1 {
        extern Database *pDb;
    }
    
    // class1main.cpp
    Database *class1::pDb;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered the strange problem, that linux c++ compiler includes the files from
I have two files: osm.h and osm.cpp I tried to create static lib from
I am in a big problem ..i have compiled my c files using linux
In my home folder in Linux I have several config files that have rc
I need to edit XML files on a small Linux box that we have
Ok, I have two related lists on my linux box in text files: /tmp/oldList
I have a C#.Net console app code that transfers files between linux and windows
gcc 4.5.1 Fedora 14 Linux I have this static library that was build from
I have 3 tiny files which I use to make a static library and
I have some files which need to be sftp from machine B ( linux

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.