d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Trying To Learn How To Make A Simple Make File
Add Reply New Topic New Poll
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Nov 23 2013 04:10pm
Still wrapping my head around this, C can be a headache compared to Java


I have two files, test1.c, and test2.c

test1.c:
Quote
#include <stdio.h>
#include <string.h>
int totoy = 4;
void print_totoy();
int main(){
 
printf("Hello world!\n"); 
    print_totoy();
return 1;
}


test2.c:
Quote
#include <stdio.h>
#include <string.h>
void print_totoy(){
   
    printf("%d", totoy);
   
    }

Now in the command line, I am trying to learn 2 things.
1. How to simply compile these 2 files together?
My incorrect approach was: gcc -o test test1.c test2.c

2. How to create a makefile with these 2 files together?
Don't know how to start.

This post was edited by oOn on Nov 23 2013 04:18pm
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Nov 23 2013 06:46pm
I guess I am misunderstanding how global variables work in this scope. Should it just be part of a header file, or is there a way to incorporate it otherwise?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 23 2013 07:14pm
I goggled 'using makefiles' and there was a pretty decent tutorial first hit I believe....
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Nov 23 2013 07:25pm
Quote (Eep @ Nov 24 2013 01:14am)
I goggled 'using makefiles' and there was a pretty decent tutorial first hit I believe....



Yeah thats what I was using

but I think I am misunderstanding dependencies or something, because despite using those commands I cant get it to recognize that I am referring to test2.c with my function call in test1.c

I was hoping for a simple explanation in the case I described above, but maybe I just need to learn this more thoroughly

This post was edited by oOn on Nov 23 2013 07:26pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 23 2013 07:29pm
I believe you need to do

Code

#include "test2.h"


where your .h file declares the function print_totoy
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Nov 23 2013 07:35pm
Quote (Eep @ Nov 24 2013 01:29am)
I believe you need to do

Code
#include "test2.h"


where your .h file declares the function print_totoy


That would make sense, thanks
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Nov 23 2013 08:09pm
Actually I think the prototypes / variables need to be declared externally in both files, and then it works fine. Seems to work ok now

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll