如果generator是nmake或msvc,则Cmake不会正确生成共享库

时间:2017-10-21 19:45:14

标签: c++ cmake mingw nmake

我正在尝试使用CMake使用expat库生成简单的可执行文件。 我的环境Windows 7,CMake 3.10。

For generator "CodeBlocks - MinGW Makefiles" there is no problem.

For generator "CodeBlocks - NMake Makefiles" dll seems ...empty? LNK1120

cmake_minimum_required(VERSION 3.10)

project(expatTraining)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".dll")
if (CMAKE_GENERATOR MATCHES "NMake Makefiles")
    message(INFO " NMake Makefiles here")
    SET(PC_EXPAT_LIBRARY_DIRS "C:/lib/nmake/lib;C:/lib/nmake/bin")
    SET(PC_EXPAT_INCLUDE_DIRS C:/lib/nmake/include)
    list(APPEND CMAKE_MODULE_PATH "C:/lib/nmake/lib")
    list(APPEND CMAKE_PREFIX_PATH "C:/lib/nmake/lib")
elseif (CMAKE_GENERATOR MATCHES "^Visual Studio 15 2017")
    message(INFO " Visual Studio 15 2017 here")
    SET(PC_EXPAT_LIBRARY_DIRS "C:/lib/msvc/lib;C:/lib/msvc/bin")
    SET(PC_EXPAT_INCLUDE_DIRS C:/lib/msvc/include)
    list(APPEND CMAKE_MODULE_PATH "C:/lib/msvc/lib")
    list(APPEND CMAKE_PREFIX_PATH "C:/lib/msvc/lib")
elseif (CMAKE_GENERATOR MATCHES "MinGW Makefiles")
    message(INFO " MinGW Makefiles here")
    SET(PC_EXPAT_LIBRARY_DIRS "C:/lib/mingw/lib")
    SET(PC_EXPAT_INCLUDE_DIRS C:/lib/mingw/include)
    list(APPEND CMAKE_MODULE_PATH "C:/lib/mingw/lib")
    list(APPEND CMAKE_PREFIX_PATH "C:/lib/mingw/lib")
else ()
    message(INFO " another generator here")
endif ()
set(CMAKE_CXX_STANDARD 11)

FIND_PACKAGE(Expat REQUIRED)
add_executable(expatTraining m.cpp)
target_link_libraries(expatTraining EXPAT::EXPAT)

CLion的输出: 问题是“LNK1120”

    "C:\Program Files\CMake\bin\cmake.exe" --build C:\dx\expatTraining\cmake-build-release --target expatTraining --
Scanning dependencies of target expatTraining
[ 50%] Building CXX object CMakeFiles/expatTraining.dir/m.cpp.obj
m.cpp
[100%] Linking CXX executable expatTraining.exe
LINK: command "C:\PROGRA~2\MICROS~2\2017\COMMUN~1\VC\Tools\MSVC\1410~1.250\bin\HostX86\x86\link.exe /nologo @CMakeFiles\expatTraining.dir\objects1.rsp /out:expatTraining.exe /implib:expatTraining.lib /pdb:C:\dx\expatTraining\cmake-build-release\expatTraining.pdb /version:0.0 /machine:X86 /INCREMENTAL:NO /subsystem:console C:\lib\nmake\lib\expat.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:expatTraining.exe.manifest" failed (exit code 1120) with the following output:
m.cpp.obj : error LNK2019: nierozpoznany zewnŕtrzny symbol __imp__XML_ExpatVersion przywo│any w funkcji _main
expatTraining.exe : fatal error LNK1120: liczba nierozpoznanych elementˇw zewnŕtrznych: 1
NMAKE : fatal error U1077: „"C:\Program Files\CMake\bin\cmake.exe"”: kod powrotu „0xffffffff”
Stop.
NMAKE : fatal error U1077: „"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x86\nmake.exe"”: kod powrotu „0x2”
Stop.
NMAKE : fatal error U1077: „"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x86\nmake.exe"”: kod powrotu „0x2”
Stop.
NMAKE : fatal error U1077: „"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x86\nmake.exe"”: kod powrotu „0x2”
Stop.

m.cpp的身体

#include <iostream>
#include <expat.h>
int main() {
    std::cout << "Hello, World!" << std::endl;
    XML_Content_Quant::XML_CQUANT_NONE;
    XML_Parser parser = XML_ParserCreate(NULL);
    XML_ExpatVersion();
    return 0;
}

0 个答案:

没有答案