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
There are multiple problems here:
The C++ compiler in the GCC (GNU Compiler Collection) is
g++, notgcc; the latter is the GNU C Compiler.The code in
main.cis a (not very good) C program and not a C++ program. C99 outlawed the implicitintreturn type; C++ essentially never allowed it.Your question uses a function
f; your compilation error referencesfunc. This means you did not show us exactly the code you tried to compile.The standards say
#include <stdio.h>; you should too.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 explicitreturn 0;. I tend to put the explicitreturn(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 writeint main(void), which also works with C++ but thevoidis not necessary there.)The header is OK, though you will learn in due course about header guards and other paraphernalia that make headers more reliable.
The
externis not mandatory. I tend to use it, but there are many who do not.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-prototypeswith C code and GCC to spot such problems, but the option is not valid for G++.Since this is a C++ question, we could consider inlining the function in the header. Indeed, C99 also supports
inlinefunctions. However, we can ignore that for the time being.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.The correct compilation requires both the main program and the function it invokes, so you might write:
Or, if you are compiling it in C, then:
Note that the
-std=c99is necessary to ensure that the absence ofreturninmain()is acceptable.Note that there are several extensions in use for C++ source code, including
.C,.cppand.cxx, all of which are accepted by G++ (as well as.c).