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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:52:44+00:00 2026-05-26T22:52:44+00:00

I am using gcc for compiling a number of .c files. Lets say following

  • 0

I am using gcc for compiling a number of .c files. Lets say following is the case:

C files are:

main.c 
tree.c

header file is:

 tree.h

I have declared some golbal variables in tree.h. Lets say following is the global varible with value assigned:

 int fanout = 5;

Earlier I had kept main() function in tree.c file. And there was no problem in linking. But now i want to keep the main function separate . I just moved the main function in the newly created .c file. Now the problem is, it
shows the linkage error:

 main.o error: fanout declared first time
 tree.o error: multiple declaration of fanout.

Please let me know how can i get rid of this problem.

Thanks in advance,.

  • 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-26T22:52:45+00:00Added an answer on May 26, 2026 at 10:52 pm

    When you include the header file which declares and defines int fanout in multiple source files, you break the One Definition Rule.
    As per ODR, there can be only one definition of an variable in one Translation unit(Header files+source file).
    To avoid it,
    You need to use extern keyword. Three simple steps:

    • Declare the extern variable

    In tree.h:

    extern int fanout;   
    
    • Define the variable

    Define the variable in one of the c files(tree.c).

    #include "tree.h"   
    extern int fanout = 5;
    
    • Use the variable

    Then you include tree.h in whichever source file you want to access fanout.

    In main.c:

    #include "tree.h"
    int main()
    {
        fanout = 10;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, SO. I have some code which I've made attempts at compiling using gcc,
I'm having problems compiling the following program. I'm using gcc -framework Foundation inherit8.1m and
I'm using Ubuntu 8.10 (Intrepid Ibex) and compiling C++ files with GCC, but when
I have been compiling my programs using GCC via the terminal, and im looking
I am compiling some C/C++ files using gcc. I noticed today a bug that
Our build system is using gcc to generate source file's header dependencies to *.d
I am using gcc -MM to generate prerequisites of dependencies rules for compiling source
I was using mysql++ library and compiling with GCC 3.3.4. That GCC version had
I'm using GCC to generate a dependency file, but my build rules put the
I'm using GCC; __FILE__ returns the current source file's entire path and name: /path/to/file.cpp

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.