0

I am writing C code (not C++) on a Linux system. I am trying to compile one executable from 2 .c files and 1 header file: main.c, sort.c, and sort.h.

main.c's first line reads: #include "sort.h"

inside sort.h, each function in sort.c is defined like this example: extern void aFunct(int param);

However, when I try to call a function in sort.c from main.c, I get the following error upon compilation: "undefined reference to 'aFunct'".

If i replace #include "sort.h" with #include "sort.c" my program works without issue. However, as I understand it, this is bad form and I would prefer to avoid this. Thanks for any help.

edit: I am compiling this with a makefile containing the following code:

all: index sort.o

sort.o: sort.c sort.h
    gcc -Wall -g -c sort.c

index: main.c sort.o
    gcc -Wall -g -o index main.c

clean:
    rm index
    rm sort.o

edit: I have fixed the problem. The problem did not stem from a misunderstanding of C files and how they link, but rather a misunderstanding of the makefile/gcc commands themself. My program works with the following makefile:

all: index sort.o

sort.o: sort.c
    gcc -Wall -g -c sort.c

index: main.c sort.o
    gcc -Wall -g -o index main.c sort.o

clean:
    rm sort.o
    rm index
3
  • 3
    Are you linking sort.o into your executable? Commented Mar 15, 2015 at 20:24
  • I added my makefile to my question so you know exactly how it is being compiled/linked Commented Mar 15, 2015 at 20:41
  • SleuthEye, turns out my problem was I THOUGHT I was linking sort.o to the executable, but I had a syntax error my makefile that stemmed from a fundamental misunderstanding of gcc syntax. Commented Mar 15, 2015 at 21:19

2 Answers 2

1

You should include #include "sort.h" in sort.c as well. You might be doing this already.

The important point is, you need to make sure that you are building both the .c files [main.c and sort.c]

Now both these obj files [main.o and sort.o] should be the input to linker.

As per my guess, you are not compiling sort.c , so linker is not able to see the implementation of the functions.

Sign up to request clarification or add additional context in comments.

2 Comments

sort.h is already included in sort.c. And unless I am having some crisis of understanding (very possible), I am compiling sort.c into an object file. If you want you can check my question again as it now contains the makefile
The issue was I was incorrectly linking sort.o. I figured it out on my own but this answer was pretty close so I decided to best answer it. Thanks man!
0

When you include one source code file to other (e.g. #include "sort.c") you can have a troubles if you include sort.c file to more than one c-file of the same project. But as I understand you just compile one c-file in which you include sort.c... so it works. But, better read some tutorials, for example How to Create Multi-Module Programs

1 Comment

I believe I am already doing everything that the link mentions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.