Reputation: 349
Resolved
I'm currently working on LabWindows/CVI, this is a C. The only version of C supported is c99. I'm trying to integrate Google Flatbuffers (the c version flatcc) to my current project. When I'm trying to link my solution, I'm facing a linking error :
First question : how could I fix this error ?
According to the vendor, LabWindows/CVI use CLANG as compiler. if I take a look at the file where the symbol aligned_free /algined_malloc apear, I can read this :
/*
* 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.
*/
Second question : According to the text above, I should have definition of aligned_free/aligned_malloc, but for some reason, I don't have it. Why ? What I'm missing ?
Additionnal information : LabWindows/CVI only accepte .lib as lib (no .a) so I had to compile flatcc using Cmake and MSVS19. I have try several configuration but nothing to do I always get the same error.
Best regard
EDIT : I fixed one undefined symbol '__allmul' by doing this cmake command :
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 .
Then editing flatcc\src\compiler\CMakeFiles\flatcc.dir\flags.make
and flatcc\src\runtime\CMakeFiles\flatcc.dir\flags.make
to looks like that : C_FLAGS = -m32 -DFLATCC_REFLECTION=0 -Wstrict-prototypes -Wsign-conversion -Wconversion -std=c99 -pedantic -Wall -Wextra -DFLATCC_PORTABLE
-m32 is for 32 bit binary and -std=c99 instead of -std=c11 (can't put c89 because flatcc contain some inline keyword)
Then editing flatcc\src\compiler\CMakeFiles\flatcc.dir\link.txt
and flatcc\src\runtime\CMakeFiles\flatcc.dir\link.txt
to look like this :
...\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
Then running Mingw32-make.exe
Upvotes: 0
Views: 468
Reputation: 349
Well, to fix this issue, I had to compile flatcc Using clang in 32bit. To do this, I moved into flatcc directory and did :
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 .
Then edited flatcc\src\compiler\CMakeFiles\flatcc.dir\flags.make
and flatcc\src\runtime\CMakeFiles\flatcc.dir\flags.make
to add -m32 to 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
Then changed flatcc\src\compiler\CMakeFiles\flatcc.dir\link.txt
and flatcc\src\runtime\CMakeFiles\flatcc.dir\link.txt
to look like this :
...\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
Note: I just changed name of .a generated to .lib
Then finally I run mingw32-make.exe
(or make.exe
)
Thanks to Victor Gallet
Upvotes: 0
Reputation: 11
Could you try to compile flatcc as a static library and force the output as .lib instead of the (by default) .a (with the "-o flatcc.lib" flag)? The problem I guess is that you're using two different compilers here (msvc and clang), on the same plateform (win32), and clang by default could have a "unix way of doing things". ;)
But actually the important point here I guess is from this comment quote:
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.
Maybe simply try to compile flatcc with clang and with the "-std=c89" flag to avoid any problem. Do:
cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=clang .
Then in the flatcc\src\compiler\CMakeFiles\flatcc.dir\flags.make file generated, you can change -std=c11 to -std=c89. Then compile the project again with:
make
By default when I compile flatcc with mingw-32 on windows, I have the -std=c11 flag set (gcc version 7.3)
Upvotes: 1