You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MSVC: Configure test failure on non-AMD64 hosts with default VS 2015+ target architecture #4320
The generated c source file for test conf.CheckFunc('printf') in test\Configure\config-h.py produces a linker error due to an unresolved printf symbol for non-AMD64 Windows hosts using the default target architecture for VS2015 and later.
The crux of the problem is using printf without an explicit #include <stdio.h> for VS2015 and later with a target architecture that is not AMD64.
For VS2015 and later on non-AMD64 hosts and using the default target architecture, the config-h.py test fails due to printf:
STDOUT =========================================================================
2c2
< Checking for C function printf()... yes
---
> Checking for C function printf()... no
It appears that starting with VS2015, printf is only exported from the AMD64 libraries. The MSVC <stdio.h> header generates inline functions that invoke a printf compatible function that is exported from the architecture specific libraries. Without inclusion of <stdio.h>, the test will fail for VS2015+ when the target architecture is not AMD64.
Test environments:
Windows 10 x86 (VMWare) Host with VS2019 BuildTools (default target architecture is 'x86')
Windows 11 ARM64 Host with VS2022 Community installed (WIP branch: default target architecture is 'ARM64')
Generated c source file for conf.CheckFunc('printf')
The generated c source file is:
#include <assert.h>
#ifdef __cplusplus
extern "C"
#endif
char printf(void);
#if _MSC_VER && !__INTEL_COMPILER
#pragma function(printf)
#endif
int main(void) {
#if defined (__stub_printf) || defined (__stub___printf)
#error "printf has a GNU stub, cannot check"
#else
printf();
#endif
return 0;
}
VS2015+ with a non-AMD64 default target architecture results in linker error:
error LNK2019: unresolved external symbol _printf referenced in function _main
Documentation: 2.1. Building Simple C / C++ Programs
Here's the famous "Hello, World!" program in C:
int
main()
{
printf("Hello, world!\n");
}
And here's how to build it using SCons. Save the code above into hello.c, and enter the following into a file named SConstruct:
Program('hello.c')
The example fails to build for VS2015 and later if the default target architecture is not 'AMD64'. For example, VS2019 on a 32-bit Windows host.
Adding #include <stdio.h> will build with the default target architecture on non-AMD64 Windows hosts with VS2015 and later:
#include <stdio.h>
int
main()
{
printf("Hello, world!\n");
}
The #include <stdio.h> causes the MSVC c preprocessor to generate inline functions for printf that dispatch to functions that are exported from the architecture-specific libraries.
@mwichmann When you have some time, this one should not take too long to resolve one way or the other.
The easiest solutions are to pick a function other than printf or explicitly include the stdio header (which may not be as easy in the test suite).
With c/c++ headers using inline generation, renaming via the preprocessor, and the like, not explicitly including the associated header seems questionable. Fifteen years ago, probably not an issue.
In this case, printf is supported when the header is included but mostly because it is remapped to a function that is exported.
Example/test/configure code should be complete according to current language standards, not just "ancient C". We've fixed a bunch of those over time, including your #4413 above. So including a required header is not just fine, it's the right thing to do. Is it that simple? I see no problem with picking a different function to use either, I know in past lives I've run into some interesting problems with GCC which remaps some standard library names to a different name so you don't see the expected symbol in a generated binary (in fact, that scenario seems to be present in your research above with the test code punting on gcc)
So including a required header is not just fine, it's the right thing to do. Is it that simple?
Unfortunately, No. It took a quick test to remember the issues.
The root cause is has to do with the code generation for CheckFunc.
Inlining for msvc-like compilers that define _MSC_VER is explicitly disabled via generation of a #pragma function() block.
The MSVC #pragma function() documentation:
Tells the compiler to generate calls to functions specified in the pragma's argument list, instead of inlining them.
Case 1: the generated c source file without any modifications is (r1 = conf.CheckFunc('printf')):
#include<assert.h>#if_MSC_VER&& !__INTEL_COMPILER#pragma function(printf)
#endifintmain(void) {
#if defined (__stub_printf) || defined (__stub___printf)
#error "printf has a GNU stub, cannot check"
#elseprintf();
#endifreturn0;
}
A warning is produced but linking is successful since the function is available for a 64-bit amd64 build on 64-bit windows:
test.c(5): warning C4163: 'printf': not available as an intrinsic function
Case 2: the generated c source file including the stdio header is (r1 = conf.CheckFunc('printf', header='#include <stdio.h>')):
#include<assert.h>#include<stdio.h>#if_MSC_VER&& !__INTEL_COMPILER#pragma function(printf)
#endifintmain(void) {
#if defined (__stub_printf) || defined (__stub___printf)
#error "printf has a GNU stub, cannot check"
#elseprintf();
#endifreturn0;
}
A compiler warning and error is produced due to the usage of the printf function:
test.c(5): warning C4163: 'printf': not available as an intrinsic function
test.c(12): error C2198: 'printf': too few arguments for call
Since a signature is not generated, including the header is not going to work unless the function being tested takes no arguments.
Resolution options:
Comment out the printf test case
Find another standard library function that is exported in modern msvc runtime libraries on all platforms (64/amd64, 64/arm64, 32/x86).
Ignore the problem and quietly close the issue (nothing to see here...)
Always interesting when it's a test that fails. Test design is hard, and people get it wrong because they got lucky and it passed "by chance" (see the recent PCH discussion, where "order matters", but the existing test passed because the first choice happened to have the sources in an order that worked).
If there's an easy choice for a standard C library function that doesn't trigger these problems, then great - but I wouldn't spend an age looking. Calling the function in a way that modern compilers don't object to would help (if you include the header to get the proto, then match the proto). I've been in exactly that place before - code generated automatically from a symbol, so you don't know the calling convention, you just want to know the symbol links ok. Compilers hadn't gotten as fussy circa the middle of the 2000 decade, so I don't think we had to solve it.
To be fair, I usually only run the MSVC/MSVS (~79 tests) tests on arm64. The configure test failed when running the entire suite (which takes forever on the low-end arm64 test box). I think I also tried in a 32-bit virtual machine and the test failed as well but I'm not positive.
It strikes me as odd that the MS runtime library exports aren't the same for all of the platform versions of the libraries.
I don't really understand why the intrinsics/inline methods are explicitly guarded against for MSVC. It appears that the configure test wants the function to be exported from the runtime or fail otherwise.
I'm not exactly sure of the utility of the configure behavior as the "correct" usage of printf via including the header and an appropriate signature will work on any of the platforms. The fact that it is not exported from the runtime or necessarily implemented as the literal symbol printf seems less important than actually being able to use the printf symbol successfully.
The two CheckFunc methods (SCons/SConf.py and SCons/Conftest.py) accept a header definition.
From the CheckFunc documentation in SCons/Conftest.py:
defCheckFunc(context, function_name, header=None, language=None):
""" Configure check for a function "function_name". "language" should be "C" or "C++" and is used to select the compiler. Default is "C". Optional "header" can be defined to define a function prototype, include a header file or anything else that comes before main(). ... """
...
Passing a header to provide a prototype for functions that take arguments can be an issue with "modern" compilers by default as the generated function invocation has an empty argument list.
A working solution is to allow a function arguments string to be defined for the generated function invocation.
CheckFunc in SCons/Conftest.py:
defCheckFunc(context, function_name, header=None, language=None, funcargs=None):
""" Configure check for a function "function_name". "language" should be "C" or "C++" and is used to select the compiler. Default is "C". Optional "header" can be defined to define a function prototype, include a header file or anything else that comes before main(). Optional "funcargs" can be defined to define an argument list for the generated function invocation. ... """
...
ifnotfuncargs:
funcargs=''
...
text="""... #if defined (__stub_%(name)s) || defined (__stub___%(name)s) #error "%(name)s has a GNU stub, cannot check"#else %(name)s(%(args)s);#endif..."""% { 'name': function_name,
'include': includetext,
'hdr': header,
'args': funcargs}
...
Sounds good to me... is something also needed for the call to memmove, which is in <string.h> and takes three arguments? Come to think of it, why are there two checks for "C library" functions? Sometimes it helps if test authors leave their thoughts behind for us late-comers :-) 🦃
is something also needed for the call to memmove, which is in <string.h> and takes three arguments?
I don't think so.
The check function test code generation without an explicit header definition is relying on the c compiler implicit prototype definition (i.e., a function returning int and assuming the number and types of arguments are correct) and that the linker will resolve the function name. The success of the linker is effectively the test result. The implicit function definition is "good-enough" as the function is not actually invoked via the generated executable.
The original problem above, is that the msvc runtime libraries only export the "printf" symbol in the 64-bit amd library. Running the test on 32-bit windows or arm64 windows result in linker errors causing the tests to fail. If I remember correctly inline functions may have been generated/included (caveat emptor: could be disinformation) via the stdio header which resolves the symbol missing issue.
Once the header is included the function invocation needs to match the prototype. Without the header, there is no type checking (assume correct) nor are there any inline definitions.
memmove is likely either an intrinsic or exported explicitly in all runtime libraries leading to success.
Come to think of it, why are there two checks for "C library" functions?
Perhaps they are just simple/easy checks that don't require linking with libraries other than the default runtime?
Otherwise, there would be a need to explicitly include other libraries for the linker which likely might be challenging across all supported platforms. Although, I suppose a library could be generated, compiled, and linked with the configure test.
P.S.: I have real-world experience with the implicit function definition problem. The simulation tool with which we used extensive custom c library calls stopped producing c function prototypes and did not document the change between major versions. When testing the new version, our models were crashing for mysterious reasons. Digging through the generated c code, eventually it became obvious that (1) prototypes were no longer begin generated and (2) without generated prototypes the call stack was being interpreted incorrectly (e.g., int in an invocation where a float was expected in the function, etc). In our case, the number of arguments was always correct but the implicit conversion between int and float was allowed by both c and the simulation language. Ultimately, the solution was to generate the function prototypes at runtime by walking the simulation language's internal symbol table and comparing the generated temporary file to the active prototype file and swapping the files and failing with a "need to build again" message when the prototypes changed.
Fair enough. I'll try to remember this - I have a substantial revamp queued for SConf/Conftest for more accurate API (and user) docs, some typing, etc. (there's a fair bit inconsistent, some of which I myself caused in past efforts to untangle it, not completely understanding everything). Anyway - I should maybe add something about the use of a header and funcargs other than just a bare statement that they exist. Along the lines of (wording not considered): If you include a header which provides a prototype declaration for the function being checked, the compiler may perform a more rigorous type check to match the prototype. If this causes unexpected failures, use funcargs to supply arguments to match the prototype.
Activity
jcbrill commentedon Mar 8, 2023
Research details, notes, and comments.
MSVC Libraries with
printfor_printfexportedThe libraries searched below should be comprehensive when no compiler or linker options are specified.
AMD64architectures onlydumpbin /LINKERMEMBER <library>(non-default installation paths):Generated c source file for
conf.CheckFunc('printf')The generated c source file is:
VS2015+ with a non-
AMD64default target architecture results in linker error:error LNK2019: unresolved external symbol _printf referenced in function _mainDocumentation: 2.1. Building Simple C / C++ Programs
Here's the famous "Hello, World!" program in C:
And here's how to build it using SCons. Save the code above into hello.c, and enter the following into a file named SConstruct:
The example fails to build for VS2015 and later if the default target architecture is not
'AMD64'. For example, VS2019 on a 32-bit Windows host.Adding
#include <stdio.h>will build with the default target architecture on non-AMD64Windows hosts with VS2015 and later:The
#include <stdio.h>causes the MSVC c preprocessor to generate inline functions forprintfthat dispatch to functions that are exported from the architecture-specific libraries.jcbrill commentedon Nov 22, 2023
@mwichmann When you have some time, this one should not take too long to resolve one way or the other.
The easiest solutions are to pick a function other than printf or explicitly include the stdio header (which may not be as easy in the test suite).
With c/c++ headers using inline generation, renaming via the preprocessor, and the like, not explicitly including the associated header seems questionable. Fifteen years ago, probably not an issue.
In this case, printf is supported when the header is included but mostly because it is remapped to a function that is exported.
mwichmann commentedon Nov 22, 2023
Example/test/configure code should be complete according to current language standards, not just "ancient C". We've fixed a bunch of those over time, including your #4413 above. So including a required header is not just fine, it's the right thing to do. Is it that simple? I see no problem with picking a different function to use either, I know in past lives I've run into some interesting problems with GCC which remaps some standard library names to a different name so you don't see the expected symbol in a generated binary (in fact, that scenario seems to be present in your research above with the test code punting on gcc)
jcbrill commentedon Nov 22, 2023
Unfortunately, No. It took a quick test to remember the issues.
The root cause is has to do with the code generation for CheckFunc.
Inlining for msvc-like compilers that define
_MSC_VERis explicitly disabled via generation of a#pragma function()block.The MSVC
#pragma function()documentation:Case 1: the generated c source file without any modifications is (
r1 = conf.CheckFunc('printf')):A warning is produced but linking is successful since the function is available for a 64-bit amd64 build on 64-bit windows:
Case 2: the generated c source file including the stdio header is (
r1 = conf.CheckFunc('printf', header='#include <stdio.h>')):A compiler warning and error is produced due to the usage of the printf function:
Since a signature is not generated, including the header is not going to work unless the function being tested takes no arguments.
Resolution options:
Any thoughts?
mwichmann commentedon Nov 23, 2023
Always interesting when it's a test that fails. Test design is hard, and people get it wrong because they got lucky and it passed "by chance" (see the recent PCH discussion, where "order matters", but the existing test passed because the first choice happened to have the sources in an order that worked).
If there's an easy choice for a standard C library function that doesn't trigger these problems, then great - but I wouldn't spend an age looking. Calling the function in a way that modern compilers don't object to would help (if you include the header to get the proto, then match the proto). I've been in exactly that place before - code generated automatically from a symbol, so you don't know the calling convention, you just want to know the symbol links ok. Compilers hadn't gotten as fussy circa the middle of the 2000 decade, so I don't think we had to solve it.
jcbrill commentedon Nov 23, 2023
To be fair, I usually only run the MSVC/MSVS (~79 tests) tests on arm64. The configure test failed when running the entire suite (which takes forever on the low-end arm64 test box). I think I also tried in a 32-bit virtual machine and the test failed as well but I'm not positive.
It strikes me as odd that the MS runtime library exports aren't the same for all of the platform versions of the libraries.
I don't really understand why the intrinsics/inline methods are explicitly guarded against for MSVC. It appears that the configure test wants the function to be exported from the runtime or fail otherwise.
I'm not exactly sure of the utility of the configure behavior as the "correct" usage of
printfvia including the header and an appropriate signature will work on any of the platforms. The fact that it is not exported from the runtime or necessarily implemented as the literal symbolprintfseems less important than actually being able to use theprintfsymbol successfully.jcbrill commentedon Nov 23, 2023
@mwichmann Possible working solution.
The two
CheckFuncmethods (SCons/SConf.pyandSCons/Conftest.py) accept a header definition.From the
CheckFuncdocumentation inSCons/Conftest.py:Passing a header to provide a prototype for functions that take arguments can be an issue with "modern" compilers by default as the generated function invocation has an empty argument list.
A working solution is to allow a function arguments string to be defined for the generated function invocation.
CheckFuncinSCons/Conftest.py:CheckFuncinSCons/SConf.py:The
printfcheck function call intest/Configure/config-h.pybecomes:The annotated generated c source file is:
The
config-h.pytest with the updatedprintfcheck function appears to pass on the following configurations:If deemed worthy, a PR is easy enough.
Happy Thanksgiving!
Joe
mwichmann commentedon Nov 23, 2023
Sounds good to me... is something also needed for the call to
memmove, which is in<string.h>and takes three arguments? Come to think of it, why are there two checks for "C library" functions? Sometimes it helps if test authors leave their thoughts behind for us late-comers :-) 🦃jcbrill commentedon Nov 23, 2023
I don't think so.
The check function test code generation without an explicit header definition is relying on the c compiler implicit prototype definition (i.e., a function returning int and assuming the number and types of arguments are correct) and that the linker will resolve the function name. The success of the linker is effectively the test result. The implicit function definition is "good-enough" as the function is not actually invoked via the generated executable.
The original problem above, is that the msvc runtime libraries only export the "printf" symbol in the 64-bit amd library. Running the test on 32-bit windows or arm64 windows result in linker errors causing the tests to fail. If I remember correctly inline functions may have been generated/included (caveat emptor: could be disinformation) via the stdio header which resolves the symbol missing issue.
Once the header is included the function invocation needs to match the prototype. Without the header, there is no type checking (assume correct) nor are there any inline definitions.
memmove is likely either an intrinsic or exported explicitly in all runtime libraries leading to success.
Perhaps they are just simple/easy checks that don't require linking with libraries other than the default runtime?
Otherwise, there would be a need to explicitly include other libraries for the linker which likely might be challenging across all supported platforms. Although, I suppose a library could be generated, compiled, and linked with the configure test.
P.S.: I have real-world experience with the implicit function definition problem. The simulation tool with which we used extensive custom c library calls stopped producing c function prototypes and did not document the change between major versions. When testing the new version, our models were crashing for mysterious reasons. Digging through the generated c code, eventually it became obvious that (1) prototypes were no longer begin generated and (2) without generated prototypes the call stack was being interpreted incorrectly (e.g., int in an invocation where a float was expected in the function, etc). In our case, the number of arguments was always correct but the implicit conversion between int and float was allowed by both c and the simulation language. Ultimately, the solution was to generate the function prototypes at runtime by walking the simulation language's internal symbol table and comparing the generated temporary file to the active prototype file and swapping the files and failing with a "need to build again" message when the prototypes changed.
mwichmann commentedon Nov 23, 2023
Fair enough. I'll try to remember this - I have a substantial revamp queued for SConf/Conftest for more accurate API (and user) docs, some typing, etc. (there's a fair bit inconsistent, some of which I myself caused in past efforts to untangle it, not completely understanding everything). Anyway - I should maybe add something about the use of a header and funcargs other than just a bare statement that they exist. Along the lines of (wording not considered): If you include a header which provides a prototype declaration for the function being checked, the compiler may perform a more rigorous type check to match the prototype. If this causes unexpected failures, use
funcargsto supply arguments to match the prototype.