我正在尝试使用MSYS2编译一个相当简单的库,但是即使函数包含在我正在编译的文件中,也会出现一些奇怪的未定义参考错误。
Makefile的相关部分:

luars232.dll: librs232_windows.o librs232.o luars232.o
    $(CC) $(LDFLAGS) -o luars232.dll luars232.o librs232.o librs232_windows.o -L./lua-5.1.5/src/ -llua

luars232.o: librs232/bindings/lua/luars232.c
    $(CC) $(CFLAGS) librs232/bindings/lua/luars232.c -o luars232.o

librs232.o: librs232/src/rs232.c
    $(CC) $(CFLAGS) librs232/src/rs232.c -o librs232.o

librs232_windows.o: librs232/src/rs232_windows.c
    $(CC) $(CFLAGS) librs232/src/rs232_windows.c -o librs232_windows.o


编译输出的前几行:

$ make windows
cc -c -fPIC -I librs232/include/ -I/usr/include/lua5.1/ -I/usr/include/json -I./lua-5.1.5/src/ -DWIN32 librs232/src/rs232_windows.c -o librs232_windows.o
cc -c -fPIC -I librs232/include/ -I/usr/include/lua5.1/ -I/usr/include/json -I./lua-5.1.5/src/ -DWIN32 librs232/src/rs232.c -o librs232.o
cc -c -fPIC -I librs232/include/ -I/usr/include/lua5.1/ -I/usr/include/json -I./lua-5.1.5/src/ -DWIN32 librs232/bindings/lua/luars232.c -o luars232.o
cc -shared -DWIN32 -o luars232.dll luars232.o librs232.o librs232_windows.o -L./lua-5.1.5/src/ -llua
C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.2.0/../../../../i686-w64-mingw32/bin/ld.exe: luars232.o:luars232.c:(.text+0x46): undefined reference to `_imp__rs232_init'
C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.2.0/../../../../i686-w64-mingw32/bin/ld.exe: luars232.o:luars232.c:(.text+0x8b): undefined reference to `_imp__rs232_set_device'
C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.2.0/../../../../i686-w64-mingw32/bin/ld.exe: luars232.o:luars232.c:(.text+0x98): undefined reference to `_imp__rs232_open'
C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.2.0/../../../../i686-w64-mingw32/bin/ld.exe: luars232.o:luars232.c:(.text+0xae): undefined reference to `_imp__rs232_close'


rs232_windows.c的摘录显示出它正在寻找的功能实际上是在源代码中定义的:

RS232_LIB struct rs232_port_t *
rs232_init(void)
{
    struct rs232_port_t *p = NULL;
    struct rs232_windows_t *wx = NULL;
    p = (struct rs232_port_t *) malloc(sizeof(struct rs232_port_t));


有谁知道为什么编译器在源代码中找不到这些功能?

最佳答案

好了,在经过数小时的反复试验之后,此问题是由以下原因引起的:源文件假设MSVC作为编译器(而不是MSYS2使用的gcc),并且在函数定义中添加了__declspec(dllexport)和__declspec(dllimport)修饰符。

08-20 01:42