# Toggle shared/static library: MSVC fails (linker); but AppleClang, gcc (Linux, MinGW) work

**URL:** https://discourse.cmake.org/t/toggle-shared-static-library-msvc-fails-linker-but-appleclang-gcc-linux-mingw-work/3706
**Category:** Code
**Tags:** os:windows, comp:msvc
**Created:** [July 7, 2021, 2:24pm UTC](https://discourse.cmake.org/t/toggle-shared-static-library-msvc-fails-linker-but-appleclang-gcc-linux-mingw-work/3706 "2021-07-07T14:24:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![SamuelMarks](https://discourse.cmake.org/user_avatar/discourse.cmake.org/samuelmarks/32/1619_2.png) [@SamuelMarks](https://discourse.cmake.org/u/SamuelMarks)
#### Post date: [July 7, 2021, 2:24pm UTC](https://discourse.cmake.org/t/toggle-shared-static-library-msvc-fails-linker-but-appleclang-gcc-linux-mingw-work/3706/1 "2021-07-07T14:24:30Z")

</div>

I made a little GitHub repo based off the official `cmake\Help\guide\tutorial\Complete` at [`0fdfd6bf`](https://gitlab.kitware.com/cmake/cmake/-/tree/0fdfd6bf/Help/guide/tutorial/Complete). Kept things simple, all C code, two `CMakeLists.txt` (one library; one executable). And GitHub Actions so you can replicate in a pull-request (or just review my full log files):

[https://github.com/SamuelMarks/cmake-example-with-vcpkg/actions/runs/1004776006](https://github.com/SamuelMarks/cmake-example-with-vcpkg/actions/runs/1004776006)

What am I doing wrong?

> LINK : fatal error LNK1104: cannot open file ‘versions.lib’

Thanks

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [July 7, 2021, 2:36pm UTC](https://discourse.cmake.org/t/toggle-shared-static-library-msvc-fails-linker-but-appleclang-gcc-linux-mingw-work/3706/2 "2021-07-07T14:36:33Z")

</div>

I suspect that you export no symbols. Set the default visibility to hidden for gcc to get the same and only export the needed symbols.

---

<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: [July 7, 2021, 2:53pm UTC](https://discourse.cmake.org/t/toggle-shared-static-library-msvc-fails-linker-but-appleclang-gcc-linux-mingw-work/3706/3 "2021-07-07T14:53:04Z")

</div>

Indeed. `versions.h` has `declspec` stuff commented out. The linker will not create a `.lib` file if no symbols are exported.

---

<div class="post-metadata">

### Author: ![SamuelMarks](https://discourse.cmake.org/user_avatar/discourse.cmake.org/samuelmarks/32/1619_2.png) [@SamuelMarks](https://discourse.cmake.org/u/SamuelMarks)
#### Post date: [July 7, 2021, 2:57pm UTC](https://discourse.cmake.org/t/toggle-shared-static-library-msvc-fails-linker-but-appleclang-gcc-linux-mingw-work/3706/4 "2021-07-07T14:57:27Z")

</div>

BTW: I’m hacking around with [`generate_export_header`](https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html) now… maybe if I manually set the visibility for the function and array in my `versions.h` then it’ll work (just going off your suggestion)

EDIT: The generated header file is not findable even after adding its directory to `target_include_directories` and trying `""` and `<>` with basename, and various levels of relative paths. Weird.

---

<div class="post-metadata">

### Author: ![SamuelMarks](https://discourse.cmake.org/user_avatar/discourse.cmake.org/samuelmarks/32/1619_2.png) [@SamuelMarks](https://discourse.cmake.org/u/SamuelMarks)
#### Post date: [July 8, 2021, 1:12pm UTC](https://discourse.cmake.org/t/toggle-shared-static-library-msvc-fails-linker-but-appleclang-gcc-linux-mingw-work/3706/5 "2021-07-08T13:12:16Z")

</div>

Ended up cheating with:

```cmake
set(_export_file "${CMAKE_CURRENT_SOURCE_DIR}/${LIBRARY_NAME}_export.h")
generate_export_header("${LIBRARY_NAME}" EXPORT_FILE_NAME "${_export_file}")

```

> <https://github.com/SamuelMarks/cmake-example-with-vcpkg/commit/e4e99106b#diff-5f7f19002d021ab261d5dce92dd114f71e0ab6c7ecfbcad2a505fa4f78a2c6dd>

Unfortunately `${CMAKE_CURRENT_SOURCE_DIR}` was the only thing that worked. Tried everything from relative paths to no `EXPORT_FILE_NAME` to completely different targets.

* * *

BTW: I actually did solve this linker error. With this export header setup properly it actually ran.
