mpack
mpack copied to clipboard
Help compiling mpack with mingw32
Good day all, I have a rather compilcated project which compiles a static C library, and then a shared C++ library and links them. This project is also cross platform, so I am compiling with gcc/g++, but now I'm adding compilation with mingw32.
I've been using this mpack project for a while now with GNU compiler and it works great! But now that I'm trying to cross compile for Windows with mingw32 I'm running into problems.
I created a minimum example which shows the problem.
C file ( compiles to static library) :
#include "mpack.h"
void func() {
mpack_write_int( NULL, 0 );
}
C++ file ( compiles to shared library ):
#include "mpack.h"
void func2() {
mpack_write_int( NULL, 0 );
}
Compilation script:
#!/bin/bash
compile() {
# Compile C file to object file
echo "Compiling C file..."
${PREFIX}gcc -c "$C_SOURCE" -o "${C_SOURCE%.c}.o"
${PREFIX}gcc -c mpack.c -o "mpack.o"
# Create static library
echo "Creating static library..."
${PREFIX}ar -rcs "lib${LIB_NAME}.a" "${C_SOURCE%.c}.o" "mpack.o"
# Compile C++ file to shared library
echo "Creating shared library..."
${PREFIX}g++ -shared -o "lib${LIB_NAME}.so" \
"${CPP_SOURCE}" \
"-L." \
"-l${LIB_NAME}" \
-fPIC
echo "Build complete!"
}
echo "------------------------------------------------------------------------"
echo "Compiling GCC"
echo "------------------------------------------------------------------------"
C_SOURCE=c_file.c
CPP_SOURCE=cpp_file.cpp
LIB_NAME=out.so
PREFIX=
compile
echo "------------------------------------------------------------------------"
echo "Compiling MinGW"
echo "------------------------------------------------------------------------"
C_SOURCE=c_file.c
CPP_SOURCE=cpp_file.cpp
LIB_NAME=out.dll
PREFIX=x86_64-w64-mingw32-
compile
I get output:
------------------------------------------------------------------------
Compiling GCC
------------------------------------------------------------------------
Compiling C file...
Creating static library...
Creating shared library...
Build complete!
------------------------------------------------------------------------
Compiling MinGW
------------------------------------------------------------------------
Compiling C file...
Creating static library...
Creating shared library...
/usr/bin/x86_64-w64-mingw32-ld: ./libout.dll.a(mpack.o):mpack.c:(.text+0xbaa): multiple definition of `mpack_write_int'; /tmp/cciDd3uY.o:cpp_file.cpp:(.text$mpack_write_int[mpack_write_int]+0x0): first defined here
collect2: error: ld returned 1 exit status
Build complete!
So the same compilation that works for GCC does not work for MinGW. The problem(s) seem to be with these inline functions in mpack.h:
/** Writes an integer in the most efficient packing available. */
MPACK_INLINE void mpack_write_int(mpack_writer_t* writer, int64_t value) {
mpack_write_i64(writer, value);
}
Any use of any of these header inlined functions will cause linking issues on MinGW.
Does anyone know how I can compile this without changing the mpack.h/.c ?
x86_64-w64-mingw32-gcc --version
x86_64-w64-mingw32-gcc (GCC) 13-win32
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.