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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:45:57+00:00 2026-06-15T13:45:57+00:00

I would like to share a C++ struct between two files (in context of

  • 0

I would like to share a C++ struct between two files (in context of Android-NDK programming). In the first one the struct is initialized and in the other one it is finally used.

So I define the struct in a header file struct.h (which I include in both .cpp files) and declare it as a static variable:

struct A {
   int v;
   A(){
    v = 0;
   }
}

static A structA;

Then I assign a value to it in the first i.cpp-File e.g.: A.v = 5. But when I call it in the second one j.cpp it`s still 0.

The whole process looks like this:

Java Code -> call i.cpp and assign value -> Java Code -> call j.cpp and read value -> wrong

but

Java Code -> call i.cpp and assign value -> Java Code -> call i.cpp and read value -> correct

  • 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-15T13:45:58+00:00Added an answer on June 15, 2026 at 1:45 pm

    First, what you want to share is an object, not a struct. The struct defines the type of the object.

    Second, static means “don’t share this with other translation units”. I’ll bet you stuck that in there because without it you got a complaint from the linker about duplicate definitions.

    The way to do that is to declare the object in the header and define it in only one place. This goes in the header:

    extern A structA;
    

    And this goes in the source file where you want to initialize it:

    A structA;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I would like to share code between multiple assemblies. This shared code will
So I'm creating an array called fruits which I would like to share between
I would like to share data between the objects. Actually, for example, there is
I would like to share a common document format between iOS and OSX. Note
I have some common code that I would like to share between pages and
I have some pdf files in public/data I would like to share files to
I would like to share my Oracle SQL Developer configuration across my several computers
I am developing an app in which i would like to share my data
My problem is: I have 3 procs that would like to share config loaded
I would like to know if there is a method to create share buttons

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.