gpt4 book ai didi

c - Makefile 难点

转载 作者:太空宇宙 更新时间:2023-11-04 01:33:44 27 4
gpt4 key购买 nike

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 文件也很简单:

客户端函数.h

#ifndef _clientfunctions_h
#define _clientfunctions_h
#endif

void printmenu();

客户端函数.c

#include <stdio.h>
#include "clientfunctions.h"

void printmenu() {
fprintf(stdout, "Please select one of the following options\n");
}

客户端.c

#include "clientfunctions.h"

int main (int argc, char * argv[])
{
printmenu();
return 0;
}

这是我遇到的错误:

架构 x86_64 的 undefined symbol :
“_main”,引用自:
主可执行文件的隐式入口/启动
ld: 找不到体系结构 x86_64 的符号
clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
make: *** [clientfunctions] 错误 1

最佳答案

尝试以下操作。

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 $@ $^

关于c - Makefile 难点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18598903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com