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

发布于 2024-09-28 09:15:54 字数 2125 浏览 0 评论 0原文

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

内容如下

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

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

问题:当我执行命令时

make -f Makefile all 

,出现错误:

gcc -Wall -fPIC -c -o hello.o hello.c make:gcc:找不到命令 make: * [hello.o] 错误 127

问题:如何解决此问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

错爱 2024-10-05 09:15:54

+++++

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

将 DYNLINKFLAGS 更改回:

DYNLINKFLAGS = -shared -Wl,-soname,[email protected]

然后将库链接更改为:

$(CC) $(DYNLINKFLAGS) -o $(LIB_RELEASE) $^
ln -sf $(LIB_RELEASE) $(LIB_MINOR)
ln -sf $(LIB_MINOR) $@
ln -sf $@ [email protected]

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

+++++

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.

橘虞初梦 2024-10-05 09:15:54

我可以看到一些错误(只是拼写错误):

  1. -Wall 之间的空间:

    CFLAGS = - Wall -c -fPIC
              ^
    
  2. PORG_OBJS 应为 PROG_OBJS< /p>

    <前><代码>$(CC) -o $(PROG) $(PORG_OBJS) -L$(路径)
    ^^^^

  3. 您正在对 PATH 进行绝对赋值。现在,makefile 中调用的每个可执行文件都将在该目录中搜索。由于在该目录中找不到 gcc ,因此您会收到此错误。要解决此问题,您可以使用不同变量名称或将目录添加到当前路径:

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

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
          ^  ^^^^^^^^
    
剩余の解释 2024-10-05 09:15:54

尝试将此行从: 更改

$(CC) -o $(PROG) $(PORG_OBJS) -l$(LIB) -L$(LIBPATH)

为:

$(CC) -o $(PROG) $(PORG_OBJS) -L$(LIBPATH) -l$(LIB)

-L 标志需要位于 -l 标志之前。

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.

再见回来 2024-10-05 09:15:54

好的。首先更改:

DYNLINKFLAGS = -shared -W1,-soname,[email protected]

DYNLINKFLAGS = -shared -W1,-soname,$@

然后更改:

ln -sf $(LIB_RELEASE) $(LIB_MINOR)
ln -sf $(LIB_MINOR) $@
ln -sf $@ [email protected]

至:

ln -sf $@ $(LIB_RELEASE)
ln -sf $@ $(LIB_MINOR)
ln -sf $@ [email protected]

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文