A question about Makefile run by GNU make


 
Thread Tools Search this Thread
Top Forums Programming A question about Makefile run by GNU make
# 1  
Old 05-20-2010
Question A question about Makefile run by GNU make

Hello everybody,

Currently I'm learning how to build projects (C programming) with GNU make. I have a problem with one Makefile and I would appreciate if you could kindly give me a hand. Here is the environment:

  • OS: Redhat linux 5
  • compiler: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
  • make: GNU Make 3.81
I have one header file (.h) and two .c files. All of the files are in the same directory. here is my Makefile:

Makefile:
Code:
CCFLAGS=-c -Wall
CC=gcc
.PHONY:clean

file1:mathfun1.o mathfun1.h
    $(CC) file1.c $^ -o $@

mathfun1.o:mathfun1.c mathfun1.h
    $(CC) $(CCFLAGS) mathfun1.c

clean:
    rm *.o file1

This works pretty well. But there is a problem. The file named file1.c is the principal file, that is, the file that includes the main function. As you can see in the first entry, among the prerequisites of the target file1, I just included mathfun1.o and mathfun1.h. As a result, if the source file "file1.c" is modified the target file1 is not updated because 'file1.c' is not in the list of the prerequisited of the target file1. Now, the problem is that if I add this file to the list of the preequisites I receive an error message.

I mean, if in the Makefile, instead of
Code:
file1:mathfun1.o mathfun1.h
    $(CC) file1.c $^ -o $@

I write
Code:
file1:mathfun1.o mathfun1.h file1.c
    $(CC) file1.c $^ -o $@

I receive the following error message:
Code:
$ make
gcc file1.c mathfun1.o file1.c -o file1
/tmp/ccYLTgKK.o: In function `main':
file1.c:(.text+0x0): multiple definition of `main'
/tmp/ccC0ZVY7.o:file1.c:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [file1] Error 1
$

Any idea? why I get this error message?


Thanks in advance,

Kind Regards,
Dariyoosh













In the case where it may be helpful, here are the complete definitions of other files. As I said earlier, all files are in the same directory.

mathfun1.h
Code:
#include <stdio.h>

int max(int n1, int n2);
int min(int n1, int n2);

mathfun1.c
Code:
#include "mathfun1.h"

int max(int n1, int n2)
{
    return ((n1 >= n2)?n1:n2);
}


int min(int n1, int n2)
{
    return ((n1 <= n2)?n1:n2);
}

file1.c
Code:
int main(int argc, char *argv[])
{
    int n1 = 0;
    int n2 = 0;

    printf("enter two integers: ");

    scanf("%d%d",&n1, &n2);
    printf("\n");

    printf("max(%d,%d) = %d\n", n1, n2, max(n1,n2));

    return 0;
}

# 2  
Old 05-20-2010
In simple words the error means there are two main functions since you have included file1.c twice here
Code:
gcc file1.c mathfun1.o file1.c -o file1

What you can do to solve your problem is build two object files mathfun1.o and file1.o and build the executable "file1" including both these object files. Your makefile would look something like this
Code:
CCFLAGS=-c -Wall
CC=gcc

file1:mathfun1.o file1.o
        $(CC) $^ -o $@

file1.o:file1.c
        $(CC) $(CCFLAGS) file1.c
mathfun1.o:mathfun1.c
        $(CC) $(CCFLAGS) mathfun1.c

clean:
        rm *.o file1

Regards,
Harish J
This User Gave Thanks to jharish For This Post:
# 3  
Old 05-20-2010
Quote:
Originally Posted by jharish
In simple words the error means there are two main functions since you have included file1.c twice here
Code:
gcc file1.c mathfun1.o file1.c -o file1

What you can do to solve your problem is build two object files mathfun1.o and file1.o and build the executable "file1" including both these object files. Your makefile would look something like this
Code:
CCFLAGS=-c -Wall
CC=gcc

file1:mathfun1.o file1.o
        $(CC) $^ -o $@

file1.o:file1.c
        $(CC) $(CCFLAGS) file1.c
mathfun1.o:mathfun1.c
        $(CC) $(CCFLAGS) mathfun1.c

clean:
        rm *.o file1

Regards,
Harish J

Hello there,


Thank you very much for your attention to my question. In fact this was the source of the problem. Here is therefore the new version of Makefile
Code:
CCFLAGS=-c -Wall
CC=gcc
.PHONY:clean

file1:mathfun1.o file1.o mathfun1.h
    $(CC) $^ -o $@

file1.o:mathfun1.h file1.c
    $(CC) $(CCFLAGS) file1.c

mathfun1.o:mathfun1.c mathfun1.h
    $(CC) $(CCFLAGS) mathfun1.c

clean:
    rm *.o file1

And everything works pretty well now.

Thank you very much!


Kind Regards,
Dariyoosh
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Makefile instructions to create directories (CentOS7 , GNU Make 3.82)

Hello, My makefiles are set up to generate an environment specific build directory based on the local configuration and some values passed to make. It generally looks like, # compilers, may be passed to make CC++ = g++ FCOMP = gfortran # version of program, may be passed to make ver =... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

2. AIX

Error when run makefile to compile C program

I have a make file for C program, which always gives the error ld: 0711-738 ERROR: Input file ../src/file_name.o XCOFF32 object files are not allowed in 64 mode Does anybody know the problem? Thanks for contribution (2 Replies)
Discussion started by: digioleg54
2 Replies

3. Solaris

SunOS 5.5.1 usage of Makefile command in make file

I am new to Solaris and compilation using make files. I have a code base which is organized into different folders. At the root folder is a master make file and in the sub directories, there are make files for that particular folder. In the make files present in subdirectories, I am seeing... (2 Replies)
Discussion started by: rajujayanthy
2 Replies

4. Shell Programming and Scripting

Bash script from makefile - it is called each time i call make

I've created a tag in the makefile: mytag: $(shell ${PWD}/script.sh) When i do: make clean - the script is executed When i perform make or make mytag the script is again executed with the output: make: Nothing to be done for mytag What i want ? I want script.sh to be executed only... (0 Replies)
Discussion started by: Pufo
0 Replies

5. Programming

Makefile No rule to make target

I am trying to create a makefile to build a program and am getting the following error: make -f tsimplex.mk make: *** No rule to make target `/main/tsimplex_main.cpp', needed by `tsimplex_main.o'. Stop. OPSYS = $(shell uname -s ) TARGET = tsimplex ROOTDIR = ../../.. GTSDIR =... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

Need help with a script to make makefile

How do we create a shell script that creates a makefile? what if we want to use the #include header files too? (2 Replies)
Discussion started by: sslokhan
2 Replies

7. Shell Programming and Scripting

how to run a shell script through Makefile

I want to run a target defined in a shell script. The shell script name is 'ua.sc' and the target in it is 'N' i.e. (ua N) throught a makefile. How can i do it so that i can run it with a make target. (3 Replies)
Discussion started by: vineet.dhingra
3 Replies

8. UNIX for Advanced & Expert Users

Makefile problem - How to run module load in a Makefile

Hi, I'm trying to run the module load command in a Makefile and i'm getting the following error: make: module: command not found Why is this? Is there any way to run this command in a Makefile? NOTE: command - module load msjava/sunjdk/1.5.0 works fine outside of the Makefile (2 Replies)
Discussion started by: hernandinho
2 Replies

9. UNIX for Dummies Questions & Answers

make and clean in a single rule in the makefile.

Hi, This stems from the following thread https://www.unix.com/showthread.php?t=18299 I have a makefile which makes either executables or a shared library. i.e. make -f unix.mak will create the executables and make -f unix.mak libolsv will create the shared library. Since these have to be... (4 Replies)
Discussion started by: vino
4 Replies

10. UNIX for Advanced & Expert Users

Problems with gnu make

I am running a make file through the gnu make tool and i am getting the following error jsh1035c:/users/egate453/admegate/kapil/samples $ make -f GNUmakefile queue_c make -f ./GNUmakefile queue_c in_objdir=1 build_root=/users/egate453/admegate/kapil/samples make: Entering directory... (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question