-2

while building Lua on windows by mingw491 it gives an undefined reference error. Following is the output:

C:\Users\Alizadeh\Desktop\lua-5.3.0>mingw32-make PLAT=mingw
cd src && mingw32-make mingw
mingw32-make[1]: Entering directory 'C:/Users/Alizadeh/Desktop/lua-5.3.0/src'
mingw32-make "LUA_A=lua53.dll" "LUA_T=lua.exe" \
"AR=gcc -std=gnu99 -shared -o" "RANLIB=strip --strip-unneeded" \
"SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe
mingw32-make[2]: Entering directory 'C:/Users/Alizadeh/Desktop/lua-5.3.0/src'
g++    -c -o lua.o lua.c
g++    -c -o lapi.o lapi.c
g++    -c -o lcode.o lcode.c
.
.
.
g++    -c -o loadlib.o loadlib.c
g++    -c -o linit.o linit.c
gcc -std=gnu99 -shared -o lua53.dll lapi.o lcode.o lctype.o ldebug.o ldo.o ldump
.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o
 ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o lbitlib.o lcorolib.o
 ldblib.o liolib.o lmathlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o loadlib.o
linit.o
ldo.o:ldo.c:(.text+0xe5): undefined reference to `__cxa_allocate_exception'
ldo.o:ldo.c:(.text+0x105): undefined reference to `__cxa_throw'
ldo.o:ldo.c:(.text+0x234): undefined reference to `__cxa_begin_catch'
ldo.o:ldo.c:(.text+0x24c): undefined reference to `__cxa_end_catch'
ldo.o:ldo.c:(.rdata$_ZTIP11lua_longjmp[__ZTIP11lua_longjmp]+0xffff3fb8): undefin
ed reference to `vtable for __cxxabiv1::__pointer_type_info'
C:/Qt/Qt5.4.0/Tools/mingw491_32/bin/../lib/gcc/i686-w64-mingw32/4.9.1/../../../.
./i686-w64-mingw32/bin/ld.exe: ldo.o: bad reloc address 0x0 in section `.rdata$_
ZTIP11lua_longjmp[__ZTIP11lua_longjmp]'
collect2.exe: error: ld returned 1 exit status
makefile:59: recipe for target 'lua53.dll' failed
mingw32-make[2]: *** [lua53.dll] Error 1
mingw32-make[2]: Leaving directory 'C:/Users/Alizadeh/Desktop/lua-5.3.0/src'
makefile:116: recipe for target 'mingw' failed
mingw32-make[1]: *** [mingw] Error 2
mingw32-make[1]: Leaving directory 'C:/Users/Alizadeh/Desktop/lua-5.3.0/src'
makefile:55: recipe for target 'mingw' failed
mingw32-make: *** [mingw] Error 2

I'm using mingw (491) on windows7 and I want to link it with Qt.

Mosi
  • 1,178
  • 2
  • 12
  • 30

2 Answers2

2

I found the answer. I've just used g++ instead of gcc and it fixed :)

Mosi
  • 1,178
  • 2
  • 12
  • 30
  • 1
    I ran into this exact same problem with the same build platform and switching to g++ worked! Thank you! – silentorb Sep 02 '15 at 01:51
  • Using g++ instead of gcc will work for building lua, but it will generates C++ symbols which will lead to issues when using the generated DLL. – Benjamin T May 31 '16 at 06:07
1

The solution is to add the following line in src/Makefile:

CXX= gcc -std=gnu99

Or more generally to set CXX to whichever value CC is set.

This problem occurs because the Lua make files rely on the implicit rules and variables of GNU make (source). Which means that make should call $(CC) -c $(CFLAGS) $(CPPFLAGS) to build .o files from .c files.

However mingw make calls $(CXX) -c $(CFLAGS) $(CPPFLAGS). This means that Lua C code is compiled as C++. The issue reveals itself at linktime as here gcc is used and it fails to find C++ related dependencies. So from here you have 2 solutions:

  1. Replace gcc by g++ at link time (Mosi's solution). Which you should NOT do! As you will end up with C++ symbols with mangled names and you will not be able to use your DLL in anything else than C++ projects. Although your code will not be portable as any other Lua DLL will not have C++ symbols.
  2. Replace g++ by gcc during compilation (see above). You will end up with valid C symbols in your DLL.
Benjamin T
  • 8,120
  • 20
  • 37
  • how do I know or double check this? `Or more generally to set CXX to whichever value CC is set.` – Ismoh Nov 21 '21 at 00:24
  • 1
    @lsmoh You can print the value of `CC`. See https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile Or do `CXX = $(CC) ` – Benjamin T Nov 22 '21 at 01:20
  • Thanks for the reply and explanation! It was really specific to a game I was modding, solution is here, but not relating to this topic. https://stackoverflow.com/questions/70048918/lua-5-1-package-loadlib-and-require-gcc-building-windows-dll – Ismoh Nov 23 '21 at 03:00