I am using Dev-C++ on windows vista. I have 3 files located in the same directory. They are:
- math_functions.h
- math_functions.c
- test3.c
math_functions.h code:
int sum (int x, int y);
float average (float x, float y, float z);
math_functions.c code:
int sum (int x, int y)
{
return (x + y);
}
float average (float x, float y, float z)
{
return (x + y + z) / 3;
}
test3.c code:
#include <stdio.h>
#include "math_functions.h"
main ()
{
int theSum = sum (8, 12);
float theAverage = average (16.9, 7.86, 3.4);
printf ("the sum is: %i ", theSum);
printf ("and the average is: %f \n", theAverage);
printf ("average casted to an int is: %i \n", (int)theAverage);
}
It fails to compile. The error message I get is:
C:\User开发者_JAVA技巧s\eSum\AppData\Local\Temp\ccKmdaaa.o(.text+0x3a) In function `main':
[Linker error] undefined reference to `sum'
[Linker error] undefined reference to `average'
C:\Users\eSum\AppData\Local\Temp\ccKmdaaa.o(.text+0x3a) ld returned 1 exit status
I use the same exact code compile in ubuntu(I run ubuntu using virtual machine i,e. vmplayer), it compiled without errors.
Do I need to set anything in Dev-C++ to compile the files?
Dev-C++ seems not to be linking math_function.c with test3.c when making text3.exe this is a configuration problem in Dev-C++ you most likely need to add math_function.c to the Dev-C++ project
The problem isn't with the header-file, but with your project settings. You have to add math_functions.c to the project so it gets compiled and linked with test3.c.
精彩评论