# ld unable to find iostreams functions under MSYS2

**URL:** https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330
**Category:** Usage
**Tags:** comp:gcc, os:windows, gen:ninja
**Created:** [December 9, 2020, 7:09pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330 "2020-12-09T19:09:17Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![R030t1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@R030t1](https://discourse.cmake.org/u/R030t1)
#### Post date: [December 9, 2020, 7:09pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/1 "2020-12-09T19:09:17Z")

</div>

If I include `iostreams` I receive linking errors running under MSYS2 targeting x86\_64:

`cmake . -GNinja`  
`ninja`

`... undefined reference to std::ios_base::Init`, etc. If I try to use `cout` it fails to find those methods as well.

I’ve tried to explicitly link in libstdc++ to no avail.

---

<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: [December 9, 2020, 7:33pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/2 "2020-12-09T19:33:57Z")

</div>

Do you have example code you could share?

---

<div class="post-metadata">

### Author: ![R030t1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@R030t1](https://discourse.cmake.org/u/R030t1)
#### Post date: [December 9, 2020, 8:18pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/3 "2020-12-09T20:18:21Z")

</div>

```auto
#include <Windows.h>
#include <iostream>
using namespace std;

int APIENTRY wWinMain(
	_In_ HINSTANCE	hInstance,
	_In_opt_ HINSTANCE	hPrevInstance,
	_In_ LPWSTR lpCmdLine,
	_In_ int nCmdShow
) {
    return EXIT_SUCCESS;
}

```

```cmake
cmake_minimum_required(VERSION 3.10.2)
project(cmaketest VERSION 0.0.1)

set(CMAKE_MODULE_PATH "cmake")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++2a -municode")

add_executable(cmaketest cmaketest.cc)
target_include_directories(cmaketest PUBLIC "${PROJECT_BINARY_DIR}")
target_link_libraries(cmaketest)

```

```auto
$ ninja
[2/2] Linking CXX executable cmaketest
FAILED: cmaketest
: && /mingw64/bin/CC.exe -std=gnu++2a -municode CMakeFiles/cmaketest.dir/cmaketest.cc.obj -o cmaketest && :
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/cmaketest.dir/cmaketest.cc.obj:cmaketest.cc:(.text+0x2b): undefined reference to `std::ios_base::Init::~Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/cmaketest.dir/cmaketest.cc.obj:cmaketest.cc:(.text+0x5b): undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

```

---

<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: [December 9, 2020, 8:44pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/4 "2020-12-09T20:44:02Z")

</div>

You must use the C++ compiler. Not sure what CMake thinks about .cc but common source file extensions for C++ are .cpp or .cxx

It seems to use cc.exe but this is wrong

---

<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: [December 9, 2020, 10:20pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/5 "2020-12-09T22:20:03Z")

</div>

CMake does think it is a C++ file (and I’d assume a C compiler would choke on that `gnu++2a` standard):

> [@R030t1](#):
>
> `Linking CXX executable cmaketest`

I suspect the toolchain is not working properly. Does it compile on the command line without the `-c` step in between?

```shell
CC -std=gnu++2a -municode -o test cmaketest.cc

```

---

<div class="post-metadata">

### Author: ![R030t1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@R030t1](https://discourse.cmake.org/u/R030t1)
#### Post date: [December 9, 2020, 11:09pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/6 "2020-12-09T23:09:05Z")

</div>

`cc` fails the same way, but `x86_64-w64-mingw32-g++` works just fine. I was originally just using `x86_64-w64-mingw32-g++` to build it manually.

---

<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: [December 9, 2020, 11:32pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/7 "2020-12-09T23:32:24Z")

</div>

Hmm. I wonder why CMake is selecting `cc.exe` rather than the `g++` executable. Do you have `CXX` set in the environment at all?

---

<div class="post-metadata">

### Author: ![R030t1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@R030t1](https://discourse.cmake.org/u/R030t1)
#### Post date: [December 11, 2020, 5:51pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/8 "2020-12-11T17:51:17Z")

</div>

I don’t, and setting it doesn’t seem to change the behavior of cmake. But in the MSYS2 MinGW shell `/mingw64/bin/cc.exe -v` indicates that it _should_ be the same compiler. Looking at the compiler invocation I can’t see anything that would change behavior.

---

<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: [December 11, 2020, 6:33pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/9 "2020-12-11T18:33:42Z")

</div>

Some programs look at the name they are called with and change behavior based on that. For example, `gcc foo.cxx` works for compilation and fails at the link step just like here:

```auto
$ cat foo.cxx
#include <iostream>

int main(int argc, char* argv[]) {
    return 0;
}
$ gcc foo.cxx
/usr/bin/ld: /tmp/cc6G9wwR.o: in function `__static_initialization_and_destruction_0(int, int)':
foo.cxx:(.text+0x35): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: foo.cxx:(.text+0x44): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

```

So I think figuring out why CMake thinks `cc.exe` is your C++ compiler is what needs investigated.

@brad.king Have you seen this?

---

<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: [December 11, 2020, 6:54pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/10 "2020-12-11T18:54:33Z")

</div>

I’ve seen this happen when someone puts `CXX=gcc` or `CXX=clang` in their environment, or uses `-DCMAKE_CXX_COMPILER=` with one of those.

In this case, `CXX=CC` might do it.

---

<div class="post-metadata">

### Author: ![R030t1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@R030t1](https://discourse.cmake.org/u/R030t1)
#### Post date: [December 11, 2020, 8:03pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/11 "2020-12-11T20:03:48Z")

</div>

That seems to be the issue, most easily solved (due to how MinGW sets up the env) by using `source_file_properties` to set `LANGUAGE CXX` for the files.

Can .cc be added as a C++ extension? Unsure if you’d suggest I make a ticket for this.

---

<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: [December 12, 2020, 12:23am UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/12 "2020-12-12T00:23:39Z")

</div>

`.cc` _is_ a C++ extension (see [here](https://gitlab.kitware.com/cmake/cmake/-/blob/0064981f6fb5ab96cb09bfdc456f6250ee4cb311/Modules/CMakeCXXCompiler.cmake.in#L47)). And CMake already does think it’s compiling C++ code:

> [@R030t1](#):
>
> `[2/2] Linking CXX executable cmaketest`

The problem is that a C compiler is being selected as your C++ compiler. That is what needs to be figured out.

---

<div class="post-metadata">

### Author: ![R030t1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@R030t1](https://discourse.cmake.org/u/R030t1)
#### Post date: [December 12, 2020, 4:33am UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/13 "2020-12-12T04:33:44Z")

</div>

On one MSYS2 installation this works but on another it doesn’t. I can’t figure out why `cc.exe` is being run, and the environments were prepared more or less the same way using MSYS2’s package manager.

On one environment, the files properties changed the behavior.

---

<div class="post-metadata">

### Author: ![ololuki](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ololuki/32/2203_2.png) [@ololuki](https://discourse.cmake.org/u/ololuki)
#### Post date: [February 20, 2022, 1:42pm UTC](https://discourse.cmake.org/t/ld-unable-to-find-iostreams-functions-under-msys2/2330/14 "2022-02-20T13:42:10Z")

</div>

The problem with linker could be caused by using unaligned versions of cmake and gcc. If you want to use mingw64 gcc you must also use mingw64 cmake.  
If cmake is not alligned with gcc, you get following error in configure phase:

```auto
-- The CXX compiler identification is GNU 11.2.0
System is unknown to cmake, create:
Platform/MINGW64_NT-6.1-7601 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake

```

You need to install mingw64 cmake:

```auto
pacman -S mingw-w64-x86_64-cmake

```

See: [mingw - CMake + MSys2 undefined references to everything (including c++ runtime) - Stack Overflow](https://stackoverflow.com/a/71193975/7419651)
