[英]How can i run multiple files in C (visual studio code)?
I want to to run a simple program, using vs code, which includes three files: main.c item.c item.h.我想运行一个简单的程序,使用 vs 代码,其中包括三个文件:main.c item.c item.h。
I understand that I there is a way to link things together, but I don't know how.我知道我有办法将事物联系在一起,但我不知道如何。 Can you explain me how to do it?
你能解释一下怎么做吗?
I've tried also to add the extension to make a project, but I didn't understand how to.我也试过添加扩展来制作一个项目,但我不明白该怎么做。
Here's the code:这是代码:
main.c主要.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "item.h"
int main () {
int a = 2;
int b = 3;
int res;
res = prod(a,b);
printf("%d ", res);
return 0;
}
item.c项目.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "item.h"
int prod(int a, int b) {
return a*b;
}
item.h项目.h
#ifndef ITEM_H
#define ITEM_H
int prod(int a, int b);
#endif
I don't know if you are using Windows or Linux or Mac, I'm going to explain it for Linux but the method is similar for the others.不知道你用的是Windows还是Linux还是Mac,我就Linux来说明,其他的方法都差不多。
First of you go on VS Code, then you click on new file and rename it "makefile", then you write this:首先你是 go 在 VS Code 上,然后你点击新文件并将其重命名为“makefile”,然后你这样写:
link: item.o main.o
gcc item.o main.o -o programName
main.o:
gcc -c main.c
item.o:
gcc -c item.c
clear:
rm -f item.o main.o programName //this one is to delete files faster
Once you wrote the makefile you write in the terminal the command make
and you get the executable file for your program.编写 makefile 后,您在终端中编写命令
make
并获得程序的可执行文件。
However in item.c
you aren't using any of the library you included, you only need to include item.h
;但是在
item.c
中你没有使用你包含的任何库,你只需要包含item.h
; last thing, I don't know why you are doing the #ifndef
thing but it seems a waste.最后一件事,我不知道你为什么要做
#ifndef
事情,但这似乎是一种浪费。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.