LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2010, 10:17 AM   #1
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Rep: Reputation: 0
Exclamation Problem in 'makefile'.....


This is the makefile....
Code:
animesh:main.o input.o bsort.o output.o
	gcc -o animesh main.o input.o bsort.o output.o
main.o:main.c output.c
	gcc -c main.c
input.o:input.c A.h
	gcc -c input.c
bsort.o:bsort.c input.c
	gcc -c bsort.c
output.o:output.c bsort.c
	gcc -c output.c
This is bsort.c
Code:
#include "input.c"
int j,k,t;
void bub_sort()
{
   for (j=0 ; j<10 ; ++j)
        {
           for (k=0 ; k<(9-j) ; ++k)  
                {  
                   if (c[k] > c[k+1])
                       t=c[k];
                       c[k]=c[k+1];
                       c[k+1]=t;
                }
        }
}
This is input.c
Code:
#include "A.h"
int c[10], i;
void insert()
{
    printf ("Enter the array.....");
    for (i=0 ; i<=10 ; ++i)
        scanf ("%d", &c[i]);  
}
This is A.h
Code:
#include <stdio.h>
This is main.c
Code:
#include "output.c"
extern void insert();
extern void bub_sort();
extern void op();
void main()
{
   void insert();
   void bub_sort();
   void op();
}
This is output.c
Code:
#include "bsort.c"
void op
{
  printf("The final sorted array is...");
  for (j=0 ; j<=10 ; ++j)
     printf("%d", c[j]);
}
I am having a lot of errors that I am not able to correct...!!!!!!!
Errors are mainly due to multiple declarations of the functions....I cannot debug those errors.....
Please help !!!!!
:|
 
Old 11-24-2010, 10:23 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by apanimesh061 View Post
I cannot debug those errors.....
Please help !!!!!
:|
Hi there!

Thank you for providing all the program code. The more information included, the better.

But:

While we probably have members who can determine exactly what errors you are getting just by looking at the code, it is generally a very good idea to include the errors in your thread!

So, please show us the error-filled output that you get when you execute `make` on your Makefile, so folks can see exactly what errors you're getting..
 
Old 11-24-2010, 01:05 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Don't #include .c files.

PS the following makefile is sufficient (assuming GNU make):
Code:
CC=gcc
animesh:main.o input.o bsort.o output.o
	$(CC) -o $@ $^

input.o: A.h
 
Old 11-25-2010, 09:12 AM   #4
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Original Poster
Rep: Reputation: 0
Unhappy

@GrapefruiTgirl....
This is the long list of errors......!!!!! :\

Code:
mint@mint ~/Templates $ make
gcc -c main.c
make: Warning: File `A.h' has modification time 3e+05 s in the future
gcc -c input.c
gcc -o animesh main.o input.o bsort.o output.o
input.o: In function `insert':
input.c:(.text+0x0): multiple definition of `insert'
main.o:main.c:(.text+0x0): first defined here
bsort.o: In function `insert':
bsort.c:(.text+0x0): multiple definition of `insert'
main.o:main.c:(.text+0x0): first defined here
bsort.o: In function `bub_sort':
bsort.c:(.text+0x57): multiple definition of `bub_sort'
main.o:main.c:(.text+0x57): first defined here
output.o: In function `insert':
output.c:(.text+0x0): multiple definition of `insert'
main.o:main.c:(.text+0x0): first defined here
output.o: In function `bub_sort':
output.c:(.text+0x57): multiple definition of `bub_sort'
main.o:main.c:(.text+0x57): first defined here
output.o: In function `op':
output.c:(.text+0x118): multiple definition of `op'
main.o:main.c:(.text+0x118): first defined here
collect2: ld returned 1 exit status
make: *** [animesh] Error 1
If anybody can please make out how to correct them....Thanks....
 
Old 11-25-2010, 09:16 AM   #5
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Original Poster
Rep: Reputation: 0
@nTubski

Well thanks...!!!!
I think I understood the code.
But could you please tell me why shouldn't I include '.c' files ???
 
Old 11-25-2010, 11:45 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
When you #include a file, it is the same as copy pasting the entire contents in place of the #include line. So when you put #include input.c inside of bsort.c it will have all the text of bsort.c so it will define bub_sort() too. That means that both bsort.o and input.o will have definitions for bub_sort(). You can't link 2 .o files defining the same function together, thus all your errors.
 
Old 11-27-2010, 12:09 AM   #7
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Original Poster
Rep: Reputation: 0
@ntubski

Does that mean that I have to include a.h, input.c, output.c and bsort.c directly in main.c ?

and makefile should be.....

CC=gcc
animesh:main.o input.o bsort.o output.o
$(CC) -o $@ $^

input.o: A.h

What is meaning of '$^' ?
 
Old 11-27-2010, 08:59 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by apanimesh061 View Post
Does that mean that I have to include a.h, input.c, output.c and bsort.c directly in main.c ?
No, the code in input.c, for example, gets compiled into input.o. Then you link all the .o files into the program (animesh).

Quote:
What is meaning of '$^' ?
It expands to the names of the prequisites of the rule. In this case it would be the same if you wrote
Code:
CC=gcc
animesh:main.o input.o bsort.o output.o
	$(CC) -o $@ main.o input.o bsort.o output.o

input.o: A.h
But if you add a new .o file using $^ saves you from having to write it in two places in the Makefile. See Automatic Variables
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
make: Warning: Both `makefile' and `Makefile' exist ? malli42108 Solaris / OpenSolaris 5 10-24-2009 09:09 AM
Is it mandatory to have the name of the makefile as 'Makefile' for kernal module comp narender.d Linux - Kernel 3 05-29-2009 06:26 AM
how to get (makefile -f makefile )output into the textview widget in Pygtk sailu_mvn Programming 3 02-28-2005 03:57 AM
generate Makefile from Makefile.in without calling ./configure ? chris78 Programming 2 05-02-2004 12:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration