Reputation: 3870
I have 4 .c files hello.c
,here.c
,bye.c
and main.c
.
One header file mylib.h
The contents are as follows
hello.c
#include<stdio.h>
void hello()
{
printf("Hello!\n");
}
here.c
#include<stdio.h>
void here()
{
printf("I am here \n");
}
bye.c
#include<stdio.h>
void bye()
{
printf("Bye,Bye");
}
main.c
#include<stdio.h>
#include "mylib.h"
int main()
{
hello();
here();
bye();
return 1;
}
mylib.h
#ifndef _mylib_
#define _mylib_
void hello();
void here();
void bye();
#endif
The makefile for creating a static lib is : Makefile
#Which Compiler
CC = gcc
#Compiler Flags
CFLAGS = - Wall -c -fPIC
DYNLINKFLAGS = -shared -W1,-soname,[email protected]
PROG = main
PROG_OBJS = main.c
LIB = mylib
LIB_FILES = libmylib.so
LIB_MINOR = $(LIB_FILES).0.1
LIB_RELEASE = $(LIB_MINOR).0
LIB_OBJS = hello.o here.o bye.o
PATH = /home/srinivasa/cspp51081/labs/srinivasa.lab2.1
all: $(LIB_FILES) $(PROG)
#Create Lib with this file
$(LIB_FILES): $(LIB_OBJS)
$(CC) $(DYNLINKFLAGS) $^
ln -sf $(LIB_RELEASE) $(LIB_MINOR)
ln -sf $(LIB_MINOR) $@
ln -sf $@ [email protected]
#Compiling main program and link with shared library
$(PROG): $(PROG_OBJS)
$(CC) -o $(PROG) $(PORG_OBJS) -l$(LIB) -L$(PATH)
main.o: main.c
hello.o: hello.c
here.o: here.c
bye.o: bye.c
#clean files
clean:
rm -rf $(LIB_OBJS) $(LIB_FILES) $(LIB_RELEASE) $(LIB_MINOR) libmylib.so.0
Problem: When I execute the command
make -f Makefile all
I get the error:
gcc -Wall -fPIC -c -o hello.o hello.c make: gcc: Command not found make: * [hello.o] Error 127
Questions : How do I resolve this?
Upvotes: 0
Views: 25011
Reputation: 8933
+++++
OK. Lets revert to your original code, but with a small difference.
Change DYNLINKFLAGS back to:
DYNLINKFLAGS = -shared -Wl,-soname,[email protected]
Then change the library link to:
$(CC) $(DYNLINKFLAGS) -o $(LIB_RELEASE) $^
ln -sf $(LIB_RELEASE) $(LIB_MINOR)
ln -sf $(LIB_MINOR) $@
ln -sf $@ [email protected]
Do "rm -f lib*", build and then post make output.
Upvotes: 1
Reputation: 8933
OK. First change:
DYNLINKFLAGS = -shared -W1,-soname,[email protected]
to
DYNLINKFLAGS = -shared -W1,-soname,$@
Then change:
ln -sf $(LIB_RELEASE) $(LIB_MINOR)
ln -sf $(LIB_MINOR) $@
ln -sf $@ [email protected]
To:
ln -sf $@ $(LIB_RELEASE)
ln -sf $@ $(LIB_MINOR)
ln -sf $@ [email protected]
Then post the library links and the final executable link.
Upvotes: 0
Reputation: 8933
Try changing this line from:
$(CC) -o $(PROG) $(PORG_OBJS) -l$(LIB) -L$(LIBPATH)
to:
$(CC) -o $(PROG) $(PORG_OBJS) -L$(LIBPATH) -l$(LIB)
The -L flag needs to precede the -l flags.
Upvotes: 1
Reputation: 455410
There are a few bugs (just typos) I can see is:
space between -
and Wall
:
CFLAGS = - Wall -c -fPIC
^
PORG_OBJS
should be PROG_OBJS
$(CC) -o $(PROG) $(PORG_OBJS) -L$(PATH)
^^^^
You are doing an absolute assignment to PATH
. Now every executable called in makefile will be search in that directory. Since gcc
is not found in that directory you get this error. To fix this you can either use a different variable name or add your directory to current path as:
PATH := $(PATH):/home/srinivasa/cspp51081/labs/srinivasa.lab2.1
^ ^^^^^^^^
Upvotes: 1