Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio compile option is grayed out for a single cpp file

I have been asked to help a colleague regarding a mysterious build error in one of the projects in a common solution. The build error happens in the linkage stage, a class constructor defined in a static lib (a.lib) is not found when it is used in a lib (b.lib) that includes that a.lib. After careful investigation I figured out that:

  1. The constructor is correctly defined in the class.
  2. The class is defined in a cpp file that belongs to the project.
  3. If a try to navigate to the cpp file or the class constructor with intellisense everything goes as expected.

vcxproj file ClCompile ItemGroup:

...
<ClCompile Include="Source\CMYKOVBLab.cpp" />
<ClInclude Include="Source\CMYKOBGLab.cpp" />  <--- The file is here.
<ClCompile Include="Source\CMYKOVBRGB.cpp" />
...

In summary, what is needed to link the library is there, but the linkage error persists.

One strange thing I noticed is the fact that the cpp with the class constructor not found could not be compiled by hitting Ctrl-F7 or the right button in a.lib. That option is grayed out, but only for that particular file. Moreover, when I build the a.lib library where this file should be compiled, it is not, and therefore no object file is created.

cpp file compilation not possible

What can be the cause? How can I fix it?

I am using Visual Studio Professional 2015 Update 3.

like image 487
FrankS101 Avatar asked Apr 27 '26 21:04

FrankS101


2 Answers

In VS 2015 the compile include files defined in the .vcxproj project file are expected to be in lexicographical order.

This is no longer needed in VS 2017 but when it is not respected, VS 2015 can create this kind of chaos.

Your colleague has modified manually the .vcxproj project file to include that file, but he has not respect the lexicographical order. You need simply to remove the file from the project and add it back again. Visual Studio will include it in the right place and your build will succeed.

Hope this can help other people too.

like image 133
FrankS101 Avatar answered Apr 29 '26 09:04

FrankS101


Your source file could not be compiled because it was added to the .vcxproj file in a "ClInclude" element, which is for headers. Fix it to ClCompile either manually or by removing and readding it in Visual Studio, then it will compile.

like image 24
Eram Avatar answered Apr 29 '26 10:04

Eram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!