简体   繁体   English

在Windows上使用Mingw的gcc构建共享库时出现“未定义的引用”问题

[英]“Undefined reference to” issue face while build shared library on Windows using gcc of Mingw

I am trying to build a shared library (.dll) for windows using Mingw. 我正在尝试使用Mingw为Windows构建共享库(.dll)。 As a dependencies, the shared library has a set of c files, header files and couple of static libraries (*.a built with mingw). 作为依赖项,共享库具有一组c文件,头文件和几个静态库(* .a用mingw构建)。 When I tried to build the shared library with source files and static libraries, the following error occurs: "undefined reference to". 当我尝试使用源文件和静态库构建共享库时,发生以下错误:“未定义引用”。 The error comes from one of the static library which has dependency on other static files. 错误来自静态库之一,该库依赖于其他静态文件。

The following example demonstrates the issue I am facing 以下示例演示了我面临的问题

There is a file ops.c, built as a static library 有一个文件ops.c,它是作为静态库构建的

#include<stdio.h>

extern void print_num();

void print_num()
{
    printf("HELLO WORLD IN OPS\n");
}

Compiled with the command: 编译命令:

gcc -c ops.c -o ops.o
ar cru libops.a ops.o

The second files mp4.c, which is also built as static library 第二个文件mp4.c,也作为静态库构建

#include<stdio.h>

void print_ops_num()
{
    printf("IN mp4c, will call ops\n");

    print_num();
}

Compiled with the command: 编译命令:

gcc -c mp4.c -o mp4.o
ar cru libmp4.a mp4.o

The list files mss.c, this will be built as shared library with above 2 static libraries 列表文件mss.c,它将与上面的2个静态库一起构建为共享库

#include<stdio.h>

void demo_func()
{
     printf("Demo dynamic build and linking \n");

     print_ops_num();

     print_num();

}

Commands: 命令:

 gcc -o libmss.dll mss.c -o mss.o -L libops.a -L libmp4.a -Wl,--out-implib=libmss.dll.a

and also used below 也用在下面

 gcc -o libmss.dll mss.c -o mss.o -L -lops -L -lmp4

The following error is seen while build the shared library 构建共享库时看到以下错误

C:\Users\AppData\Local\Temp\cc6tISrV.o:mss.c:(.text+0x15): undefined reference to `print_ops_num'
C:\Users\AppData\Local\Temp\cc6tISrV.o:mss.c:(.text+0x1a): undefined reference to `print_num'
g:/work/temp/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\AppData\Local\Temp\cc6tISrV.o: ba
eloc address 0x0 in section `.pdata'
collect2.exe: error: ld returned 1 exit statusc  ops.a  ops.c  ops.o

The above problem does not occur when the same procedure is folowed on a linux platform. 当在Linux平台上执行相同的过程时,不会发生上述问题。 I am new Windows platform, can someone help me to solve the above issue? 我是新的Windows平台,有人可以帮助我解决以上问题吗?

The line 线

gcc -o libmss.dll mss.c -o mss.o -L libops.a -L libmp4.a -Wl,--out-implib=libmss.dll.a

is incorrect. 是不正确的。 You are using the -L flags, which point the linker to directories where to look for libraries. 您正在使用-L标志,该标志将链接程序指向在其中查找库的目录 These directories do not exist, obviously, so nothing happens and your DLL is not linked to the static libraries. 这些目录不存在,显然,因此没有任何反应,并且您的DLL没有链接到静态库。 You need to use the -l flags: 您需要使用-l标志:

gcc -shared -o libmss.dll mss.c -o mss.o -L . -l ops -l mp4 -Wl,--out-implib=libmss.dll.a

The reason why this worked in Linux and not in Windows is that DLL needs to have all external dependencies resolved during linking, while in Linux SO can have undefined symbols, which are resolved only when the library is used. 之所以在Linux中而不是在Windows中起作用,是因为DLL需要在链接期间解决所有外部依赖关系,而在Linux中SO可以具有未定义的符号,只有在使用库时才可以解析。

By the way, with modern MinGW, you don't need to create import libraries ( libmss.dll.a ). 顺便说一下,使用现代MinGW,您无需创建导入库( libmss.dll.a )。 Is is possible to link directly to the DLL. 可以直接链接到DLL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Linux上使用gcc和在Windows上使用MinGW构建共享库 - Building a shared library using gcc on Linux and MinGW on Windows 使用Mingw创建OpenCV 3.1的共享构建时未定义的对cv :: redirectError的引用 - Undefined reference to cv::redirectError while creating shared build of OpenCV 3.1 with Mingw 使用 mingw 对 imp 的未定义引用 - Undefined Reference to imp using mingw 仅使用MinGW GCC的未定义引用 - Undefined references only using MinGW GCC eclipse + cdt + mingw + Windows错误“在构建时对“ WinMain @ 16”的未定义引用” - eclipse + cdt + mingw + windows error “undefined reference to `WinMain@16'” on build 使用来自 mingw 的 gcc,SDL:“对‘SDL_Init’的未定义引用”错误消息是什么意思,我该如何解决? - Using gcc from mingw, SDL: "undefined reference to `SDL_Init'" What does the error message mean and how do I resolve it? windows下使用mingw生成动态库时出现未定义符号错误,但linux没有 - Undefined symbols error when using mingw to generate dynamic library under windows,but linux does not 使用DllMain函数(MinGW + CMake)创建Windows共享库 - Create a Windows shared library with DllMain function (MinGW + CMake) 尝试使用mingw构建和编译lua 5.3.3,未定义参考错误 - trying to build and compile lua 5.3.3 with mingw , undefined reference error 使用Mingw在Windows上安装Linux库 - Installing Linux Library on Windows Using Mingw
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM