# c file is only compiled when it is used by add\_subdirectory

**URL:** https://discourse.cmake.org/t/c-file-is-only-compiled-when-it-is-used-by-add-subdirectory/754
**Category:** Usage
**Tags:** gen:vs
**Created:** [March 3, 2020, 1:11pm UTC](https://discourse.cmake.org/t/c-file-is-only-compiled-when-it-is-used-by-add-subdirectory/754 "2020-03-03T13:11:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![MichaWiedenmann](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@MichaWiedenmann](https://discourse.cmake.org/u/MichaWiedenmann)
#### Post date: [March 3, 2020, 1:11pm UTC](https://discourse.cmake.org/t/c-file-is-only-compiled-when-it-is-used-by-add-subdirectory/754/1 "2020-03-03T13:11:14Z")

</div>

I have a C++ project with a `.c` and `.cpp` file:

```
# d:\top\sub\CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(foo CXX)
add_executable(foo WIN32 file1.cpp file2.c)

```

when generating a `vcxproj` the `.c` file gets listed as `NONE` and is not compiled:

```
<!-- generated by: cmake -G "Visual Studio 14 2015" d:\top\sub -->
<ItemGroup>
  <ClCompile Include="D:\uniplot\sub\file1.cpp" />
  <None Include="D:\uniplot\sub\file2.c" />
</ItemGroup>

```

While tracking down the issue, I noted that when I put a parent CMakeLists on top and use `add_subdirectory()` the file gets listed as `CICompile` and it works:

```
# d:\top\CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(top)
add_subdirectory(sub)

```

generates

```
<!-- generated by: cmake -G "Visual Studio 14 2015" d:\top -->
<ItemGroup>
  <ClCompile Include="D:\uniplot\sub\file1.cpp" />
  <ClCompile Include="D:\uniplot\sub\file2.c">
    <CompileAs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">CompileAsC</CompileAs>
    <CompileAs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">CompileAsC</CompileAs>
    <CompileAs Condition="'$(Configuration)|$(Platform)'=='MinSizeRel|Win32'">CompileAsC</CompileAs>
    <CompileAs Condition="'$(Configuration)|$(Platform)'=='RelWithDebInfo|Win32'">CompileAsC</CompileAs>
  </ClCompile>
</ItemGroup>

```

How can I make it work without the parent/outer layer?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [March 3, 2020, 2:13pm UTC](https://discourse.cmake.org/t/c-file-is-only-compiled-when-it-is-used-by-add-subdirectory/754/2 "2020-03-03T14:13:03Z")

</div>

I may be wrong but the issue may come from the fact that you’ve limited your project to C++ compiler only.  
In the case where you use add\_subdirectory you leave project languages to default which means C & C++ and this is why it works in that case.

---

<div class="post-metadata">

### Author: ![MichaWiedenmann](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@MichaWiedenmann](https://discourse.cmake.org/u/MichaWiedenmann)
#### Post date: [March 3, 2020, 2:22pm UTC](https://discourse.cmake.org/t/c-file-is-only-compiled-when-it-is-used-by-add-subdirectory/754/3 "2020-03-03T14:22:07Z")

</div>

I changed it to `project(foo CXX C)` and it seems to work.
