visual-c++linkermalloccl

what must I link to avoid linker error on _aligned_alloc (MSVC command-line)?


I'm trying to build a simple DLL on Windows that wraps _aligned_alloc, _aligned_realloc, and _aligned_free, using the cl command-line tools. My source file is a .c file, includes <stdlib.h> and <malloc.h>, and seems to compile OK with:

cl /LD CustomAllocators.c /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib

but then fails to link, showing:

CustomAllocators.obj : error LNK2019: unresolved external symbol _aligned_alloc referenced in function Allocate
CustomAllocators.dll : fatal error LNK1120: 1 unresolved externals

All those /NODEFAULTLIB switches are the result of much googling, and seem like they ought to do it, unless these allocation functions just aren't in any of those standard libraries... but in that case, I have no idea where they might be.

Can anyone tell me what library I need to include to resolve these symbols, or if there's something else I might be doing wrong?


Solution

  • According to [MS.Learn]: <cstdlib> - Remarks (emphasis is mine):

    These functions have the semantics specified in the C standard library. MSVC doesn't support the aligned_alloc function.

    You might want to switch to [MS.Learn]: _aligned_malloc.

    dll00.c:

    #include <stdio.h>
    #include <stdlib.h>
    
    #if defined(_WIN32)
    #  define DLL00_EXPORT_API __declspec(dllexport)
    #else
    #  define DLL00_EXPORT_API
    #endif
    
    
    #if defined(__cplusplus)
    extern "C" {
    #endif
    
    DLL00_EXPORT_API int dll00Func00();
    
    #if defined(__cplusplus)
    }
    #endif
    
    
    int dll00Func00()
    {
        void *p = _aligned_malloc(2048, 1024);
        printf("Aligned pointer: %p\n", p);
        _aligned_free(p);
        return 0;
    }
    

    Output (build - check [MS.Docs]: Use the Microsoft C++ toolset from the command line):

    [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q067809018]> sopr.bat
    ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
    
    [prompt]> "c:\Install\pc032\Microsoft\VisualStudioCommunity\2019\VC\Auxiliary\Build\vcvarsall.bat" x64
    **********************************************************************
    ** Visual Studio 2019 Developer Command Prompt v16.10.0
    ** Copyright (c) 2021 Microsoft Corporation
    **********************************************************************
    [vcvarsall.bat] Environment initialized for: 'x64'
    
    [prompt]> dir /b
    dll00.c
    
    [prompt]>
    [prompt]> cl /nologo /MD /DDLL dll00.c  /link /NOLOGO /DLL /OUT:dll00.dll
    dll00.c
       Creating library dll00.lib and object dll00.exp
    
    [prompt]> dir /b
    dll00.c
    dll00.dll
    dll00.exp
    dll00.lib
    dll00.obj
    
    [prompt]>
    

    Test the .dll:

    [prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe"
    Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>> import ctypes as cts
    >>>
    >>> dll = cts.CDLL("./dll00.dll")
    >>> # This is for display purpose only. Skipping crucial steps. Don't do this in production!!!
    >>> dll.dll00Func00()
    Aligned pointer: 0000025E33A9A000
    0
    

    As mentioned in comments, some aspects were skipped (to keep the code simple). Check [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer).