Zach Caudle
Zach Caudle

Reputation: 145

Calling methods from multiple C files without custom header files, Makefile Linking

This has gotten a bit lost in translation so I am going to be more precise: we have classes recursion.c, fib.c, and countUp.c. from recursion.c we have to recursively call fib.c or countUp.c, decided by the input argument. I can't use header files and am only given that I must place prototypes:

int fib(int n);

and

void countUp(int n);

My Makefile

TAR = tar
COMPILER_FLAGS = -g -Wall -std=c99 -c
LINKER_FLAGS = -g -o
OBJS = recurse.o
C_FILES = recurse.c fib.c countUp.c
ASM_FILES = recurse.asm
TARGET_FILE = recurse
TARGET_TAR = PA5.tar

$(TARGET_TAR): $(TARGET_FILE)
    $(TAR) -cvf $(TARGET_TAR) $(C_FILES) $(ASM_FILES) $(TARGET_FILE) Makefi$

recurse.o: recurse.c
    $(C_COMPILER) $(COMPILER_FLAGS) $(C_FILES)

$(TARGET_FILE): $(OBJS)
    $(LD_LINKER) $(LINKER_FLAGS) $(TARGET_FILE) $(OBJS)

where fib and countUp class methods must be called recursively. The recursive.c file is considered our c driver. Do not create or implement any header files OTHER than those that are standard c headers (stdio.h, string.h, etc.). When I try to run this I get:

make
gcc -g -o recurse recurse.o
recurse.o: In function `main':
(file root location)/recurse.c:43: undefined reference to `fib'
(file root location)/recurse.c:46: undefined reference to `countUp'
collect2: ld returned 1 exit status
make: *** [recurse] Error 1

Any clue what is going on.

Original Question: I have multiple C files that I am combining into an executable. For example say I have math.c, the arguments are passed into it, and then if the input argument calls add it performs functions from add.c, if the argument calls subtract it will call functions from subtract.c, etc. The files are then compiled into a .o file, and then an executable is created. The issue I have is not being able to utilize header (.h) files. Is there any way to break into the separate classes or am I missing something? I really don't know exactly how to ask the question, jargon is pretty bad as far as C goes, sorry :(

I don't really get the idea of a driver I guess. (Not a device driver, she keeps telling us this is a c executable driver).

Upvotes: 0

Views: 1278

Answers (3)

user1222021
user1222021

Reputation:

If I understood correctly, what I think you need to do is add the following prototypes above any of the functions you define in recursion.c. The prototypes will allow you to call these functions from within any function inside recursion.c (In fact, including a header file is akin to copy-pasting all of the prototypes defined in the file, as @Justin and @EdS already pointed out)

int fib(int n);
void countUp(int n);

int main() {
 ...
}

Then you need to make sure that your project file includes the files recursion.c, fib.c, and countUp.c - When you build your project, the linker will do its job and lookup the entry points in your compiled object files, and will proceed to assemble a single executable file.

What compiler are you using?

Upvotes: 2

Justin
Justin

Reputation: 86779

Including a header file is just a preprocessor directive to include the contents of that file at the location of the include. To achieve the same thing without a header file just copy and paste the code that you would have put in the header file into the top of each c file.

Of course this isn't very maintainable as if you want to change that contents you need to change it in many files, hence why header files exist in the first place.

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124752

Since this is homework and considering the fact that you have told us that A) You have no header files to use, and B) you have not been instructed to utilize the extern keyword, it seems to me that your only choice is to include the .c files themselves:

#include "add.c"
#include "subtract.c"
/* etc... */

int main()
{
    // use functions defined in "add.c", "subtract.c", etc.
}

Note that this is bad form as you are including the implementation instead of the interface and likely pulling in a bunch of stuff you don't want or need. If that doesn't answer your question then there is something, some instruction from your teacher, missing in the question.

Upvotes: 0

Related Questions