Do you have lots of global variables as well as functions?
It's generally simpler if you restrict the first pass through the code to only move functions that don't depend on global variables.
Let's say your "large" main.c file consists of two functions.
Code:
#include <stdio.h>
#include <string.h>
void foo ( const char *s ) {
}
int main ( ) {
foo("hello");
}
In three separate editor windows, open (or create) three files:
- main.c
- functions.c
- functions.h
Initialise functions.c to look like this
Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
Initialise functions.h to look like this
Code:
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
// Prototypes go here
#endif
After the system includes in main.c, add the line
Code:
#include "functions.h"
One by one, you then do the following things.
1. Cut the code for the 'foo' function from main.c
2. Paste the code for the 'foo' function to the end of functions.c
3. Declare a prototype for the 'foo' function in functions.h
So like this.
Main.c
Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
// foo cut from here
int main ( ) {
foo("hello");
}
Functions.c
Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
// foo pasted here
void foo ( const char *s ) {
}
Functions.h
Code:
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
// Prototypes go here
void foo ( const char *s );
#endif
After each move of a function, compile everything to make sure it still works.