header files - first step

Hello to all forum members,
Probably more than one knows this task, but although it seems very simple, it completely does not go out.
I have to create 3 files: use.cpp my.cpp and my.h

use.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "my.h"

int main()
{
	foo = 7;
	print_foo();
	print(99);

	return 0;
}


my.h
1
2
3
 extern int foo;
void print_foo();
void print(int);


my.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "my.h"


using namespace std;

int foo;

void print_foo() {
	cout << foo;
	
}

void print(int i) {
	cout << i;
	
} 



It's trivial, but as I try to compile it in Visual Studio it gets an error:

The "CL.exe" command terminated by code 2.

And when he tries this version in Dev C ++ he compiles, but the console does not display the results.

What am I doing wrong?!
Kinda a side comment, but noticed you forgot the std:: on your couts
I have not forgotten about std ::, I used "use namespace std;"
Kinda a side comment, but noticed you forgot the std:: on your couts

He has using namespace std;


Also, the program ran fine when I tried it on Visual Studio. I believe Visual Studio requires the "pch.h" header : #include "pch.h"

Make sure that header is in BOTH .cpp files

If you're using an older version, you have the other header that I forgot, but make sure you have it on both .cpp files or it wont compile.
Last edited on
Is that actually all the information that VS is giving you? There isn't a more-detailed error message?

It compiles and links for me... something might be messed up with your project settings. Try making an empty project in C++ and re-create all three files.

Making it be an empty project is important because then you don't have to detail with that precompiled header complexity.

you forgot the std:: on your couts
burzogrzmot has "using namespace std;" in my.cpp
Last edited on
Oops missed it sry guys
In an empty project, an error about "unable to find a file" pops up at all, but does not provide more information. Just enough. And by the project opened in the console it gives you such a mistake:

Error MSB6006 Command "CL.exe" terminated by code 2. Task C: \ Program Files (x86) \ Microsoft Visual Studio \ 2019 \ Community \ MSBuild \ Microsoft \ VC \ v160 \ Microsoft.CppCommon.targets 429

My Visual Studio 2019


Wszystkie nagłówki są poprawne. Czyli to będzie problem z moim Visual Studio?
A dlaczego ten kod nie pokazuje w konsoli żadnych danych wyjściowych w Dev C++?

158/5000
All headers are correct. So it will be a problem with my Visual Studio?
And why does not this code show any output in Dev C ++ on the console?
Last edited on
On Visual Studio in your open project, go to "View" and click "solution explorer". Look at the "Header Files" tab the pops up. Do you have my.h and my.cpp in there?
Also, the program ran fine when I tried it on Visual Studio. I believe Visual Studio requires the "pch.h" header : #include "pch.h"

this causes a lot of confusion. It depends on your project settings whether you need it or not. The default settings make you use it, but the default settings are more or less for students in school. Large projects may benefit from precompiled headers (not sure), but small ones, they cause as many problems as they solve and turning off is fine. We always turned it off on every new project.
I have been struggling with it for a few days. I tried to do this task using the "pch.h" file and the code :: block on another computer. The result is always similar: error - undefined functions
If it says undefined functions, it probably doesn't see your header or extra cpp file. Again, make sure they are inside of "Solution Explorer" under "Header Files", otherwise they will NOT be seen.
I started to do everything step by step. First I declared and defined in the file my.h int foo and print_foo (omitting the file my.cpp). Compiled it worked. Then I defined only the variable foo in the file my.cpp and in the file my.h I only declared - it worked !!! Moving everything in turn, I made a mistake in the declaration of the print function:
instead
print (int);
I typed
print (int i);

The compiler has displayed an error (and rightly). I corrected it and the error did not disappear. Whatever I did not make the mistake disappear. I removed the project and started to do it anew, step by step. This time (writing this "program" hundredth time) without making a mistake. Everything is compiled and works ... will someone explain to me what's going on? The code has not changed!
Last edited on
Hard to say without seeing the project file. If you want, you can open the .*proj file in notepad/wordpad (I think it's .vxproj or something similar) and paste the XML comments here, both of the project that had compile errors, and the project that compiled correctly, and we can compare the differences.
A note on Visual Studio

This error:

The "CL.exe" command terminated by code 2.


Means the compiler crashed.

I get this on VS2019 (not so much on VS2017) in certain situations.

I don't see this in your code, but when I write (accidentally)

1
2
3
4
int foo()
{

}


...forgetting the return, VS2019 crashes instead of informing of the missing return.

By default, if you start a "console" project where the compiler fills in the project with a basic main function, it usually enables pre-compiled headers (the PCH file mentioned in post(s) above).

If you start an empty project, it usually doesn't.

If you don't use or have pre-compiled headers in your project, or you want to remove them, you must manually disable this setting if it was automatically put in the project definition. There will be one "CPP" file with "create pch" and the others will "use PCH", but you need "no using" pch.

PCH is ok to use, but if it gets in the way at all, especially for student projects, it's best not to have them.

Although it is an extra step, I can recommend checking your installation so as to include Clang. VS2019 can download and configure Clang 8 in one step. It is never selected on a new project by default. You can, however, choose clang as the toolset. If/when you do, VS2019 copies (or leaves in place) some of the settings for MSVC compiler. You have to "shake it" - close the project, then re-open - it "figures it out", and then Clang works quite well (better than MSVC).

Topic archived. No new replies allowed.