Question:
How To Work With Multiple C Files?
Kyle
2012-07-07 17:30:16 UTC
I know this is a ridiculously stupid question, but how do you work with multiple C files in compiling and linking them into one executable? I am making a program, and I have a file called randomNumber.h and another called randomNumber.c and another called main.c . In randomNumber.h, I have my random number function's prototype. In randomNumber.c, I have my random number function's implementation. In main.c, I pass parameters to my random number function, and I get errors about how my function was undefined. I am using Visual Studio 2012, all of this is in one project, and it is the only project in the solution, and I built the solution. How do you manage a C project split up between files? How do you split it up?
Four answers:
2012-07-07 17:47:01 UTC
Your header file will be an interface which contains the prototypes of all the functions you wish to export out of the relevant translation units. Try the following:



randomNumber.c:



#include

#include "randomNumber.h"



int randomnumber(void)

{

        return rand();

}



randomNumber.h:



#ifndef RANDOM_NUMBER_H /* Header guards prevent multiple inclusion. */

#define RANDOM_NUMBER_H



extern int randomnumber(void);



#endif



main.c:



#include

#include "randomNumber.h"



int main(void)

{

        printf("%d\n", randomnumber());

        return 0;

}



When you start working with multiple files it becomes necessary to start using a make utility to compile programs. I suggest using GNU make which you can get from the MinGW package. A simple makefile for this program would look something like this:



Makefile:



NAME := random



LDFLAGS :=



OBJ := \

        randomNumber.o \

        main.o



all: $(OBJ)

        $(CC) -o $(NAME) $(CFLAGS) $(OBJ) $(LDFLAGS)



clean:

        $(RM) $(OBJ) $(NAME)



It's important to note that you must use the tab character instead of spaces for indentations when writing Makefiles.



edit:

@Ratchetr: The problem with VS project files is that they're not portable to other environments. Since there are make utilities for most modern platforms (I believe VS has one called nmake) I reckon it's the simplest and most efficient method for compiling C programs. A better option would be to use CMake or SCons, but the issue with those is they're a whole other level above makefiles.
Ratchetr
2012-07-07 17:56:34 UTC
Henni's answer is quite good. Follow that sample code and it should work.



But ignore everything after "When you start working with multiple files it becomes necessary to start using a make utility to compile programs".



You are using Visual Studio. You don't need to worry about make files. That's essentially what a project file is in VS. Writing make files is a skill most of us just don't need anymore. Thankfully.
David Karr
2012-07-07 17:34:00 UTC
main.c needs to include randomnumber.h. You define your interfaces in header files, and include those in the C files that need to use those interfaces.
2016-10-15 11:31:37 UTC
Are you which of them comprise the header archives on your substantial record? E.g., in the experience that your header record is declared as anIntArray then you definately ought to placed #comprise "anIntArray." on your substantial functionality (after the making use of namespace std; line) certainly - did you shop your header archives as a .h record? If no longer, you will ought to try this first.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...