1

Beginner here. I code in 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. Especially if Im using same header in the files. In this case it was stdio.h and 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. 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. enter image description here

Artaxerxes
  • 137
  • 1
  • 3
  • 10
  • 1
    Do you have more than one `main()` function? Please post the compiler output message, in the question. There is no reason why each source file can't `#include` the same library headers: they have "header guards" to allow it. – Weather Vane Nov 27 '19 at 15:24
  • 2
    This is very unusual, something is probably wrong with your code but we can't tell you how you can fix it without seeing your code and the exact error messages that you get – John Doe Nov 27 '19 at 15:28
  • I opened the link that points to the error here. https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-error-lnk2005?view=vs-2019 According to this page, I cannot have sam headers in the same project in multiple files. I tested this by deleting other files or by using // that made those headers into comments. And it compiled with no problems whatsoever. – Artaxerxes Nov 27 '19 at 15:47
  • Ill add an image just in case – Artaxerxes Nov 27 '19 at 15:50
  • You can't have `main` in both files. – tkausl Nov 27 '19 at 16:02
  • Forgive me but how do i write the code without main. My intention is to have multiple C source files in the same project. And then compile them and learn. It seems like there isnt a way for me to have multiple C source files in the same project. Edit: I just tested it by removing the main function in the first file and tried compiling the second. It didnt work. I then deleted the first file and compiled the second file without issues – Artaxerxes Nov 27 '19 at 16:11
  • You can have as many source files as you want, but you can have ony __one__ main function. – tkausl Nov 27 '19 at 16:13
  • Alright I tested it again. It was the main function. Thanks. So there isnt a way for me to have main functions in every file in the same project. Thats sad. Maybe I'll give up VS and stick to Linux instead. The text editor there is very easy to use for beginners – Artaxerxes Nov 27 '19 at 16:18
  • can you suggest an alternative IDE for me to compile C programs. Visual Studio doesnt look like the one for beginners – Artaxerxes Nov 27 '19 at 16:24
  • You can have more than one "project" in the same "solution". So you could add a new project for each of your programs all in the same "solution". – Ian Abbott Nov 27 '19 at 16:42
  • You will have the same problem when you move to Linux. main() is the entry point function of your application. main() is the first function invoked, so if you have multiple main() functions which one is the operating system supposed to call when you attempt to execute your application? – Ed Dore Nov 27 '19 at 17:06
  • Maybe I misunderstood. Ive already been using Linux(ubuntu) using vmware. And I have no problems saving multiple C source files. Correct me if I'm wrong but the concept of Solution and Project doesnt exist in Linux text editor. The C file gets stored normally in any folder you want and gets compiled with a gcc and ./a.out in the terminal. You can store as many C source files as you please under the same directory and execute effortlessly because Projects and Solutions as far as I know do not exist there. So VS' behavior felt unusual because I dont run into this problem in Linux txt editor – Artaxerxes Nov 27 '19 at 17:59
  • So here is the big misunderstanding, introduced by Microsoft's way in Visual Studio. BTW, you will find the same pattern in many IDEs and on Linux, too. A "solution" is kind of a box to collect one or more "projects" which belong to each other, or even not. And a "project" is meant to hold all files which belong to a single program. That's why you can have only *one* `main()` in a "project." VS likes to organize these in separate folders. – the busybee Nov 27 '19 at 21:56
  • "A "solution" is kind of a box to collect one or more "projects" which belong to each other". What exactly does that mean ? Is that the reason why im unable to run projects. I have multiple projects under the same solution. But whenever I try to run lets say Project2, Project 1 gets compiled instead. No matter what i do, project1 is the one that gets compiled and displayed in the terminal. I tried closing project1. It just didnt matter, I never got the output of the C file under project2. The output of project1 C file was displayed everytime. Same thing with project3 and 4. quite discouraging – Artaxerxes Nov 28 '19 at 07:05

1 Answers1

7

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. This structure is quite common in many IDEs (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.

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.

You also can't have the same function definition in multiple files otherwise you get the LNK 2005 linker error that you've seen. 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.

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.

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

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).

For convenience, if you are just starting to learn, create a new solution for each Project (so for every main()) that you want.

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.

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. 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.

John Doe
  • 1,613
  • 1
  • 17
  • 35
  • In Ubuntu, I write a C program, save it. Then I type gcc filename.c and then ./a.out to get the desired output. I know Visual Studio is better but the fact that I'm a beginner really shows because Ive found VS tedious compared to the 2 simple lines I have to type to get the output. Another thing, there is this problem where it VS doesnt execute the project I want but instead executes another project everytime. Ive got multiple projects under the same solution and I ultimately gave up cause it just wouldnt compile what i wanted – Artaxerxes Nov 29 '19 at 15:02
  • There also seem to be other problems like scanf ignored. I fixed a couple of them using CRT_SECURE_NO_WARNINGS but thats just a fraction of the issues i faced in VS. Thanks for answering though. It helps – Artaxerxes Nov 29 '19 at 15:09
  • 1
    All the "issues" that you are referring to are actually things put in place by VS to make your code more secure or make you develop faster/better code (like not using `scanf` which is not recommended for a [number of reasons](https://stackoverflow.com/questions/2430303/disadvantages-of-scanf) ). As I said, try to find someone IRL that can explain to you what is happening in real time. It will take just a couple of minutes and make your developing experience much better – John Doe Dec 02 '19 at 08:46
  • 1
    I'm not saying that VS is the be-all end-all of IDEs but most of the problem you are facing now are gonna be present in pretty much every other IDE – John Doe Dec 02 '19 at 08:49
  • Really sorry to bother you again after 2 months. But you mentioned "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." I didn't quite catch that. – Artaxerxes Feb 16 '20 at 09:19
  • 1
    @Artaxerxes no worries, let's say you create a function `int test(){return 1;}` in a file called `foo.c` and the `main` in the file `main.c`. If in `main.c` you say `#include "foo.c"` you will most likely get a multiple definitions error. This is because `foo.c` gets compiled twice, the compiler compiles `foo.c`, finds the function `test()` then compiles `main.c`, sees that `foo.c` is included, tries to compile it, finds `test()` again and gets confused. You can avoid this by creating a file `foo.h` that contains only `int test();` and then make `foo.c` and `main.c` `#include` the file `foo.h` – John Doe Feb 17 '20 at 13:37
  • 1
    also, `you can't include ... Source files` is not strictly correct, you can technically include them but it's a very bad thing to do. In the file `foo.h` you need to use `include guards` like i specified in the answer, these are pretty much a way to tell the compiler "hey, you already compiled this file, no need to do it again" so that it doesn't find the same functions twice and doesn't get confused. – John Doe Feb 17 '20 at 13:39