在VM ware workstation上不能使用bash编译C
语言代码,不知道如何使用makefile,
可以考虑使用以下步骤搭建 C 语言编译环境并使用 Makefile 进行编译:
安装编译工具链:打开终端(如 Ubuntu 终端),运行以下命令安装 C 语言编译器和 Make 工具:
sudo apt-get update
sudo apt-get install build-essential
创建 C 语言源代码文件和 Makefile:在终端中使用命令行创建 C 语言源代码文件和 Makefile。例如,创建名为 hello.c 的源代码文件和 Makefile 文件:
touch hello.c Makefile
编写 C 语言源代码:使用文本编辑器打开 hello.c 文件,并编写 C 语言代码。例如:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
编写 Makefile:使用文本编辑器打开 Makefile 文件,并编写 Makefile。例如:
all: hello
hello: hello.o
gcc -o hello hello.o
hello.o: hello.c
gcc -c hello.c
clean:
rm -f hello hello.o
在 Makefile 中,all 是默认目标,hello 是需要生成的可执行文件,hello.o 是编译生成的目标文件,clean 是用来清理编译生成的文件的目标。
使用 Makefile 编译源代码:在终端中进入源代码文件所在目录,并运行以下命令:
make
Make 工具会根据 Makefile 自动编译源代码并生成可执行文件。如果一切正常,会在当前目录下生成名为 hello 的可执行文件。
运行可执行文件:在终端中输入以下命令来运行可执行文件:
./hello
如果一切正常,会在终端中输出 Hello, world!。
这样就可以在 VMWare Workstation 上使用 Makefile 编译 C 语言代码了。