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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:09:49+00:00 2026-05-28T07:09:49+00:00

I am just a beginner in C++. I am trying to construct some header

  • 0

I am just a beginner in C++. I am trying to construct some header file header.h, but the output is always like the following:

/tmp/ccTmZKXX.o: In function `main':
main.c:(.text+0x13): undefined reference to `func'
collect2: ld returned 1 exit status

Could you please help me to see whether my way of using header file is correct or not? Thanks a lot!

Main code (main.c):

#include "stdio.h"
#include "func.h"

main() {
    double a = f(2.3);
    printf("a=%f\n", a);
}

where func.c contains:

double func (double x) { return x ;}

where func.h contains:

double func (double);

And I compile with:

gcc -o main main.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-05-28T07:09:50+00:00Added an answer on May 28, 2026 at 7:09 am

    There are multiple problems here:

    1. The C++ compiler in the GCC (GNU Compiler Collection) is g++, not gcc; the latter is the GNU C Compiler.

    2. The code in main.c is a (not very good) C program and not a C++ program. C99 outlawed the implicit int return type; C++ essentially never allowed it.

    3. Your question uses a function f; your compilation error references func. This means you did not show us exactly the code you tried to compile.

    4. The standards say #include <stdio.h>; you should too.

      #include <stdio.h>
      #include "func.h"
      
      int main()
      {
          double a = func(2.3);
          printf("a=%f\n", a);
      }
      

      NB: This is a perfectly good C program if you work with C99. In C89, you are expected to return a value from main() rather than ‘fall off the end’. C99 follows C++98 and allows falling off the end as equivalent to an explicit return 0;. I tend to put the explicit return(0); (usually with, sometimes without, the parentheses – the compilers don’t mind either way) anyway. (I compile C with -Wstrict-prototypes; to get a warning-free compilation, I write int main(void), which also works with C++ but the void is not necessary there.)

    5. The header is OK, though you will learn in due course about header guards and other paraphernalia that make headers more reliable.

      #ifndef FUNC_H_INCLUDED
      #define FUNC_H_INCLUDED
      
      extern double func(double a);
      
      #endif /* FUNC_H_INCLUDED */
      

      The extern is not mandatory. I tend to use it, but there are many who do not.

    6. The source file defining the function should include the header to ensure that the function definition is consistent with the declaration. All code that uses the function should include the header so that there is a prototype in scope. This cross-checking is crucial for reliability. C++ requires prototypes in scope before a function is used; it does not demand a prototype in scope before the function is defined (but it is good practice to do so). It is strongly recommended in C that you have a prototype in scope before defining an external (non-static) function. You can use -Wmissing-prototypes with C code and GCC to spot such problems, but the option is not valid for G++.

      #include "func.h"
      
      double func(double x) { return x; }
      
    7. Since this is a C++ question, we could consider inlining the function in the header. Indeed, C99 also supports inline functions. However, we can ignore that for the time being.

    8. Since this is a C++ question, we could consider that using <stdio.h> is not good because it is not type safe. You might be better off using <iostream> et al, not least because they are type safe.

      #include <iostream>
      #include "func.h"
      
      int main()
      {
          double a = func(2.3);
          std::cout << "a=" << a << std::endl;
      }
      
    9. The correct compilation requires both the main program and the function it invokes, so you might write:

      g++ -o main main.c func.c
      

      Or, if you are compiling it in C, then:

      gcc -std=c99 -o main main.c func.c
      

      Note that the -std=c99 is necessary to ensure that the absence of return in main() is acceptable.

      Note that there are several extensions in use for C++ source code, including .C, .cpp and .cxx, all of which are accepted by G++ (as well as .c).

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

Sidebar

Related Questions

This may sound like a stupid question but I'm a beginner not just to
I was just trying to learn the syntax of the beginner things, and how
I am just a beginner in php. I am trying to print the array
I'm a beginner at PHP, and I'm still trying to work out proper file
I'm sure this is a completely obvious beginner question, but trying to find answers
I am trying to mix QooXDoo and jquery , jqGrid but i am just
Just trying to ping up some experienced Thread gurus out there...trying to learn more
What I'm trying to do is quite simple but as a beginner I'm getting
i'm a beginner at CSS and trying to do a NETTUTS , but there's
I'm just a beginner, What's wrong with my code, I'm trying to experiment on

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.