# Generating a Visual Studio project including external MS projects

**URL:** <https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977>\
**Category:** Code\
**Created:** [April 10, 2020, 10:38pm UTC](https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977 "2020-04-10T22:38:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Capps\_Arlie](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Capps\_Arlie](https://discourse.cmake.org/u/Capps_Arlie)\
**Post date:** [April 10, 2020, 10:38pm UTC](https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977/1 "2020-04-10T22:38:16Z")

</div>

I have a large program, configured with CMake, that links against several (tens of) libraries. The libraries are built with MS Visual Studio 2017 (using Intel C++ and Fortran compilers): they have handwritten project.vcxproj and .vfproj files.

CMake generates the MS Visual Studio solution, and I’m impressed with how far I’ve gotten. But I’m stuck on a linking problem and a nagging cosmetic issue.

Here is a (distilled) example of what I’m trying to do.

```
# liba is Fortran
include_external_msproject(liba 
    ${CMAKE_SOURCE_DIR}/imports/liba/Win32/liba.vfproj
    TYPE 6989167D-11E4-40FE-8C1A-2192A86A7E90 
    GUID 0000AAAA-BBBB-CCCC-87EE-000000FFFFFF)

# libb is C++ and needs liba
include_external_msproject(libb
    ${CMAKE_SOURCE_DIR}/imports/liba/Win32/libb.vcxproj 
    GUID 00CC00AA-BBB2-C3CC-87E1-00FF00FF00FF
    liba)

set_property(TARGET liba PROPERTY FOLDER "Fortran Libraries")
set_property(TARGET libb PROPERTY FOLDER "C++ Libraries")

# prog_sources is defined earlier
add_executable(prog ${prog_sources})

add_dependencies(prog libb)

```

Questions:

1. The folder properties don’t work. When I opened the .sln file in Visual Studio, I hoped to see liba within the “Fortran Libraries” folder and libb inside the “C++ Libraries” folder, but there are no folders: all projects in the solution are at the top level. What am I doing wrong?

2. add\_dependencies(prog libb) adds a ProjectReference to libb inside prog.vcxproj, as I expected, but prog tries to link to libb.lib. Several of my libraries’ output file names don’t match the project name, so I get a linker error complaining about “missing libb.lib”. Is there a way to set the file name that the linker should look for?

Thanks,

Arlie Capps

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [April 10, 2020, 11:45pm UTC](https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977/2 "2020-04-10T23:45:57Z")

</div>

> [@Capps\_Arlie](#):
>
> Several of my libraries’ output file names don’t match the project name, so I get a linker error complaining about “missing libb.lib”. Is there a way to set the file name that the linker should look for?

You’ll probably need to hack together an `INTERFACE` target here I think. It seems that `include_external_msproject` makes a utility (`add_custom_target`-like) target internally. There seems to be some magic internally about how these things get linked to (and I don’t see a specific `.lib` path from CMake; VS may be making that assumption somewhere), but I can’t decipher it right now. @brad.king?

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [April 13, 2020, 11:52am UTC](https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977/3 "2020-04-13T11:52:31Z")

</div>

When linking to a target loaded via `include_external_msproject` we depend on Visual Studio’s [Link Library Dependencies](https://docs.microsoft.com/en-us/cpp/build/reference/linker-property-pages?view=vs-2019#link-library-dependencies) feature. That’s supposed to link the proper `.lib` automatically.

See code [here](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.17.1/Source/cmGlobalVisualStudio8Generator.cxx#L324-342) and [here](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.17.1/Source/cmVisualStudio10TargetGenerator.cxx#L3851-3855).

---

<div class="post-metadata">

**Author:** ![Capps\_Arlie](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Capps\_Arlie](https://discourse.cmake.org/u/Capps_Arlie)\
**Post date:** [April 14, 2020, 6:46pm UTC](https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977/4 "2020-04-14T18:46:40Z")

</div>

Brad King wrote:

> When linking to a target loaded via include\_external\_msproject we  
> depend on Visual Studio’s Link Library Dependencies feature.  
> That’s supposed to link the proper .lib automatically.  
> (Links to code: cmGlobalVisualStudio8Generator.cxx#L324-342 and  
> cmVisualStudio10TargetGenerator.cxx#L3851-3855.)

Brad,

I see the generated project file does have the Link Library Dependencies property set to True: thanks for pointing that out. Additionally, though, the generated project file explicitly names libb.lib:

```
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <ClCompile> .... stuff .... </ClCompile>
    .... more stuff ....
    <Link>
        <AdditionalDependencies>
            .... lots of libraries; 
            libb.lib; .... <!-- where does this name come from? -->
        </AdditionalDependencies>
        .... yet more stuff ....
    </Link>
</ItemDefinitionGroup>

```

Thanks,

Arlie

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [April 14, 2020, 7:12pm UTC](https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977/5 "2020-04-14T19:12:51Z")

</div>

> the generated project file explicitly names `libb.lib`

We have a similar case in the test suite [here](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.17.1/Tests/VSExternalInclude/CMakeLists.txt#L40) and that does not happen. Please zip up a minimal source tree showing the problem and attach the example here.

---

<div class="post-metadata">

**Author:** ![Capps\_Arlie](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Capps\_Arlie](https://discourse.cmake.org/u/Capps_Arlie)\
**Post date:** [April 16, 2020, 5:06pm UTC](https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977/6 "2020-04-16T17:06:20Z")

</div>

> > the generated project file explicitly names libb.lib

> We have a similar case in the test suite (link omitted) and that does not  
> happen. Please zip up a minimal source tree showing the problem and attach  
> the example here.

I’m having trouble making a minimal source tree. Additionally, I found some calls to target\_link\_libraries() that contributed at least some of the failures I’m finding, so I’ll continue to troubleshoot that way. I’ll send in a minimal reproducer if I can find one.

Thanks very much for your help.

---

<div class="post-metadata">

**Author:** ![Ixitxachitl](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/a87d85/32.png) [@Ixitxachitl](https://discourse.cmake.org/u/Ixitxachitl)\
**Post date:** [July 20, 2022, 1:57pm UTC](https://discourse.cmake.org/t/generating-a-visual-studio-project-including-external-ms-projects/977/7 "2022-07-20T13:57:33Z")

</div>

(I am aware that this thread is two years old).

> [@Capps\_Arlie](#):
>
> The folder properties don’t work.

Try adding:

`set_property(GLOBAL PROPERTY USE_FOLDERS ON)`
