E
E
Evgeny Proff2018-04-28 12:32:40
C++ / C#
Evgeny Proff, 2018-04-28 12:32:40

How to fix C++ file linking error in VS 2017(LNK2005)?

Hey!
There was a problem in VS 2017 with a file linking error (LNK2005), I will attach screenshots for clarity.
There are 3 files in the project:

  • MyForm.h is the file of the C++\CLI form itself, here I do #include "source.cpp", which most likely fails, I also use methods for interacting with the form like openFileDialog, etc.
    spoiler
    5ae43dbb2f7b7003435962.jpeg5ae43dc33f59b774396348.jpeg

  • Source.cpp is the main script for 1.3k lines, I need to call the function void solve() {...} in MyForm.h, which works only within the standard libraries and Source.cpp itself, without calling anything "from within", for simplicity, I commented everything out and left it in the whole file:
    void solve() {
    //
    }
    spoiler
    5ae43ddd48cb6764306187.jpeg

  • MyForm.cpp - needed only to define the entry point to the application, nothing happens there.
    spoiler
    5ae43e12a57d5011363197.jpeg


Perhaps I messed up with the namespace or the connection, but if you create the H1.h header, where to write:
#include "source.cpp"
void solve();

- also does not work (
In Google I found information about redefining the same function in several files, but I have only 1 file with a definition and I include it only 1 time;
If the thought is that the error is due to the fact that I somehow connect like this: Source.cpp -> MyForm.h -> MyForm.cpp... Tell me
how to do it right!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2018-04-28
@EvgenyProff

1. *.h files should contain only function headers and declarations of global variables.
2. *.cpp files are not written in #includes (only *.h is written in them), *.cpp are compiled separately and then the resulting object files are linked together into one executable.
3. To avoid including the same file multiple times, use #ifndef:

// файл MyForm.h
#ifndef __MYFORM__
#define __MYFORM__
// здесь содержимое файла
#endif

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question