【发布时间】:2010-10-16 04:01:20
【问题描述】:
我有 4 个 .c 文件 hello.c、here.c、bye.c 和 main.c。
一个头文件mylib.h
内容如下
你好.c
#include<stdio.h>
void hello()
{
printf("Hello!\n");
}
这里.c
#include<stdio.h>
void here()
{
printf("I am here \n");
}
再见.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 是: 生成文件
#Which Compiler
CC = gcc
#Compiler Flags
CFLAGS = - Wall -c -fPIC
DYNLINKFLAGS = -shared -W1,-soname,$@.0
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 $@ $@.0
#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 制作:gcc:找不到命令 make: * [hello.o] 错误 127
问题:How do I resolve this?
【问题讨论】:
-
您确定 main.c 文件与您运行 make 命令的目录相同吗?
-
@Ziffusion - 感谢您的富有洞察力的评论。我相信某处 main.c 文件已被删除。我现在写了一个新的 main.c 并放在文件夹中。但是现在我得到'gcc -Wall -fPIC -c -o hello.o hello.c make: gcc: Command not found make: *** [hello.o] Error 127'
-
你可以发布调试输出吗?
-
那是因为您在 Makefile 中设置了 PATH 的值。因此,shell 无法找到 gcc。尝试将 Makefile 中的 PATH 变量重命名为 LIBPATH(在所有位置)。
-
@Ziffusion:+1。谢谢,我不可能知道这一点。现在我收到错误 - /usr/bin/ld:
cannot find -lmylib collect2: ld returned 1 exit status make: *** [main] Error 1不确定链接器为什么找不到文件。
标签: c linux makefile shared-libraries