Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I can't reference the implement of function defined in .h file?

IDE: VS code

gcc version: 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

I new in C , now I have three files:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Get_para.h

#ifndef _GETPARA_H_
#define _GETPARA_H_
extern double C;
extern double dEarthAngularVelocity;
...
extern void calParameter();
#endif

and Get_para.c, which is the implement of Get_para.h

#include <math.h>
#define _USE_MATH_DEFINES
#define pi M_PI

double C = 3e8;
double dEarthAngularVelocity = 7.29210e-5;
...

void calParameter(){
...
}

then, I want to include Get_para.h in test.c and call calParameter function which is implemented in Get_para.c

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

int main(){
   calParameter();
   printf("%lf\n",dSemiMajorAxis);
}

I use ‘run code’ in VS,the command in terminal is:

if ($?) { gcc test.c -o test } ; if ($?) { .\test }

the output is:

C:\Users\bob\AppData\Local\Temp\ccuWLUIl.o:test.c:(.text+0xe): undefined reference to `calParameter'
C:\Users\bob\AppData\Local\Temp\ccuWLUIl.o:test.c:(.rdata$.refptr.dSemiMajorAxis[.refptr.dSemiMajorAxis]+0x0): undefined reference to `dSemiMajorAxis'
collect2.exe: error: ld returned 1 exit status

but I want to just include the "Get_para.h" then I can use the implement of them in "Get_para.c".I search this in google , others’ code didn’t work on my computer. Now I guess the problem is the parameters of gcc, but can’t figure out what is it or what knowledge of C I need to know to solve this problem.

>Solution :

You have to compile the implementation of the function too:

gcc Get_para.c test.c -o test

Consider learning a build system, like cmake.

#define _USE_MATH_DEFINES

For a macro like this to work, it has to be defined before any includes.

#define _USE_MATH_DEFINES
#include <math.h>
#define _GETPARA_H_

It’s not valid to define your macros with leading _ followed by an upper case letter. Such identifiers are reserved. For example, use:

#define GETPARA_H_
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading