链接错误:使用CMake在Windows上为MinGW静态构建C ++ Poco

时间:2017-07-06 22:51:08

标签: c++ windows cmake mingw poco-libraries

我想在Windows上静态构建最新的Poco C ++库完整版(仅限Poco :: Foundation和Poco :: Zip,没有样本和测试),用于MinGW(由QT Creator提供)。

首先,我从主页https://pocoproject.org/download/index.html下载并解压缩Windows版本库 它现在是版本 poco-1.7.8p3-all

现在我必须更改不同配置的MC设置而不是VS,所以我必须更改文件 $(POCO_BASE)/cmake/PocoMacros.cmake 在IF之后

if ("${CMAKE_GENERATOR}" MATCHES "Visual Studio") ... endif ()

添加行:

 get_filename_component(sdk_dir "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows;CurrentInstallFolder]" REALPATH)
 set(sdk_bindir "${sdk_dir}/bin")

我使用CMake命令:

cmake -G "MinGW Makefiles" -DENABLE_CRYPTO=OFF -DENABLE_NET=OFF -DENABLE_XML=OFF -DENABLE_DATA=OFF -DENABLE_MONGODB=OFF -DENABLE_PAGECOMPILER=OFF -DENABLE_PAGECOMPILER_FILE2PAGE=OFF -DENABLE_NETSSL=OFF -DPOCO_STATIC=ON -DCMAKE_INSTALL_PREFIX:PATH=C:\Users\user\Desktop\stackPocoStatic\built

输出:

-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/Qt/Tools/mingw530_32/bin/gcc.exe
-- Check for working C compiler: C:/Qt/Tools/mingw530_32/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Qt/Tools/mingw530_32/bin/g++.exe
-- Check for working CXX compiler: C:/Qt/Tools/mingw530_32/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Poco package version: 1.7.8p3-all (2017-06-22)
-- Setting Poco build type - RelWithDebInfo
-- Found message compiler: C:/Program Files/Microsoft SDKs/Windows/v7.1/Bin/MC.Exe
-- Building static libraries
-- Building without tests & samples
-- Build with using internal copy of sqlite, libz, pcre, expat, ...
-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing:
 OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR)
-- CMake 3.8.2 successfully configured Poco using MinGW Makefiles generator
-- Installation target path: C:/Users/user/Desktop/stackPocoStatic/built
-- C_FLAGS:  =
-- CMAKE_C_FLAGS_DEBUG:=-g   -D_DEBUG
-- CMAKE_C_FLAGS_RELEASE:=-O3 -DNDEBUG
-- CMAKE_C_FLAGS_MINSIZEREL:=-Os -DNDEBUG
-- CMAKE_C_FLAGS_RELWITHDEBINFO:=-O2 -g -DNDEBUG
--
--
-- CXX_FLAGS:=
-- CMAKE_CXX_FLAGS_DEBUG:=-g -D_DEBUG
-- CMAKE_CXX_FLAGS_RELEASE:=-O3 -DNDEBUG
-- CMAKE_CXX_FLAGS_MINSIZEREL:=-Os -DNDEBUG
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO:=-O2 -g -DNDEBUG
-- Building: XML
-- Building: JSON
-- Building: Util
-- Building: Net
-- Building: Zip
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/user/Desktop/stackPocoStatic/poco-1.7.8p3-all

然后我运行make:

PS C:\Users\user\Desktop\stackPocoStatic\poco-1.7.8p3-all> mingw32-make.exe
[ 43%] Built target Foundation
[ 60%] Built target XML
[ 63%] Built target JSON
[ 70%] Built target Util
[ 94%] Built target Net
[100%] Built target Zip
PS C:\Users\user\Desktop\stackPocoStatic\poco-1.7.8p3-all> mingw32-make.exe install
...

它看起来没问题,所以我想编译并运行代码,例如:

#include <iostream>
#include <fstream>
#include <Poco/Zip/Compress.h>

int main( int argc, char * argv[] )
{
    std::ofstream outputZipArchive("archive.zip", std::ios::binary);

    Poco::Zip::Compress compressor( outputZipArchive, true );
    for( unsigned i = 1; i < argc; ++i )
    {
        const char * fileToZip = argv[i];
        std::cout << i << ") Adding: " << fileToZip << std::endl;
        try
        {
            compressor.addFile( fileToZip, fileToZip );
        }
        catch(const Poco::FileNotFoundException & e)
        {
            std::cerr << "Can not add file " << fileToZip << ", because: " << e.what() << std::endl;
        }
    }

    compressor.close();
    outputZipArchive.close();
}

在编译期间我看到链接错误:

PS C:\Users\user\Desktop> g++.exe z.cc -isystemC:\Users\user\Desktop\stackPocoStatic\built\include -LC:\Users\user\Desktop\stackPocoStatic\built\lib\  -lPocoFoundation -lPocoZip
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x57): undefined reference to `_imp___ZN4Poco3Zip8CompressC1ERSob'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0xda): undefined reference to `_imp___ZN4Poco4PathC1EPKc'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0xf2): undefined reference to `_imp___ZN4Poco4PathC1EPKc'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x127): undefined reference to `_imp___ZN4Poco3Zip8Compress7addFileERKNS_4PathES4_NS0_9ZipCommon17CompressionMethodENS5_16CompressionLevelE'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x139): undefined reference to `_imp___ZN4Poco4PathD1Ev'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x148): undefined reference to `_imp___ZN4Poco4PathD1Ev'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x166): undefined reference to `_imp___ZN4Poco3Zip8Compress5closeEv'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x175): undefined reference to `_imp___ZN4Poco3Zip10ZipArchiveD1Ev'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x191): undefined reference to `_imp___ZN4Poco3Zip8CompressD1Ev'

C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x1bb): undefined reference to `_imp___ZN4Poco4PathD1Ev'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x1d0): undefined reference to `_imp___ZN4Poco4PathD1Ev'
C:\Users\user\AppData\Local\Temp\ccg0K4lf.o:z.cc:(.text+0x276): undefined reference to `_imp___ZN4Poco3Zip8CompressD1Ev'

collect2.exe: error: ld returned 1 exit status 

有趣的是,当我运行./configure时没有选项 -DPOCO_STATIC = ON

cmake -G "MinGW Makefiles" -DENABLE_CRYPTO=OFF -DENABLE_NET=OFF -DENABLE_XML=OFF -DENABLE_DATA=OFF -DENABLE_MONGODB=OFF -DENABLE_PAGECOMPILER=OFF -DENABLE_PAGECOMPILER_FILE2PAGE=OFF -DENABLE_NETSSL=OFF -DCMAKE_INSTALL_PREFIX:PATH=C:\Users\user\Desktop\stackPocoDynamic\built

然后

mingw32-make.exe && mingw32-make.exe install

然后编译:

PS C:\Users\user\Desktop> g++.exe z.cc -isystemC:\Users\user\Desktop\stackPocoDynamic\built\include -LC:\Users\user\Desk
top\stackPocoDynamic\built\lib\  -lPocoFoundation -lPocoZip

没有链接错误。当然这次我需要.dll(libPocoZip.dll和libPocoFoundation.dll)来运行代码:

 .\a.exe file.txt
 1) Adding: file.txt

我当然试过了: Static and Dynamic/Shared Linking with MinGW -Wl,-Bstatic

PS C:\Users\user\Desktop> g++.exe -Bstatic z.cc -isystemC:\Users\user\Desktop\stackPocoStatic\built\include -LC:\Users\user\Desktop\stackPocoStatic\built\lib\  -lPocoFoundation -lPocoZip  

我还尝试添加选项 -static Statically linking libraries in MinGW

PS C:\Users\user\Desktop> g++.exe z.cc -static -isystemC:\Users\user\Desktop\stackPocoStatic\built\include -LC:\Users\us
er\Desktop\stackPocoStatic\built\lib\  -lPocoFoundation -lPocoZip

不幸的是,仍然存在链接错误。

在Windows上使用MSYS进行编译时,我注意到了类似的链接错误:

./configure  --static --no-tests --no-sharedlibs --omit=XML,JSON,Util,NetSSL_OpenSSL,Data,Data/SQLite,Data/ODBC,Data/MySQL,MongoDB,PageCompiler,PageCompiler/File2Page,Crypto,PDF,CppParser --config=MinGW 

所以我的问题是: 是否可以在Windows上静态编译MinGW的POCO C ++库?

0 个答案:

没有答案