Makefiles对我来说很困惑。我所要做的就是将一些函数分离到一个单独的文件中,但是我无法编译它。我错过了什么?谢谢!
生成文件:
all: clientfunctions client
clientfunctions.o: clientfunctions.c
gcc -c clientfunctions.c -o clientfunctions.o
client.o: client.c clientfunctions.o
gcc -c client.c -o client.o
client: client.o
gcc client.o -o client
.c和.h文件也很简单:
clientfunctions.h
#ifndef _clientfunctions_h
#define _clientfunctions_h
#endif
void printmenu();
clientfunctions.c
#include <stdio.h>
#include "clientfunctions.h"
void printmenu() {
fprintf(stdout, "Please select one of the following options\n");
}
client.c
#include "clientfunctions.h"
int main (int argc, char * argv[])
{
printmenu();
return 0;
}
这是我得到的错误:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [clientfunctions] Error 1
答案 0 :(得分:2)
请尝试以下操作。
all: client
clientfunctions.o: clientfunctions.c
gcc -c clientfunctions.c -o clientfunctions.o
client.o: client.c
gcc -c client.c -o client.o
client: client.o clientfunctions.o
gcc client.o clientfunctions.o -o client
这是编写此Makefile的更惯用的方法。
all: client
client: client.o clientfunctions.o
$(CC) -o $@ $^
答案 1 :(得分:1)
您需要编译两个.c文件并将它们链接到您的可执行文件中。您需要依赖clientfunctions.o
目标中的client
,并在链接中包含此对象才能执行此操作
client: client.o clientfunctions.o
gcc client.o clientfunctions.o -o client
答案 2 :(得分:1)
你的工作方式太难了。您可以依赖隐式规则并大大简化您的makefile,其整个内容可能(可能取决于您使用的Make)简单如下:
client: client.o clientfunctions.o
答案 3 :(得分:0)
你实际上并没有说出什么是错的,除了“我无法让它编译”,它基本上没有告诉我们什么。尝试下次提供错误消息。
然而,这与make没有任何关系。下次当你认为你遇到makefile问题时,只需将命令切换到shell提示符即可。如果它们有效,那么你的问题就是make。如果它们不起作用,那么你的问题在于你的编译命令,你应该查看编译器的文档来解决它。 Make基本上是一个以正确的顺序和适当的时间运行shell命令的工具。
在您的情况下,如果要生成目标文件,则需要将-c
选项添加到编译行。如果没有该选项,编译器将尝试生成可执行文件。