Linux Makefile 错误:未找到命令 - 创建共享库时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3947622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:40:50  来源:igfitidea点击:

Makefile Error : Command Not Found- while creating a shared library

clinuxmakefileshared-libraries

提问by Eternal Learner

I have 4 .c files hello.c,here.c,bye.cand main.c. One header file mylib.h

我有4个.c文件hello.chere.cbye.cmain.c。一个头文件mylib.h

The contents are as follows

内容如下

hello.c

你好ç

#include<stdio.h>

void hello()
{
    printf("Hello!\n");
}

here.c

这里.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

mylib.h

#ifndef _mylib_
#define _mylib_

void hello();
void here();
void bye();

#endif

The makefile for creating a static lib is : Makefile

创建静态库的makefile是: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

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?

问题 : How do I resolve this?

采纳答案by Ziffusion

+++++

+++++

OK. Lets revert to your original code, but with a small difference.

好的。让我们恢复到您的原始代码,但有一点不同。

Change DYNLINKFLAGS back to:

将 DYNLINKFLAGS 改回:

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.

执行“rm -f lib*”,构建然后发布 make 输出。

回答by codaddict

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

我可以看到的一些错误(只是错别字)是:

  1. space between -and Wall:

    CFLAGS = - Wall -c -fPIC
              ^
    
  2. PORG_OBJSshould 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 gccis not found in that directory you get this error. To fix this you can either use a differentvariable name or add your directory to current path as:

     PATH := $(PATH):/home/srinivasa/cspp51081/labs/srinivasa.lab2.1
          ^  ^^^^^^^^
    
  1. -和之间的空间Wall

    CFLAGS = - Wall -c -fPIC
              ^
    
  2. PORG_OBJS应该 PROG_OBJS

    $(CC) -o $(PROG) $(PORG_OBJS) -L$(PATH)
                       ^^^^
    
  3. 您正在对PATH. 现在 makefile 中调用的每个可执行文件都将在该目录中搜索。由于gcc未在该目录中找到,您会收到此错误。要解决此问题,您可以使用不同的变量名称或将您的目录添加到当前路径:

     PATH := $(PATH):/home/srinivasa/cspp51081/labs/srinivasa.lab2.1
          ^  ^^^^^^^^
    

回答by Ziffusion

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.

-L 标志需要在 -l 标志之前。

回答by Ziffusion

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.

然后发布库链接和最终的可执行链接。