Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileno error on windows MSYS2 mingw64 #2541

Closed
billcxx opened this issue Nov 9, 2022 · 3 comments
Closed

fileno error on windows MSYS2 mingw64 #2541

billcxx opened this issue Nov 9, 2022 · 3 comments

Comments

@billcxx
Copy link

billcxx commented Nov 9, 2022

I git clone spdlog and try to build it in the MSYS2 mingw64 terminal on Win10.
cmake configuration is fine but make gives the following error.

$ make
[ 10%] Building CXX object CMakeFiles/spdlog.dir/src/spdlog.cpp.obj
In file included from /home/00010/spdlog/src/spdlog.cpp:12:
/home/00010/spdlog/include/spdlog/details/os-inl.h: In function ‘size_t spdlog::details::os::filesize(FILE*)’:
/home/00010/spdlog/include/spdlog/details/os-inl.h:237:16: error: ‘::fileno’ has not been declared
  237 |     int fd = ::fileno(f);
      |                ^~~~~~
/home/00010/spdlog/include/spdlog/details/os-inl.h: In function ‘bool spdlog::details::os::in_terminal(FILE*)’:
/home/00010/spdlog/include/spdlog/details/os-inl.h:449:21: error: ‘fileno’ was not declared in this scope; did you mean ‘file’?
  449 |     return ::isatty(fileno(file)) != 0;
      |                     ^~~~~~
      |                     file
make[2]: *** [CMakeFiles/spdlog.dir/build.make:76: CMakeFiles/spdlog.dir/src/spdlog.cpp.obj] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/spdlog.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

I know this is the same error as in #1581 which is marked as fixed.
I'm not sure if I'm missing any steps in building spdlog in MSYS2 mingw64.

@tt4g
Copy link
Contributor

tt4g commented Nov 9, 2022

Yes, it should be fixed.

See the fix commit ccffb6e.
The fix is simple: if CMAKE_SYSTEM_NAME is CYGWIN or MSYS, set CMAKE_CXX_EXTENSIONS=ON.
This is a setting to address the problem that fileno() is not defined unless -std=gnu***, as noted in this comment.

However, if you are using CMake installed on Windows, CMAKE_SYSTEM_NAME will not be CYGWIN or MSYS.
See: #2532, https://gitlab.kitware.com/cmake/cmake/-/issues/24125, https://discourse.cmake.org/t/how-to-check-that-the-current-compiler-is-gcc-msys-when-using-ninja-generator/6698

You are probably using CMake for Windows.
You should install CMake for MSYS or set cmake -DCMAKE_CXX_EXTENSIONS=ON manually on the command line.

@billcxx
Copy link
Author

billcxx commented Nov 9, 2022

Thanks for the quick reply!

My problem is indeed not using the correct cmake, but slightly different from your guess.

When I install the msys cmake, I install through this command pacman -S cmake which install the msys version of the cmake, not the mingw64 version of the cmake! This matters.

Today, I opened the mingw64 terminal and used the cmake located in /c/msys64/usr/bin/ (this is wrong). The mingw64 cmake should located in /c/msys64/mingw64/bin/, if you have not installed the mingw-w64-x86_64-cmake, the terminal will find the wrong one to use (like in my case, it found the msys cmake). If I switch to the msys terminal and build spdlog from here, everything works.

BTW, when I use the wrong cmake, it gives some warning that System is unknown to cmake.
Hope this will help other msys newbies like me.

@billcxx billcxx closed this as completed Nov 9, 2022
@billcxx
Copy link
Author

billcxx commented Nov 9, 2022

Forget to mention, the same applies to mingw64.
To compile spdlog in mingw64 terminal, you need to install everything in msys2 with the prefix mingw-w64-x86_64

With mingw64, the spdlog library builds fine, but there are some problems when building the example. I guess this is not spdlog's problem, so closing this issue.

FAILED: example/example.exe
cmd.exe /C "cd . && C:\msys64\mingw64\bin\c++.exe -O3 -DNDEBUG  example/CMakeFiles/example.dir/example.cpp.obj -o example\example.exe -Wl,--out-implib,example\libexample.dll.a -Wl,--m
ajor-image-version,0,--minor-image-version,0  libspdlog.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x83c5): undefi
ned reference to `__imp_WSAStartup'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x83df): undefi
ned reference to `__imp_htons'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x8401): undefi
ned reference to `__imp_inet_pton'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x841d): undefi
ned reference to `__imp_socket'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x845a): undefi
ned reference to `__imp_setsockopt'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x86e9): undefi
ned reference to `__imp_WSAGetLastError'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x8714): undefi
ned reference to `__imp_WSAGetLastError'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x8727): undefi
ned reference to `__imp_closesocket'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x8735): undefi
ned reference to `__imp_WSACleanup'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x8755): undefi
ned reference to `__imp_WSAGetLastError'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x875e): undefi
ned reference to `__imp_WSACleanup'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x877e): undefi
ned reference to `__imp_WSAGetLastError'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text+0x8787): undefi
ned reference to `__imp_WSACleanup'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text$_ZN6spdlog5sink
s8udp_sinkISt5mutexE8sink_it_ERKNS_7details7log_msgE[_ZN6spdlog5sinks8udp_sinkISt5mutexE8sink_it_ERKNS_7details7log_msgE]+0x6d): undefined reference to `__imp_sendto'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text$_ZN6spdlog5sink
s8udp_sinkISt5mutexED1Ev[_ZN6spdlog5sinks8udp_sinkISt5mutexED1Ev]+0x1e): undefined reference to `__imp_closesocket'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text$_ZN6spdlog5sink
s8udp_sinkISt5mutexED1Ev[_ZN6spdlog5sinks8udp_sinkISt5mutexED1Ev]+0x2c): undefined reference to `__imp_WSACleanup'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text$_ZN6spdlog5sink
s8udp_sinkISt5mutexED0Ev[_ZN6spdlog5sinks8udp_sinkISt5mutexED0Ev]+0x1e): undefined reference to `__imp_closesocket'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text$_ZN6spdlog5sink
s8udp_sinkISt5mutexED0Ev[_ZN6spdlog5sinks8udp_sinkISt5mutexED0Ev]+0x2c): undefined reference to `__imp_WSACleanup'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text$_ZNSt23_Sp_coun
ted_ptr_inplaceIN6spdlog5sinks8udp_sinkISt5mutexEESaIvELN9__gnu_cxx12_Lock_policyE2EE10_M_disposeEv[_ZNSt23_Sp_counted_ptr_inplaceIN6spdlog5sinks8udp_sinkISt5mutexEESaIvELN9__gnu_cxx1
2_Lock_policyE2EE10_M_disposeEv]+0x3e): undefined reference to `__imp_WSACleanup'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example/CMakeFiles/example.dir/example.cpp.obj:example.cpp:(.text$_ZNSt23_Sp_coun
ted_ptr_inplaceIN6spdlog5sinks8udp_sinkISt5mutexEESaIvELN9__gnu_cxx12_Lock_policyE2EE10_M_disposeEv[_ZNSt23_Sp_counted_ptr_inplaceIN6spdlog5sinks8udp_sinkISt5mutexEESaIvELN9__gnu_cxx1
2_Lock_policyE2EE10_M_disposeEv]+0x72): undefined reference to `__imp_closesocket'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants