简体   繁体   English

每个文件都需要 Visual Studio #include “file.c”

[英]Visual studio #include “file.c” needed in every file

I recently wanted to test my C project on visual studio (I used visual studio code) but for some reason my other file (file.c) do not recognize function from my main file (main.c).我最近想在 Visual Studio 上测试我的 C 项目(我使用了 Visual Studio 代码),但由于某种原因,我的其他文件(file.c)无法从我的主文件(main.c)中识别出 function。 I need to manually add #include in both files.我需要在两个文件中手动添加#include。 I can compile the code normally using cmd (gcc main.c).我可以使用 cmd (gcc main.c) 正常编译代码。 If you have any idea what cause this I would be happy to hear.如果您知道这是什么原因,我很乐意听到。

main.c: main.c:

#include <stdio.h>
#include <stdlib.h>

int randNumber = 10;

#include "file.c"

int main ()
{
  int a = 10;
  int b = 2;
  printf ("%i", multiplication (a, b));
  return 0;
}

file.c:文件.c:

int multiplication (int a, int b)
{
  return a * b * randNumber;
}

This runs normally in vs code but not in visual studio.这在 vs 代码中正常运行,但在 Visual Studio 中不运行。 (I can also compile the code with cmd outside of visual studio with the command (gcc main.c)) The thing that doesn't work is the randNumber, it gets an error saying (identifier "randNumber" is undefined). (我也可以使用命令(gcc main.c)在Visual Studio外部使用cmd编译代码)不起作用的是randNumber,它会出现错误消息(标识符“randNumber”未定义)。

You should not include source files ( .c ) in other source files.您不应在其他源文件中包含源文件 ( .c )。 software_rendering.c uses types defined in win32_platform.c . software_rendering.c使用在win32_platform.c中定义的类型。 For example, the Render_Buffer structure.例如, Render_Buffer结构。

You need to put the common types and definitions in a header file ( .h ) and include that.您需要将常用类型和定义放在 header 文件 ( .h ) 中并包含在内。 Keeping the Render_Buffer example: move the Render_Buffer structure definition in a header file and include that header file in both source files.保留Render_Buffer示例:将Render_Buffer结构定义移动到 header 文件中,并将 header 文件包含在两个源文件中。

See Creating your own header file in C for details.有关详细信息,请参阅C 中的创建您自己的 header 文件

As a side note: please do not post images of your code, instead paste it as text and properly format it as code .附带说明:请不要发布代码的图像,而是将其粘贴为文本并将其正确格式化为 code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM