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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:44:07+00:00 2026-06-03T00:44:07+00:00

I need to use a static fstream in multiple source files. However I can

  • 0

I need to use a static fstream in multiple source files. However I can only use it from a single file and not from the others. Furthermore, its use in the other files does not give any error, it just does nothing.

This is the code:

/// log.h
#ifndef LOG_H
#define LOG_H
#include <fstream>
static std::ofstream ofs;
#define LOG(level) ofs << level << ": "
#endif

/// test.cpp
#include "log.h"
#include "other.h"
int main()
{
   ofs.open("file.log");
   LOG(5) << "Test log 1" << std::endl;      // OK
   OtherFunc();
}

/// other.h
#ifndef OTHER_H
#define OTHER_H
extern int OtherFunc();
#endif

/// other.cpp
#include "other.h"
#include "log.h"
int OtherFunc()
{
   LOG(5) << "Test log 2" << std::endl;      // Nothing
}

This is the generated file:

5: Test log 1

Thank you!

Platform:
Linux
g++ 4.5.1

  • 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-03T00:44:08+00:00Added an answer on June 3, 2026 at 12:44 am

    static here means you’re explicitly asking the compiler to give the file-scope variable std::ofstream ofs static linkage, which means it is visibly only in the current file.

    The twist is that you’re doing this in a header, which means every .cpp file including the header gets its own distinct instance of std::ofstream ofs. Only because you’ve given it static linkage can they all have distinct file-scope variables with the same name – otherwise there would be a name clash.

    So, in main.cpp, you open your local ofs and write to it.
    In other.cpp, you have your own local copy of ofs, but never open it … so the output doesn’t go anywhere, and certainly not to file.log.


    The other answers are correct, that changing the header declaration to extern std::ofstream ofs; will allow all the .cpp files to share a single object called ofs, and then you just need to put the instance in exactly one place (main.cpp would be fine).

    It might be simpler, and cleaner, to make the LOG(level) an out-of-line function call though; then the output stream could live in a log.cpp with the function definition, and nobody else needs to see it.

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

Sidebar

Related Questions

I need to explain myself why I do not use static methods/propertis. For example,
I'm looking for an easy to use static file service that I can use
This is a design question: when do I need to create/use a static method
I need a static variable that points to itself to use when referencing the
I have a use case where I need to call a (non-static) method in
So, I need use this event so I can navigate trought blog posts. I
I need to use NSImage which appears need to be imported from <AppKit/AppKit.h> .
I need to use a static html page as an MVC3 razor masterpage. It
For VC, I can write a DEF file and use the 'NONAME' directive to
Given that I'm allowed to use static JS and HTML tags (only those supported

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.