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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:16:16+00:00 2026-05-25T03:16:16+00:00

I have two .c files and one .h file included from them both. In

  • 0

I have two .c files and one .h file included from them both. In the .h file, I have declared global constants. When building with gcc, I get linking problems, telling me that the constants are defined twice, even though I have

#ifndef __FOO
#define __FOO

const struct foo bar = ...

#endif

I get

/tmp/ccql6KF1.o:(.rodata+0x0): multiple definition of `bar'

However, compiling the very same code with g++ works perfectly. Is there some differences in the way C and C++ treats global constants declared in .h files? What approach should I consider?

Please note that all objects need to share the memory for the constants, since I have limited resources.

  • 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-05-25T03:16:17+00:00Added an answer on May 25, 2026 at 3:16 am

    You should declare the constant in a .h file and define it in a single .c file:

    bar.h:

    extern const struct foo bar;
    

    bar.c:

    #include "bar.h"
    
    /* do this in a single file */
    const struct foo bar = ...;
    

    Then include bar.h everywhere you want to access bar:

    something.c:

    #include "bar.h"
    
    void doSomethingWithBar() {
        struct *foo something = &bar;
        ...
    }
    

    Edit (by Shahbaz): The reason why this works and your code doesn’t work is that when you include a file, the contents of that file are copy pasted in place of #include (this is regardless of the file, you can include anything, including files with .h extension is just a convention). So when you say const struct foo bar; in a header file and include it in two files, it’s exactly like writing that line in both files, therefore defining the variable in both files and hence the link error.

    Your header protection also doesn’t work (the

    #ifndef __BAR_H__
    #define __BAR_H__
    ... header contents
    #endif
    

    ) because each of your source files are compiled separately, therefore when bar.h is included in one file and __BAR_H__ defined, when the next file is being compiled this definition of __BAR_H__ is lost.

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

Sidebar

Related Questions

I have created two JavaScript files. One file is "validators.js" and the other is
Say I have two files where there is one number per line File 1
I have two project, one which generates some artifacts from a WSDL file using
Or I have two files, each with one global initialization. One depends on the
I have two different modules that need access to a single file (One will
I have a file named file with three lines: line one line two line
I have two files one with senators who are retiring and one of complete
I have two template files: one for the form (loginFormTemplate) and one for the
Here I am trying to implement the jQuery I have two main files one
I have two files. file1 has the data like belowing containing only one column.

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.