-
November 13th, 2009, 08:19 PM
#1
help making MakeFile
I am suppose to make a MakeFile for the 2 source files named p4a.c and p4b.c
Am i doing this makefile correctly? Any suggestions would be so helpful
my MakeFile so far
Code:
.SUFFIXES: .c .o
CC - gcc
CFLAGS = -g
.c.o:
$(CC) $(CFLAGS) -c $,
sample: p4a.c p4b.c
gcc p4a.c p4b.c -o sample
p4a.c:
p4b.c:
clean:
rm -f *.o core
p4a.c
Code:
#include<stdio.h>
#include<string.h>
main(int argc,char* argv[])
{
short int a[100][5];
short int res;
FILE *fp1;
if(!(fp1=fopen(argv[1],"r")))
printf("Error opening file\n");
else
{
FILE *fp2;
int i,j,k,l;
i=j=0;
while(!feof(fp1))
{
for(j=0;j<4;j++)
fscanf(fp1,"%d",&a[i][j]);
i++;
}
for(k=0;k<i;k++)
{
unsigned short int musk=!0; //musk=11...111
musk=musk|15; //musk=000..001111
musk=musk<<12; //musk=111100..000
res=0;
for(l=0;l<4;l++)
{
short int temp=(a[k][l]&musk);
res=res|temp;
musk=musk>>4;
}
a[k][4]=res;
}
if(!(fp2=fopen(argv[2],"w")))
printf("Error:cannot open file");
else
{
for(k=0;k<i;k++)
{
for(j=0;j<5;j++)
fprintf(fp2,"%d%s",a[k][j],"\t");
fprintf(fp2,"%s","\n");
}
fclose(fp1);
fclose(fp2);
}
}
return 0;
}
p4b.c
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct token
{
char name[15];
int def,usecount,use[100];
}identifier;
identifier id[1000];
int idcounter;
void search(char* token,int linenum)
{
int i;
for(i=0;i<idcounter;i++) //check if already exist
{
if(!strcmp(token,id[i].name))
{
id[i].use[id[i].usecount]=linenum;
id[i].usecount++;
return;
}
}
//insert
strcpy(id[idcounter].name,token);
id[idcounter].def=linenum;
id[idcounter].usecount=0;
idcounter++;
}
void check(char line[81],int linenum)
{
char token1[15];
char* token=(char*)malloc(sizeof(char)*15);
if(line[0]=='_' || (line[0]>='a' && line[0]<='z') || (line[0]>='A' && line[0]<='Z'))
{
token=strtok(line," ,\t"); //take label
strcpy(token1,token);
if(token1[strlen(token1)-1]==':')
token1[strlen(token1)-1]='\0';
search(token1,linenum);
token=strtok(0," ,\t"); //skip opcode\datatype
strcpy(token1,token);
if(token1[0]=='.') //check data type
return;
while(token)
{
token=strtok(0," ,\t");
if(token)
{
strcpy(token1,token);
if(token1[0]=='#')
return; //start of comment
else if(token1[0]=='_' || (token1[0]>='a' && token1[0]<='z') || (token1[0]>='A' && token1[0]<='Z'))
{
if(token1[strlen(token)-1]=='\n')
token1[strlen(token)-1]='\0';
search(token1,linenum);
}
}
}
}
else
{
token=strtok(line," ,\t"); //skip opcode
token=strtok(0," ,\t"); //search for optional operands
while(token)
{
strcpy(token1,token);
if(token1[0]=='#')
return; //start of comment
else if(token1[0]=='_' || (token1[0]>='a' && token1[0]<='z') || (token1[0]>='A' && token1[0]<='Z'))
{
if(token1[strlen(token)-1]=='\n')
token1[strlen(token)-1]='\0';
search(token1,linenum);
}
token=strtok(0," ,\t");
}
}
}
main(int argc,char* argv[])
{
char line[81],line2[81];
int i,linenum,p,q;
FILE *fp1,*fp2;
if(argc!=3) //check invalid command
{
printf("Invalid command argument number\n");
return 1;
}
if(!(fp1=fopen(argv[1],"r")))
{
printf("Error opening inputfile\n");
return 1;
}
if(!(fp2=fopen(argv[2],"w")))
{
printf("Error opening outputfile\n");
return 1;
}
linenum=i=1;
while(fgets(line,81,fp1))
{
if(line[0]=='#')
{
//fputs(strcat(strcat(itoa(i,str,100),". "),line),fp2);
//fputs("\n",fp2);
fprintf(fp2,"%d%s",i,". ");
fputs(line,fp2);
linenum++;
i++;
}
else if(strlen(line)>1)
{
strcpy(line2,line);
fprintf(fp2,"%d%s",i,". ");
fputs(line2,fp2);
//fputs(strcat(strcat(itoa(i,str,100),". "),line2),fp2);
check(line,linenum);
i++;
linenum++;
//printf("%d.\t%s",i,line);
}
else
{
fputs(line,fp2);
//fputs("\n",fp2);
linenum++;
}
}
fprintf(fp2,"%s","\n\n\tCross Reference Table\n");
fprintf(fp2,"%s","\n\nname\t\t def\t \tuse\n\n");
for(p=0;p<idcounter;p++)
{
fprintf(fp2,"%s\t\t%d\t\t",id[p].name,id[p].def);
for(q=0;q<id[p].usecount;q++)
{
if(q==0)
fprintf(fp2,"%d",id[p].use[q]);
else
fprintf(fp2,"%c%d",',',id[p].use[q]);
}
fprintf(fp2,"%s","\n");
}
fclose(fp1);
fclose(fp1);
return 0;
}
-
November 13th, 2009, 09:18 PM
#2
Re: help making MakeFile
Originally Posted by cabosun1
Am i doing this makefile correctly?
Evidently not.
Who did you copy that one off? Whoever it was your teacher will know it was copied too. Best to start your own from scratch and only put stuff in it that you understand.
A quick search on google for "Makefile Manual"
First on the list:
http://www.gnu.org/software/make/manual/make.html
I'm assuming you're using GNU Make right? Probobally.
Thats a bit long so another search "Makefile tutorial"
Second on the list (the first link mentioned windows so best to steer clear if you working under linux).
http://mrbook.org/tutorials/make/
Google is so cool!
Anyway. Makefiles are essentially a rule based script file for automatically building stuff.
The basic idea is that they only build stuff when its needed and not just build everything every time they're used.
The basic format of a makefile is a bunch of rules stating how to build different files:
Code:
file_to_build: files needed to build
method to build it
So to compile a file "my_program" from the files "my_source.c" and "my_header.h" you'd have a makefile that simply said:
Code:
my_program: my_source.c my_header.h
gcc -o my_program my_source.c
Hope this helps
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
-
November 13th, 2009, 09:45 PM
#3
Re: help making MakeFile
does this look alright?
CC=gcc
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=p4a.c p4b.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=hello
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
.c.o:
$(CC) $(CFLAGS) $< -o $@
instead of CC=g++
my compiler uses
gcc p4a.c
to compile a program.
-
November 13th, 2009, 10:06 PM
#4
Re: help making MakeFile
Again you're making it unessarily complicated
Taken from your first post, a file just containing the following would be sufficient to build your program:
Code:
sample: p4a.c p4b.c
gcc -o sample p4a.c p4b.
Its simple and it works.
If you need a "clean" option, then you need to work out what command(s) you need to call in order to delete everything made by the build process.
Only use the other stuff when a) know what its does and b) have a reason to use it.
If you have the time do read the makefile manual. But first another tutorial perhapse:
http://www.eng.hawaii.edu/Tutor/Make/
Last edited by couling; November 13th, 2009 at 10:11 PM.
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|