0

I'm having some serious issues with Visual Studio 2013. I have C++ console project with 2 files: Main.cpp and A.cpp, I'm importing A.cpp with #include "A.cpp" in Main.cpp.

Here is the problem: When I edit A.cpp and run Main.cpp, the changes do not occur. I have to change Main.cpp as well and only then will Visual Studio notice the change and recompile everything properly.

This is extremely annoying when I'm trying to change something in A.cpp, any idea how to fix this?

EDIT:

I have tried renaming A.cpp to A.hpp and include A.hpp from Main.cpp with same result: changes to A.hpp do not occur in build until I change Main.cpp as well. I have tried putting A.hpp to both header files and source files in the solution explorer, still the same results.

4
  • 3
    Add A.cpp into VS project. BTW, unless you know what you are doing, you should never include .cpp files into each other. C++ is not Java. Commented Dec 17, 2015 at 11:48
  • And how do I add A.cpp to the VS project? I see both Main.cpp and A.cpp in the Source Files folder in the Solution explorer. Commented Dec 17, 2015 at 12:00
  • @kajacx, do you have A.h. If not, then create one and add it. You can show a minimal working example so people can judge what's wrong you're are doing. Commented Dec 17, 2015 at 12:28
  • @kajacx That means that A.cpp is already included in the project. Do what user @VolAnd suggested and create a corresponding header file A.h and include it instead of A.cpp in main. Commented Dec 17, 2015 at 12:28

1 Answer 1

2

Never do #include "A.cpp"! Only h-files (or hpp) should be included with #include. In case you want use in Main.cpp some functions defined in A.cpp I have to create A.h (and do not change A.cpp) with declarations of that functions. Of course, you need add both Main.cpp and A.cpp to project.

NOTE: If you add A.cpp into VS project and leave #include "A.cpp" (not #include "A.h") this will lead to a problem with redefinition of functions from A.cpp.

See simple explanation in C++ forum and other examples

Sign up to request clarification or add additional context in comments.

3 Comments

He should add it as "C++ header file" (can be changed in file properties).
Your advice is sound in general. However, there are some cases when you do want to include "cpp" files. I can remember it is used in a technique that lets you implement templates in a "cpp" file: codeproject.com/Articles/48575/…
@MarkoPopovic Yes, my answer is quite general, but it is for general question. In any case, thanks for link, perhaps it will be useful for kajacx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.