1

I have encountered a problem when calling my freeHeap function inside of sortComp.c

I am calling it as such

heapRef myHeap = buildHeap(numData, heapSort, numData);     

freeHeap(myHeap);

When compiling I recieve the error "Undefined reference to 'freeHeap'"

I am including heap.h and inside of heap.h i have declared freeHeap

void freeHeap(heapRef *);

I am compiling it as such

gcc -o sortComp sortComp.c insertionSort.c heap.c insertionSort.h heap.h

It is defined in heap.c as

void freeHeap(heapRef *pH){
   heapRef H = *pH;
   free(H->data);
   free(H);
}

FIXED:

I changed the calling of freeHeap(myHeap);

to

freeHeap(&myHeap);

and it stopped complaining

2 Answers2

0

Use this in the declaration of your function freeHeap() in heap.h :

  extern void freeHeap(heapRef*);

Edit Jesus Christ, your argument to freeHeap() doesn't match with the prototype's, just as your declaration doesn't match with your definition.So your argument needs to be of type heapRef* instead of heapRef.In your program myHeap is of type heapRef while you are supposed to pass heapRef*.Use this:

freeHeap(&myHeap);
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
  • But you have to use `extern` nevertheless when you have declaration in one file and definition in another. – Rüppell's Vulture May 03 '13 at 00:25
  • and use `heapRef*` in the declaration, not `heapRef` – Rüppell's Vulture May 03 '13 at 00:25
  • why would freeHeap() require being extern when my heap.h file has 6 other functions that dont have extern and can be called without any problem in another file – user2345160 May 03 '13 at 00:26
  • It's something with your command on the command line that you are missing.Since I use a GUI to compile stuff,I see no error. – Rüppell's Vulture May 03 '13 at 00:26
  • and extern didnt fix my problem either – user2345160 May 03 '13 at 00:26
  • yea all of the other functions in heap.h can be called fine and work perfectly – user2345160 May 03 '13 at 00:30
  • @user2345160 Ok, the problem might have been solved,but I just don't understand why your compiler gives THAT error!!It should point out to the type mismatch of the argument to `freeHeap()` and it's `declaration`,but instead it says it `freeHeap()` itself is undefined.Don't you agree? – Rüppell's Vulture May 03 '13 at 00:44
  • You said, 'You see, though you are including the header file heap.h in your program, and though this header file contains the declaration for freeHeap(),it has no "connection" with the definition in another file heap.c as long as you don't declare the function as extern.' But this seems to contradict that claim: http://stackoverflow.com/questions/3367124/should-functions-be-made-extern-in-header-files – rliu May 03 '13 at 00:45
  • @roliu I would go by Jonathan Leffler's answer: `Functions declared in headers are normally (unless you work really hard) extern. Personally, I prefer to see the explicit keyword there - but the compiler doesn't need it. It reminds the readers that they are extern, and since humans are more fallible than computers, I find the reminder helps.` I too prefer the explicit keyword.But in the OP's question, the real source of the trouble was very simple--He was passing an argument of type `heapRef` instead of the expected `heapRef*`.But still, the nature of the error displayed is confusing. – Rüppell's Vulture May 03 '13 at 00:49
  • @roliu Thanks for pointing it out though,I won't be `that` fussy about `extern` for `functions` anymore.But for variables,one has to!! – Rüppell's Vulture May 03 '13 at 00:52
  • I agree that `extern` is nice for readability. But suggesting that the compiler can't figure out where that function is is false (at least according to those answers). All I was suggesting is that you remove that claim from your answer. Since you did, we're in good order :) – rliu May 03 '13 at 01:12
  • @roliu How did you immediately get the link to that question?Was that in your list of favorites?Please tell me so that I can make use of StackOverflow better.I mean this link -- http://stackoverflow.com/questions/3367124/should-functions-be-made-extern-in-header-files – Rüppell's Vulture May 03 '13 at 02:47
  • @Rüppell'sVulture I used [google](https://www.google.com/search?q=extern+functions+headers&aq=f&oq=extern+functions+headers&aqs=chrome.0.57j62l3.6345j0&sourceid=chrome&ie=UTF-8). Or if you're afraid of the google overlords then the third link using [duckduckgo](https://duckduckgo.com/?q=extern+functions+headers) also seems sufficient – rliu May 03 '13 at 05:49
  • @Rüppell'sVulture I think anyone serious about learning a language would just refer to the specs though. Like if you are using `gcc` you'd refer to the spec here: http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Standards.html#Standards. It says there that it supports various C standards (although with varying levels of completion) and you can refer to those C standards to know what your compiler will actually do. But that's a lot of work... and only worth it if you're serious about knowing your language of choice. SO is generally good about accuracy if an answer gets enough exposure. – rliu May 03 '13 at 06:00
  • @roliu I wouldn't like to go **that** deeply into it,even though I would like to.I have time constraints,and have to work on my Discrete Maths too.Not to mention I have to start `Analysis of Algorithms` from scratch.Let's hope StackExchange is of help in those subjects too. – Rüppell's Vulture May 03 '13 at 06:04
0

Please take the .h files out of your compile command. It should probably be:

gcc -o sortComp sortComp.c insertionSort.c heap.c
Dr.Tower
  • 985
  • 5
  • 11
  • I only put them in there because it wasnt working without them. I tried it again without them though and it still is giving me the same error – user2345160 May 03 '13 at 00:25
  • Have you tried to compile each of the files into .o files first? i.e. gcc -c sortComp.c; gcc -c insertionSorce.c; gcc -c heap.c; – Dr.Tower May 03 '13 at 00:26