Reputation: 295
I'm trying to split up a large C file because it's very difficult to read and keep track of what's doing what. I read up on it and it supposed to be as simple as creating a c file that contains the function, then a header file that references the function, then include the header file in main c file and simply use the function. However, I've done that and the function in question isn't actually being called in my code.
I tried placing printf statements in the function so I could see if it was really being called and these do not show up in monitor (which I interpret as the function is not being called)
//MAIN.c
#include "just_printf.h"
void app_main(){
printf("attempting to call print\n");
just_print();
}
//---------------------------------------
//JUST_PRINTF.h
#ifndef MAIN_JUST_PRINT_H_
#define MAIN_JUST_PRINT_H_
void just_print();
#endif
//---------------------------------------
//JUST_PRINTF.c
#include "just_printf.h"
void just_print(){
printf("tried to prrint");
}
When I run main.c, it's supposed to print "attempting to call print" then when it calls just_print() it's supposed to print "tried to prrint". This is just a sanity check before I try moving actual functionality out of my main.c file. What am I doing wrong? Am I misunderstanding how to split up a c file? Right now all I get is the first printf statement before the function call.
Upvotes: 2
Views: 317
Reputation: 73444
In main.c, change:
void app_main() {
to:
int main(void) {
and then compile like:
gcc main.c just_printf.c
and the output will be (after executing a.out):
Georgioss-MBP:~ gsamaras$ ./a.out
attempting to call print
tried to prrint
You should get a warning about implicit declaration of the function printf, since you don't include stdio.h
- do that in the header file, I would suggest, like this:
#include <stdio.h>
Suggestion: In the header file, the include guard would be usually written like:
JUST_PRINT_H
i.e., without the trailing underscore, and the full name of the file, in capital characters.
Upvotes: 2