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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:11:09+00:00 2026-06-17T10:11:09+00:00

im having trouble linking .h and .c files, i’ve also read some threads regarding

  • 0

im having trouble linking .h and .c files, i’ve also read some threads regarding this problem and all of them is a bit vague and still i can’t fully grasp the concept of it, and im having a lot of linking problems, Say i have b.c and b.h which i will use in a.c, and im confused whether to include b.h both a.c and b.c cuz b.c itself needs to know the structure defined in b.h, i have some function which has its prototype in b.h and is defined in b.c which also use the structure in b.h, im am not including b.h in b.c cuz as what i know b.h is more like an interface to a.c which will use the functions in b.c… Here a more clear example

b.h file

typedef struct{
int x, y;
}myStruct;

void funct1(myStruct);
void funct2(myStruct);

b.c file

void funct1(myStruct x)
{
    //do something
}

void funct2(myStruct y)
{
     //do something
} 

a.c file

#include "b.h"

int main()
{
myStruct x;
  funct1(x);
  funct2(y);
return 0;
}

Executed the command in cygwin: gcc b.c a.c -g

Now the confusing part, i have a linking error wherein when b.c is compiled it can’t detect the structure and the prototypes in b.h. Cuz all i know is that b.h is used to link b.c from a.c but when both .c is compiled it seems that b.c can’t find its strucutre and prototypes,

Why didn’t i include b.h in b.c?
Answer: Cuz as what i know, b.h is already included in a.c and when i include it again in b.c, i’ll be doing double inclusions <— thats what i learn so far and i know there is #ifdef but it seems it won’t work, maybe i still don’t know how to use it, if you know please feel free to discuss this.

If you have any idea as to how to go about this feel free to tell me some.

there is a #ifdef directive but i can’t seem to have any idea how to do this.

NOTE: ASSUME THAT ALL ABOVE CODES IS SYNTACTICALLY CORRECT if there are any misspelled word please ignore, i’m only after the inclusions between .h and .c

  • 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-17T10:11:11+00:00Added an answer on June 17, 2026 at 10:11 am

    You do indeed need to #include b.h in b.c. Each file is compiled separately before the linker takes over, so it doesn’t matter that you have included b.h in a.c, because b.c is compiled by itself and has no idea about the contents of b.h unless you include it.

    Here’s an example of a #include guard

    // some_header_file.h
    #ifndef SOME_HEADER_FILE_H
    #define SOME_HEADER_FILE_H
    // your code
    #endif
    

    When some_header_file.h is included anywhere, everything in between the #ifndef and the #endif will be ignored if SOME_HEADER_FILE_H has been defined, which will happen on the first time it is included in the compilation unit.

    It is common practice to name the #define after the name of the file, to ensure uniqueness within your project. I like to prefix it with the name of my project or namespace as well, to reduce the risk of clashes with other code.

    NOTE: The same header file CAN be included multiple times within your project even with the above include guard, it just can’t be included twice within the same compilation unit. This is demonstrated as follows:

    // header1.h
    #ifndef HEADER_H
    #define HEADER_H
    int test1 = 1;
    #endif
    
    // header2.h
    #ifndef HEADER_H
    #define HEADER_H
    int test2 = 2;
    #endif
    

    Now let’s see what happens when we try to include the above two files. In a single compilation unit:

    // a.cpp
    #include "header1.h"
    #include "header2.h"
    #include <iostream>
    int main()
    {
       std::cout << test1;
       std::cout << test2;
    };
    

    This generates a compiler error because test2 is not defined – it is ignored in header2.h because HEADER_H is already defined by the time that is included. Now if we include each header in separate compilation units:

    // a.cpp
    #include "header2.h"
    int getTest2()
    {
       return test2;
    };
    
    // b.cpp
    #include "header1.h"
    #include <iostream>
    int getTest2(); // forward declaration
    int main()
    {
       std::cout << test1;
       std::cout << getTest2();
    };
    

    It compiles fine and produces the expected output (1 and 2), even though we are including two files which both define HEADER_H.

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

Sidebar

Related Questions

this is a hw problem, ive done all the coding but im having trouble
I'm having some trouble compiling/linking a set of classes, several of them dealing with
I'm a bit new to programming for iOS, and I'm having some trouble linking
I'm having some trouble linking properly against libraries in C. I'm sure this is
I'm having trouble with the linking of my files. Basically, my program consists of:
I'm having quite a bit of trouble linking a test project of FLTK I'm
I'm having trouble linking my program to a library. I've never done this before
I'm having trouble with some very basic static library linking in C. The library
I'm having trouble finding any information regarding the following warning when linking a dynamic
I am having trouble linking 2 object files one of which was generated from

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.