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

On WIN32, fix compile errors on Clang with MSVC headers #718

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/libFLAC/CMakeLists.txt
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now it is only targeting when using Clang and MSVC headers, for MinGW headers contains unistd.h but MSVC ones does not. Adding cmake_policy is to suppress the warnings.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ if(FLAC__CPU_X86_64 OR FLAC__CPU_IA32)
if(WITH_AVX AND MSVC)
set_source_files_properties(fixed_intrin_avx2.c lpc_intrin_avx2.c stream_encoder_intrin_avx2.c lpc_intrin_fma.c PROPERTIES COMPILE_FLAGS /arch:AVX2)
endif()
if(WITH_AVX AND WIN32 AND CMAKE_C_COMPILER_ID MATCHES "Clang")
cmake_policy(PUSH)
cmake_policy(SET CMP0075 NEW)
check_include_file("unistd.h" HAVE_UNISTD_H)
if(NOT HAVE_UNISTD_H)
# Clang on Windows using MSVC headers
set_source_files_properties(fixed_intrin_avx2.c lpc_intrin_avx2.c stream_encoder_intrin_avx2.c lpc_intrin_fma.c PROPERTIES COMPILE_FLAGS -mavx2)
endif()
cmake_policy(POP)
endif()
else()
check_cpu_arch_arm64(FLAC__CPU_ARM64)
if(FLAC__CPU_ARM64)
Expand Down
2 changes: 1 addition & 1 deletion src/share/getopt/getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static char *posixly_correct;
/* Avoid depending on library functions or files
whose names are inconsistent. */

#ifndef getenv
#if !defined(getenv) && !(defined(__clang__) && defined(_MSC_VER))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too sure what this does, and why it doesn't work for your particular configuration. Maybe it can be removed completely? I think it catches some corner case that existed 30 years ago or something.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems on Windows, using Clang with MSVC headers would cause error: conflicting types for 'getenv'

X:\path\to\clang.exe -DHAVE_CONFIG_H -D_DARWIN_C_SOURCE -D_FORTIFY_SOURCE=2 -D_POSIX_PTHREAD_SEMANTICS -D_TANDEM_SOURCE -D__STDC_WANT_IEC_60559_BFP_EXT__ -D__STDC_WANT_IEC_60559_DFP_EXT__ -D__STDC_WANT_IEC_60559_FUNCS_EXT__ -D__STDC_WANT_IEC_60559_TYPES_EXT__ -D__STDC_WANT_LIB_EXT2__ -D__STDC_WANT_MATH_SPEC_FUNCS__ -IX:/path/to/flac/include -IX:/path/to/flac/build -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return -Wcast-align -Wnested-externs -Wshadow -Wundef -Wmissing-declarations -Winline  -DNDEBUG -O3 -DNDEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrt -Wdeclaration-after-statement -fstack-protector-strong -MD -MT src/share/getopt/CMakeFiles/getopt.dir/getopt.c.obj -MF src\share\getopt\CMakeFiles\getopt.dir\getopt.c.obj.d -o src/share/getopt/CMakeFiles/getopt.dir/getopt.c.obj -c X:/path/to/flac/src/share/getopt/getopt.c
X:/path/to/flac/src/share/getopt/getopt.c:225:14: error: conflicting types for 'getenv'
  225 | extern char *getenv (const char * name);
      |              ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stdlib.h:1184:28: note: previous declaration is here
 1184 |     _DCRTIMP char* __cdecl getenv(
      | 

Which is defined as

_Check_return_ _CRT_INSECURE_DEPRECATE(_dupenv_s)
_DCRTIMP char* __cdecl getenv(
    _In_z_ char const* _VarName
    );

in <stdlib.h>.

I think the best way to handle this error is to remove the extern declaration when it is clang and MSVC.

extern char *getenv (const char * name);
#endif

Expand Down
Loading