[英]flatcc undefined symbol aligned_free/aligned_malloc
解决
我目前正在研究 LabWindows/CVI,这是一个 C。 唯一支持的 C 版本是 c99。 我正在尝试将 Google Flatbuffers(c 版本flatcc )集成到我当前的项目中。 当我尝试链接我的解决方案时,我遇到了链接错误:
第一个问题:我该如何解决这个错误?
根据供应商的说法,LabWindows/CVI 使用CLANG 作为编译器。 如果我看一下符号aligned_free /algined_malloc 出现的文件,我可以读到:
/*
* NOTE: MSVC in general has no aligned alloc function that is
* compatible with free and it is not trivial to implement a version
* which is. Therefore, to remain portable, end user code needs to
* use `aligned_free` which is not part of C11 but defined in this header.
*
* glibc only provides aligned_alloc when _ISOC11_SOURCE is defined, but
* MingW does not support aligned_alloc despite of this, it uses the
* the _aligned_malloc as MSVC.
*
* The same issue is present on some Unix systems not providing
* posix_memalign.
*
* Note that clang and gcc with -std=c11 or -std=c99 will not define
* _POSIX_C_SOURCE and thus posix_memalign cannot be detected but
* aligned_alloc is not necessarily available either. We assume
* that clang always has posix_memalign although it is not strictly
* correct. For gcc, use -std=gnu99 or -std=gnu11 or don't use -std in
* order to enable posix_memalign, or live with the fallback until using
* a system where glibc has a version that supports aligned_alloc.
*
* For C11 compliant compilers and compilers with posix_memalign,
* it is valid to use free instead of aligned_free with the above
* caveats.
*/
第二个问题:根据上面的文字,我应该有aligned_free/aligned_malloc的定义,但由于某种原因,我没有它。 为什么? 我错过了什么?
附加信息: LabWindows/CVI 只接受.lib 作为 lib (no.a),所以我必须使用 Cmake 和 MSVS19 编译 flatcc。 我尝试了几种配置,但无事可做,我总是遇到同样的错误。
最良好的问候
编辑:我通过执行此 cmake 命令修复了一个未定义的符号“__allmul”:
cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=x86_64-w64-mingw32-c99 -DFLATCC_PORTABLE=true -DFLATCC_ALLOW_WERROR=false -DBUILD_TESTING=false -DFLATCC_CXX_TEST=false -DFLATCC_REFLECTION=false -B.
然后编辑flatcc\src\compiler\CMakeFiles\flatcc.dir\flags.make
和flatcc\src\runtime\CMakeFiles\flatcc.dir\flags.make
看起来像这样: C_FLAGS = -m32 -DFLATCC_REFLECTION=0 -Wstrict-prototypes -Wsign-conversion -Wconversion -std=c99 -pedantic -Wall -Wextra -DFLATCC_PORTABLE
-m32 用于 32 位二进制和 -std=c99 而不是 - std=c11(不能放 c89,因为 flatcc 包含一些内联关键字)
然后编辑flatcc\src\compiler\CMakeFiles\flatcc.dir\link.txt
和flatcc\src\runtime\CMakeFiles\flatcc.dir\link.txt
看起来像这样:
...\bin\clang\bin\ar.exe rc ..\..\..\lib\flatcc.lib CMakeFiles/flatcc.dir/__/__/external/hash/cmetrohash64.c.obj ...
...bin\clang\bin\ranlib.exe ..\..\..\lib\flatcc.lib
然后运行Mingw32-make.exe
您能否尝试将 flatcc 编译为 static 库并强制 output as.lib 而不是(默认情况下).a(带有“-o flatcc.lib”标志)? 我猜的问题是你在这里使用两个不同的编译器(msvc和clang),在同一个平台(win32)上,默认情况下clang可能有一个“unix做事方式”。 ;)
但实际上我猜这里的重点是来自这个评论引用:
请注意,带有 -std=c11 或 -std=c99 的 clang 和 gcc 不会定义 * _POSIX_C_SOURCE ,因此无法检测到 posix_memalign 但 *aligned_alloc 也不一定可用。 我们假设 * clang 总是有 posix_memalign 虽然它不是严格 * 正确的。
也许只是尝试使用 clang 和“-std=c89”标志编译 flatcc 以避免任何问题。 做:
cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=clang .
然后在生成的flatcc\src\compiler\CMakeFiles\flatcc.dir\flags.make文件中,可以将-std=c11改为-std=c89。 然后再次编译项目:
make
默认情况下,当我在 windows 上使用 mingw-32 编译 flatcc 时,我设置了 -std=c11 标志(gcc 版本 7.3)
好吧,为了解决这个问题,我必须在 32 位中使用 clang 编译 flatcc。 为此,我进入了 flatcc 目录并做了:
cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=x86_64-w64-mingw32-c99 -DFLATCC_PORTABLE=true -DFLATCC_ALLOW_WERROR=false -DBUILD_TESTING=false -DFLATCC_CXX_TEST=false -DFLATCC_REFLECTION=false -DFLATCC_USE_GENERIC_ALIGNED_ALLOC -B .
然后编辑flatcc\src\compiler\CMakeFiles\flatcc.dir\flags.make
和flatcc\src\runtime\CMakeFiles\flatcc.dir\flags.make
将 -m32 添加到 C_FLAGS:
C_FLAGS = -m32 -DFLATCC_REFLECTION=0 -Wstrict-prototypes -Wsign-conversion -Wconversion -std=c11 -pedantic -Wall -Wextra -DFLATCC_PORTABLE -DFLATCC_USE_GENERIC_ALIGNED_ALLOC
然后将flatcc\src\compiler\CMakeFiles\flatcc.dir\link.txt
和flatcc\src\runtime\CMakeFiles\flatcc.dir\link.txt
为如下所示:
...\bin\clang\bin\ar.exe rc ..\..\..\lib\flatcc.lib CMakeFiles/flatcc.dir/__/__/external/hash/cmetrohash64.c.obj ...
...bin\clang\bin\ranlib.exe ..\..\..\lib\flatcc.lib
注意:我只是将生成的.a的名称更改为.lib
然后最后我运行mingw32-make.exe
(或make.exe
)
感谢维克多·加勒
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.