简体   繁体   English

如何在 C(visual studio 代码)中运行多个文件?

[英]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.

相关问题 要编译和运行的 Visual Studio Code 任务。c 文件 - Visual Studio Code Task to Compile and Run .c Files 当我运行 C 代码时,如何阻止 Visual Studio Code 打开外部终端 window - How to stop Visual Studio Code from opening an external terminal window when I run my C code 在 Visual Studio Code 的 C/C++ 扩展中启动调试器后,如何运行命令? - How do I run a command after launching the debugger in the C/C++ extension for Visual Studio Code? 可以使用Visual Studio通过ARM脚本运行C代码吗? - Can Visual Studio be used to run C code with ARM scripts? 我可以在同一个项目 (Visual Studio 19) 中有多个 C 源文件吗? - Can i have multiple C source files in the same project (Visual Studio 19)? 如何在visual studio c中打印■? - How can I print ■ in visual studio c? 如何在 Visual Studio 代码中编译 c 中的 header 文件 - how to compile header files in c in visual studio code 如何在Visual Studio 2008中使格式文档快捷方式适用于C源文件? - How can I make the format document shortcut work for C source files in Visual Studio 2008? 如何使用make file选项在Visual Studio 2005中链接* .c文件? - How can I link *.c files in Visual Studio 2005 using make file option? 如何将带有 .c 和 .h 文件的现有项目转换为 Visual Studio 2019 中的动态链接库? - How can I convert an existing project with .c and .h files into a dynamic link library in visual studio 2019?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM