0

My header file contains, among other things:

#define PAUSE system("pause");
typedef unsigned char uint8;
static const double PI = 3.14159265358979323846;
static const double oneDegInRads = PI / 180;

I've been including this header file into many .cpp files all over the place without a single problem. As I want to clean up my code I was fixing warnings, mostly minor stuff like "cast double to float, possible loss of data", or "unsigned to signed mismatch" warnings. So I turned on the "Treat compiler warnings as errors" in the settings, so by using that I can have a strict assessment of how bad my code is and fix it accordingly. With it turned on, my solution won't build, and shows the error:

Code                         Description                                  File   
C2220     "Warning treated as error - no 'object' file generated     myDefinitions.h

I know why this is happening, as I turned that specific setting on. I have tried suppressing and disabling this warning with:

#pragma warning (disable : 2220)

And I've tried also following the "One CPP for each header rule", making a "myDefinitions.cpp" and including only the header and nothing else, but the error message still comes up.

So my questions are:

1: Can I have a header file without a .cpp one (without ignoring the warnings)?

2: If not, how do I make my .cpp, as I've already tried but failed?

3: What better solution can you suggest to have one page where all your important definitions that you use very frequently are? I need to make it possible to include them anywhere you want.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119

3 Answers3

1

Visual Studio is trying to compile your myDefinitions.h header file to an object file.

It should not do that! Only .cpp source files should be translated to object files.

I guess you probably renamed a .cpp file to myDefinitions.h at some point, and Visual Studio didn't understand you wanted to change its file "type".

Try to delete your myDefinitions.h file, and then create a new myDefinitions.h file, choosing the correct file type from the new file dialog. You can then put the old content into that new header.

Johan Boulé
  • 1,936
  • 15
  • 19
  • Wow, that certainly did SOMETHING, as now instead of getting about 5 of these errors I only get one, and it's not the myDefinitions file, which could be another problem altogether. Interesting. Let me get back to you while I figure this out. – Zebrafish Apr 30 '16 at 18:46
  • Holy cow, man, that did it. All this reading I did with people saying you gotta have a cpp, that's strange. So I completely deleted the header, created a new one, and the error went away. Do you mind explaining why this happened? Edit: Never mind, your original answer explains it. I'm just wondering what the hell I did. Many thanks. – Zebrafish Apr 30 '16 at 18:53
  • Actually, it wasn't that. The reason it showed the error was that in my myDefinitions.h there were implicit type conversions, like: 'initializing': truncation from 'double' to 'float' Nevertheless your answer led me to the right solution, so thanks. – Zebrafish Apr 30 '16 at 19:02
1

OK, thank you to both Ton Plooij and Johan Boule. The reason this was happening was that in my header file there were implicit type conversions such as:

C4305: 'initializing': truncation from 'double' to 'float'

In the Error tab it only said:

"Warning treated as error - no 'object' file generated

Which led me believe that it was LOOKING FOR a CPP file, which is strange because header files aren't made into object files. What I didn't know is that I could find the "Warning" that triggered the "Error" message by clicking on the "Warnings" tab. And it showed exactly which "Warnings" made the "Error" show up.

Thanks again for your help.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • 1
    Wow, so Visual studio was giving a rather confusing message here : "no 'object' file generated" and then "myDefinitions.h".. This really makes think it's trying to compile a myDefinitions.obj file. Now I understand that the message only means it's bailing out at this point where it encountered the warning, because warnings are treated as error, and, thus, is not even bothering with the object file creation. Phew. – Johan Boulé Apr 30 '16 at 21:35
0

The C2220 error occurs because there are warnings. Since there are warnings, no object or executable will be generated. You can't disable this C2220, it's what you enabled with the /WX option. So, either fix all warnings or disable the /WX (Treat all warnings as errors) option.

Ton Plooij
  • 2,583
  • 12
  • 15
  • Actually, you are correct. The reason it showed that error in the "Error" tab was because there were warnings about implicit type conversions in myDefinitions.h, like C4305: 'initializing': truncation from 'double' to 'float'. Though I did not know that I could find which specific error by clicking on the "Warnings" tab. Upvoted. Though I thought you could disable or suppress these warnings, unlike what you're saying. – Zebrafish Apr 30 '16 at 19:07