1. 程式人生 > >cmake:讓mingw(gcc)生成MSVC可用的dll(.lib)

cmake:讓mingw(gcc)生成MSVC可用的dll(.lib)

開發十年,就只剩下這套架構體系了! >>>   

mingw(gcc)編譯出來的動態庫(DLL)預設是沒有MSVC連線動態庫所需要的lib檔案(import library)的。

關於MSVC的DLL和LIB的作用和區別,請參考這篇博文,講得很透徹了–>《DLL和LIB的區別》

那麼如果MSVC要連結gcc生成的DLL,卻沒有lib檔案(import library)怎麼辦? 
對於這個問題網上都有解決的辦法,這篇文章講到一些辦法,可供收藏《VC6 呼叫GCC的DLL》,對於沒有提供lib的DLL都可以用這篇文章提供的辦法試試。

如果專案中不同的模組用不同的編譯器編譯,這時如果gcc編譯的DLL沒有import library(lib檔案),可以通過cmake設定選項讓gcc在編譯的的時候生成import library(lib檔案)。

CMAKE有一個GNUtoMS引數就是解決這個問題的。

GNUtoMS 
Convert GNU import library (.dll.a) to MS format (.lib).

When linking a shared library or executable that exports symbols using GNU tools on Windows (MinGW/MSYS) with Visual Studio installed convert the import library (.dll.a) from GNU to MS format (.lib). Both import libraries will be installed by install(TARGETS) and exported by install(EXPORT) and export() to be linked by applications with either GNU- or MS-compatible tools.

CMAKE_GNUtoMS 
Convert GNU import libraries (.dll.a) to MS format (.lib).

This variable is used to initialize the GNUtoMS property on targets when they are created. See that target property for additional information.(這個變數用來初始化GNUtoMS屬性)

所以,在用cmake生成Makefile時,設定GNUtoMS就可以解決這個問題。有兩種途徑:

shell命令列方式
如下在命令列中-D引數定義一個為BOOL型別的CMAKE_GNUtoMS引數為ON,就指示在編譯時對dll生成import library(.lib)

cmake %source_folder% -G “Eclipse CDT4 - MinGW Makefiles” -DCMAKE_GNUtoMS:BOOL=ON

cmak-gui
如下在cmake-gui介面中將CMAKE_GNUtoMS選項勾選,再點<generate>按鈕生成Makefile

然後執行make編譯專案的過程中,生成dll時會輸出下面的資訊(前提是你安裝了VC編譯器)表明它在呼叫VC的lib(dll工具)生成import library(.lib)

Setting environment for using Microsoft Visual Studio 2010 x64 tools.

Microsoft (R) Library Manager Version 10.00.30319.01

Copyright (C) Microsoft Corporation. All rights reserved.

正在建立庫 libturbojpeg.lib 和物件 libturbojpeg.exp

編譯完成後,你就會發現所有的dll都有對應的lib檔案了。
--------------------- 
作者:10km 
來源:CSDN 
原文:https://blog.csdn.net/10km/article/details/50525604 
版權宣告:本文為博主原創文章,轉載請