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

  • Home
  • SEARCH
  • 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 3241962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:15:34+00:00 2026-05-17T18:15:34+00:00

Compiling the file main.c with: gcc main1.c -o final gives me: /tmp/cc1cwhAP.o: In function

  • 0

Compiling the file main.c with:

gcc main1.c -o final

gives me:

/tmp/cc1cwhAP.o: In function `main':
main1.c:(.text+0xb): undefined reference to `hi'
main1.c:(.text+0x15): undefined reference to `hi'
collect2: ld returned 1 exit status

main1.c:

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

int main(void)
{
 hi = 1;
 printf("hi = %d",hi);
 return(0);
}

incl.h:

extern int hi;

What am I doing wrong? (Please be verbose with your answers)

Removing the extern keyword solves the issue, but I want to be able to use the hi variable in other source files.

  • 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-17T18:15:34+00:00Added an answer on May 17, 2026 at 6:15 pm

    Let’s forget about header files for a bit (after all their contents are inserted inside source files during compilation and they only exist during pre-processing). They’re only an easy way to use the same code in several source files (think copy/paste). So, in the interest of efficiency, that duplicated code should do nothing; or we risk doing something more than once.

    int x; /* puts aside a memory area with space for an int and call it x */
    extern int x; /* tell the compiler that x exists and it is an int */
    

    Which of the above does less? That’s the one to put in a header file. But as we’ve told the compiler x exists, we need to actually define it somewhere. The best place to define it is in a source file.

    As header files don’t do anything, they’re pretty much safe to insert into lots of source files.

    Now, back to header files. They’re usually linked to a specific source file (through their names: foobar.h is linked to foobar.c). This is a good method to identify where declarations in the header file are defined.

    /* foobar.h */
    extern int x;
    void myfoo(int);
    

    After seeing this header file, I’d expect foobar.c to contain the definition of x and myfoo.

    /* foobar.c */
    int x;                /* global x. BAD BAD BAD */
    void myfoo(int alpha) { x = !x; return x ? alpha : 0; }
    

    Why globals are bad?

    quote from http://c2.com/cgi/wiki?GlobalVariablesAreBad

    Really Bad Reasons to Use Global Variables

    • “I’m a slow typist. Globals save me keystrokes.”
    • “I don’t want to pass it around all the time.”
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am compiling from the command line with gcc -o output-file $(mysql_config --cflags) main.c
Compiling a file that uses OpenGL with Visual C++, when I try to include
Compiling a C++ file takes a very long time when compared to C# and
I have a Nant build file which executes NUnit after compiling the dll's. I
What harm can come from defining BOOST_DISABLE_ABI_HEADERS when compiling boost? From the boost file:
Here's a simple C file: #include <stdio.h> #include <stdlib.h> int main() { printf(hi there!\n);
I am using gcc -MM to generate prerequisites of dependencies rules for compiling source
I'm having problems compiling the following program. I'm using gcc -framework Foundation inherit8.1m and
Compiling this lines long int sz; char tmpret[128]; //take substring of c, translate in
When compiling my C++ .Net application I get 104 warnings of the type: Warning

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.