Makefile 错误:未找到命令 - 创建共享库时
我有 4 个 .c 文件 hello.c
、here.c
、bye.c
和 main.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
+++++
好的。让我们恢复到原来的代码,但有一点小小的不同。
将 DYNLINKFLAGS 更改回:
然后将库链接更改为:
执行“rm -f lib*”,构建,然后发布 make 输出。
+++++
OK. Lets revert to your original code, but with a small difference.
Change DYNLINKFLAGS back to:
Then change the library link to:
Do "rm -f lib*", build and then post make output.
我可以看到一些错误(只是拼写错误):
-
和Wall
之间的空间:PORG_OBJS
应为PROG_OBJS
< /p><前><代码>$(CC) -o $(PROG) $(PORG_OBJS) -L$(路径)
^^^^
您正在对
PATH
进行绝对赋值。现在,makefile 中调用的每个可执行文件都将在该目录中搜索。由于在该目录中找不到gcc
,因此您会收到此错误。要解决此问题,您可以使用不同变量名称或将目录添加到当前路径:There are a few bugs (just typos) I can see is:
space between
-
andWall
:PORG_OBJS
should bePROG_OBJS
You are doing an absolute assignment to
PATH
. Now every executable called in makefile will be search in that directory. Sincegcc
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:尝试将此行从: 更改
为:
-L 标志需要位于 -l 标志之前。
Try changing this line from:
to:
The -L flag needs to precede the -l flags.
好的。首先更改:
至
然后更改:
至:
然后发布库链接和最终的可执行链接。
OK. First change:
to
Then change:
To:
Then post the library links and the final executable link.