[CMake] Header files excluded from build in Visual Studio

Claus Höfele claus at claushoefele.com
Fri Mar 13 00:47:08 EDT 2009


Thanks for the quick replies.

I'm using cmake 2.6.3 + Visual Studio 8 2005 SP1

Playing around further with the file properties in Visual Studio, it
looks like VS only checks the syntax of a file if the Tool property is
set to C/C++ Compiler Tool. However, this setting will cause the
header file to be compiled, which is obviously not what you want.

So the setting for a header file should be:
1.) Excluded From Build: No
2.) Tool: Custom Build Tool

2.) is already done, 1.) is not.

For my purposes, it was enough to change cmSourceFile::CheckExtension() like so:

  // Look for header files.
  if ...
  else
    {
    // This is a known header file extension.  The source cannot be compiled.
    if(!this->GetProperty("HEADER_FILE_ONLY"))
      {
      this->SetProperty("HEADER_FILE_ONLY", "1");
      }
    }

This allows me to override the HEADER_FILE_ONLY setting to false in my
CMakeLists.txt and produces the project files I want.

Would be great if a similar fix would make it into the next release.

-Claus



On Fri, Mar 13, 2009 at 1:47 PM, Bill Hoffman <bill.hoffman at kitware.com> wrote:
> Claus Höfele wrote:
>>
>> Hi everyone,
>>
>> I've recently switched to cmake to generate Visual Studio project
>> files for a project of mine and it's working great so far.
>>
>> One issue that bugs me is that header files end up being disabled in
>> Visual Studio (Excluded From Build property is set for the header
>> files). This prevents Visual Studio from syntax checking these files,
>> which is quite a bummer for me because I have a lot of inline code in
>> my header files.
>>
>> Setting the property HEADER_FILE_ONLY to false doesn't work because
>> the code in cmSourceFile::CheckExtension() forces this property to
>> true for headers files. The generator for Visual Studio then
>> automatically disables the file when it writes the project file.
>>
>> For Visual Studio, this behaviour doesn't make much sense because
>> header files are not compiled anyway. At the very minimum, I should be
>> able to override this setting.
>>
>> I wonder if there's a workaround for this problem without resorting to
>> building my own cmake executable.
>>
>
> Well, looks like you found the reason that this fix needs fixing!
>
> Sadly it does require a rebuild of CMake, there is no way to fix this
> without changing the c++ source code.
>
> -Bill
>


More information about the CMake mailing list