简体   繁体   English

我可以在同一个项目 (Visual Studio 19) 中有多个 C 源文件吗?

[英]Can i have multiple C source files in the same project (Visual Studio 19)?

Beginner here.初学者在这里。 I code in C atm.我用 C atm 编码。 So Ive just started using VS 2019. A few troubles along the way but I got Build error along with LNK 2005 and LNK 1169. Turns out I cant have multiple files in the same project.所以我刚开始使用 VS 2019。一路上遇到了一些麻烦,但我遇到了 Build 错误以及 LNK 2005 和 LNK 1169。原来我不能在同一个项目中有多个文件。 Especially if Im using same header in the files.特别是如果我在文件中使用相同的标题。 In this case it was stdio.h and math.h.在这种情况下,它是 stdio.h 和 math.h。 I recreated this multiple times.我多次重新创建了这个。 The code would compile without a hitch only if one file was present in the project.仅当项目中存在一个文件时,代码才能顺利编译。 Or atleast just one file with that particular header.或者至少只有一个具有该特定标题的文件。 Which pretty much prevents me from having multiple C source files under the same project.这几乎阻止了我在同一个项目下拥有多个 C 源文件。 So is there any way to have multiple files in the same project without these errors.那么有没有办法在同一个项目中拥有多个文件而不会出现这些错误。

A little annoying that I can't have multiple files with same header which is impossible cause obviously, I gotta use stdio.h everywhere.有点烦人,我不能有多个具有相同标题的文件,这显然是不可能的,我必须在任何地方使用 stdio.h。 enter image description here在此处输入图片说明

Essentially a Solution is a container used to contain multiple Projects , you can choose to compile either the whole Solution (so all the projects in the solution), or a single Project in said Solution.本质上,一个解决方案是一个用于包含多个项目的容器,您可以选择编译整个解决方案(解决方案中的所有项目),或所述解决方案中的单个项目。

Each Project can have only 1 main() because otherwise your OS won't know where to start executing, of course you can have multiple source files in the same project otherwise for even smaller projects you'd have a single file with a huge number of lines.每个项目只能有 1 个main()因为否则您的操作系统将不知道从哪里开始执行,当然您可以在同一个项目中拥有多个源文件,否则对于更小的项目,您将拥有一个数量庞大的单个文件行。 This structure is quite common in many IDE s (maybe with different names than Project and Solution , this is just a nomenclature that VS uses), it isn't really bound to Windows or Linux in particular.这种结构在许多IDE 中很常见(可能与ProjectSolution名称不同,这只是 VS 使用的命名法),它并没有真正绑定到 Windows 或 Linux。

Try on Linux to have multiple source files with multiple main() and try to compile then together with gcc , you'll see that you get a very similar error.尝试在 Linux 上有多个带有多个main()源文件,然后尝试与gcc一起gcc ,你会看到你得到一个非常相似的错误。

You also can't have the same function definition in multiple files otherwise you get the LNK 2005 linker error that you've seen.您也不能在多个文件中使用相同的函数定义,否则会出现您所看到的LNK 2005链接器错误。 This is because when the Linker is linking your files if it finds two identical definitions it doesn't know which one to take and goes crazy.这是因为当链接器链接您的文件时,如果它发现两个相同的定义,它不知道要采用哪个定义并且会发疯。

But then how do you include the same header files in different sources?但是如何在不同的源中包含相同的头文件呢?

Well that's why .h files have include guards , they ensure that the contents of an .h files are included by the linker only once in each Compilation.嗯,这就是为什么.h文件有包含保护,它们确保.h文件的内容在每次编译中只被链接器包含一次。

Usually in the header file there is one of two possible ways to include these guards, there are very small differences between these two methods but they don't really apply if you're not doing some very specific stuff:通常在头文件中,有两种可能的方法来包含这些守卫,这两种方法之间的区别非常小,但如果您不做一些非常具体的事情,它们实际上并不适用:

 #pragma once
 // Content of the header

or或者

#ifndef GUARD_H
#define GUARD_H

// Content of header

#endif

Where GUARD_H should be different for every .h file, usually it's a combination of your Project and header file names just to make sure it's different to other guards that could exist in other headers.每个.h文件的GUARD_H应该不同,通常它是您的项目和头文件名的组合,以确保它与其他头文件中可能存在的其他保护不同。

That's also why you can't (and shouldn't) include Source files in your code, if you include a source file then you are at a high risk of including the same definition multiple times since they don't use include guards.这也是您不能(也不应该)在代码中包含源文件的原因,如果您包含源文件,那么您很可能会多次包含相同的定义,因为它们不使用包含保护。

The C file gets stored normally in any folder you want and gets compiled with a gcc and ./a.out in the terminal C 文件通常存储在您想要的任何文件夹中,并在终端中使用 gcc 和 ./a.out 进行编译

That's essentially what VS does, just instead of you choosing which files to add in the compilation VS takes automatically all the files in the same Solution or Project (depending which one you decide to build).这基本上就是 VS 所做的,只是不是您选择要在编译中添加的文件,VS 会自动获取同一解决方案或项目中的所有文件(取决于您决定构建哪个文件)。

For convenience, if you are just starting to learn, create a new solution for each Project (so for every main() ) that you want.为方便起见,如果您刚刚开始学习,请为您想要的每个项目(因此为每个main() )创建一个新的解决方案。

Is this better or worse than using a simple text editor?这比使用简单的文本编辑器好还是坏? Well, after the first 10-15 minutes of learning you'll see that it's much faster and useful to use an actual IDE as it provides many features that will make you write code faster and with less errors.好吧,在学习的前 10-15 分钟后,您会发现使用实际的 IDE 更快、更有用,因为它提供了许多功能,可以让您更快地编写代码并减少错误。

This is a really quick explanation of how things work in general.这是对一般事物如何运作的非常快速的解释。 If you can, try to find someone IRL that can explain these things to you.如果可以,请尝试找到可以向您解释这些事情的 IRL。 It's really hard to explain it on the internet but it will take less than 10 minutes being together in front of the same computer.在互联网上很难解释它,但在同一台电脑前在一起只需不到 10 分钟。

暂无
暂无

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

相关问题 如何在 C(visual studio 代码)中运行多个文件? - How can i run multiple files in C (visual studio code)? 如何在Visual Studio 2008中使格式文档快捷方式适用于C源文件? - How can I make the format document shortcut work for C source files in Visual Studio 2008? 如何将用C编写的源代码从另一个项目包含到我自己的Visual Studio C ++项目中 - How can I include source code written in C from another project into my own project in C++ in Visual Studio 我可以在同一个Xcode项目中使用Swift,Objective-C,C和C ++文件吗? - Can I have Swift, Objective-C, C and C++ files in the same Xcode project? 如何将带有 .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? 我可以在Visual Studio中的单个项目中混合使用C ++和C吗? - Can I mix C++ and C in a single project in Visual Studio? 如果它们具有相同的名称并且我无法修改其他文件,如何在其他C文件中使用具有相同名称的多个函数? - How can I use multiple functions with the same name in other C files if they have the same name and I can't modify the other files? 我在Visual Studio 2012 Pro中有一个C ++ DLL类型的项目,但无法将其编译为CLR-为什么? - I have a C++ DLL type project in Visual Studio 2012 Pro but I can't compile it as CLR - Why is that? 如何在 Visual Studio 中从多个 c 源文件和 header 文件创建 .dll 或 .lib? - How to create .dll or .lib from multiple c source and header files in visual studio? 在多个c源文件中具有相同的功能 - having the same function in multiple c source files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM