0

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 : 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

2 errors remain : Remaining error

Xemuth
  • 415
  • 3
  • 12

2 Answers2

1

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)

  • Hello, thanks for your response. I will give it a try, I will be right back. – Xemuth Dec 29 '20 at 11:02
  • Let me know ;) I managed to compile flatcc with msvc and linked it with mingw32 on windows, so I guess there's not reason why you couldn't use it with clang – Victor Gallet Dec 29 '20 at 11:10
  • My cmake command is : ```cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=clang -DFLATCC_PORTABLE=true -DFLATCC_ALLOW_WERROR=false -DBUILD_TESTING=false -DFLATCC_CXX_TEST=false -DFLATCC_REFLECTION=false -B .``` I have changed -std=c11 to -std=c99 in flags.make, then I run Mingw32-make.exe on the generated cmake. It compile well but my file are still .a, where I have to use ```"-o flatcc.lib``` ? In which file I can change this ? – Xemuth Dec 29 '20 at 12:34
  • Well to acquiere a .lib I just changed links.txt : ```...bin\clang\bin\ar.exe rc ..\..\..\lib\flatcc.lib ...``` – Xemuth Dec 29 '20 at 13:56
  • 1
    Actually because you were using clang on windows, even if the file is .a it's still a valid windows static lib. So if you want a .lib, you can just rename it. yes, changing the links.txt file which was generated by the cmake command will give you a .lib file anyway :) – Victor Gallet Dec 29 '20 at 14:06
  • So now, if you compile your program and links it against this flatcc.lib, and include the headers (in include/flatcc/) to get the declarations, you'll be able to use it. – Victor Gallet Dec 29 '20 at 14:08
  • Victor Gallet, done I have edited the first post. Compiling it with clang fixed one of 3 errors. It's better than nothing – Xemuth Dec 29 '20 at 14:08
0

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

Xemuth
  • 415
  • 3
  • 12