0
votes

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?

4
Are you sure that you have the file main.c in the same directory where you are running the make command? - Ziffusion
@Ziffusion - THanks for the insightful comment. I believe somewhere the main.c file had been deleted. I now have written a new main.c and put in the folder. However now I get 'gcc -Wall -fPIC -c -o hello.o hello.c make: gcc: Command not found make: *** [hello.o] Error 127' - Eternal Learner
Any chance you can post debug make output? - Ziffusion
That's because you are setting the value of PATH in your Makefile. The shell is unable to find gcc because of that. Try renaming the PATH variable in the Makefile to, say, LIBPATH (in all places). - Ziffusion
@Ziffusion: +1 . Thanks, no way that I would have figured that out. Now I get the error - /usr/bin/ld: cannot find -lmylib collect2: ld returned 1 exit status make: *** [main] Error 1 Not sure as to why linker is not able to find the file. - Eternal Learner

4 Answers

1
votes

There are a few bugs (just typos) I can see is:

  1. space between - and Wall:

    CFLAGS = - Wall -c -fPIC
              ^
    
  2. PORG_OBJS should be PROG_OBJS

    $(CC) -o $(PROG) $(PORG_OBJS) -L$(PATH)
                       ^^^^
    
  3. 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
          ^  ^^^^^^^^
    
1
votes

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.

1
votes

+++++

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.

0
votes

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.