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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:52:59+00:00 2026-06-08T01:52:59+00:00

I want some shared variables should be accessed among source files, main.c and second.c

  • 0

I want some shared variables should be accessed among source files, main.c and second.c and my header file is all.h defined the shared data type,

#ifndef ALL_H
#define ALL_H
struct foo {
    double v;
    int i;
};

struct bar {
    double x;
    double y;
};
#endif

main.c is given below

/* TEST*/
#include "all.h"
#include "second.h"

int main(int argc, char* argv[])
{
    struct foo fo; // should be accessed in second.c
    fo.v= 1.1;
    fo.i = 12;

    struct bar ba; // should be accessed in second.c
    ba.x= 2.1;
    ba.y= 2.2;

    sec(); // function defined in second.c

    return 0;
}

second.h is given below

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

int sec();

second.c is given below

#include "second.h"

extern struct foo fo;
extern struct bar ba;

int sec()
{
    printf("OK is %f\n", fo.v+ba.x);

    return 0;
}

I thought i have all the declaration and include the headers. But when i compile

    gcc -o main main.c second.c 

or 

    gcc -c second.c
    gcc -c main.c
    gcc -o main main.o second.o

It will give some error like

second.o: In function `sec':
second.c:(.text+0x8): undefined reference to `fo'
second.c:(.text+0xe): undefined reference to `ba'
collect2: ld returned 1 exit status

I think somewhere of the use of extern was wrong or i use the gcc incorrectly?

  • 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-08T01:53:00+00:00Added an answer on June 8, 2026 at 1:53 am

    The problem is with the scope. Your variables (fo & ba) have local scope as they are declared within main.So, their visibility is restricted to within main function. Please make them global variables and it should work.

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

Sidebar

Related Questions

I have some shared code I want to reuse in my Xcode projects. The
I want some code to be triggered every second. Usually, I'd create a Timer
I want private variables to be constructed for each child and not shared. (That's
today I wanted to use some variables in my shared/menu partial. I know that
I want to share some image Icons between two applications. I stored the icons
I want to share some modules between a python GAE project, and another python
I have a PHP page and I want to share some data between pages
As the title, I want to abstract some function and share them between different
I want some examples of C preprocessor directives, such as: #define pi 3.14 #define
I want some of the goodies in a ListView, like being able to use

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.