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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:20:36+00:00 2026-05-26T18:20:36+00:00

I have a 2 modules (.c files) and one .h header file: file1.c: #include

  • 0

I have a 2 modules (.c files) and one .h header file:

file1.c:

#include <stdio.h>
#include "global.h"

int main()
{
    i = 100;
    printf("%d\n",i);
    foo();
    return 0;
}

file2.c

#include <stdio.h>
#include "global.h"

void foo()
{
    i = 10;
    printf("%d\n",i);
}

global.h

int i;
extern void foo()

When I do gcc file1.c file2.c everything works fine and I get the expected output. Now, when I initialize variable ‘i’ in the header file to say 0 and compile again I get a linker error:

/tmp/cc0oj7yA.o:(.bss+0x0): multiple definition of `i'
/tmp/cckd7TTI.o:(.bss+0x0): first defined here

If I just compile file1.c (removing call to foo()) with the initialization in the header file i.e. gcc file1.c, everything works fine. What is going on?

  • 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-26T18:20:37+00:00Added an answer on May 26, 2026 at 6:20 pm

    There are 3 scenarios, you describe:

    1. with 2 .c files and with int i; in the header.
    2. With 2 .c files and with int i=100; in the header (or any other value; that doesn’t matter).
    3. With 1 .c file and with int i=100; in the header.

    In each scenario, imagine the contents of the header file inserted into the .c file and this .c file compiled into a .o file and then these linked together.

    Then following happens:

    1. works fine because of the already mentioned “tentative definitions”: every .o file contains one of them, so the linker says “ok”.

    2. doesn’t work, because both .o files contain a definition with a value, which collide (even if they have the same value) – there may be only one with any given name in all .o files which are linked together at a given time.

    3. works of course, because you have only one .o file and so no possibility for collision.

    IMHO a clean thing would be

    • to put either extern int i; or just int i; into the header file,
    • and then to put the “real” definition of i (namely int i = 100;) into file1.c. In this case, this initialization gets used at the start of the program and the corresponding line in main() can be omitted. (Besides, I hope the naming is only an example; please don’t name any global variables as i in real programs.)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different modules that need access to a single file (One will
I have some large csv file, with about 200 header names (the first one
I have a big project compiled into libProject.so-file (shared library), I made some modules
I have this class in one file and item class in another file in
I want to have a number of files imported in a general python file
I have on occasion used a header file purely to import a group of
I have a header file containing a lot of small inline functions. Most of
i have a module (a single .py file, actually), with a class called HashedDir.
I have a file that I want to read in using the File::Slurp module
I have a ruby file as follows: module Example class Myclass def t_st Hello

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.