首页 > 解决方案 > VS 2017 在编译适用于 Linux 的 CMake 项目时出现意外行为

问题描述

我有一个使用 g++ 在 Linux 上运行良好的 CMake 项目。

在 Windows(10,Creators Update)上,CMake 运行良好并生成相关文件。

所以我打开了 TestProject.sln 解决方案文件并进行了测试运行,我运行了“本地 Windows 调试器”并得到了这个错误。

Error   LNK1104 cannot open file 'jsoncpp.lib'

这就是我设置这个项目的方式。

.
|-root
|  |- src
|  |- lib
|  |- bin
|  |- app
|  |- include
|  |- extras
|        |- jsoncpp
|-build

我不太确定这里可能出了什么问题。

我已将 jsoncpp 添加为子模块。

我做了

git submodule update --init --recursive

并且有一个 jsoncpp.lib 文件

root\lib\Debug

这是我的 CMakeLists.txt

cmake_minimum_required (VERSION 3.13)
project (TestProject)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/root/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/root/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/root/bin)

set(SOURCE_DIR ${PROJECT_SOURCE_DIR}/root/src)
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/root/include)
set(EXTRAS_DIR ${PROJECT_SOURCE_DIR}/root/extras)
set(PROJECT_MAIN_DIR ${PROJECT_SOURCE_DIR}/root/app)

set(SOURCES
  ${INCLUDE_DIR}/Combinations.hpp  
  ${INCLUDE_DIR}/Lib.hpp  
  ${SOURCE_DIR}/Lib.cpp
  )

add_executable(TestProject ${PROJECT_MAIN_DIR}/Main.cpp ${SOURCES})

add_subdirectory(${EXTRAS_DIR}/jsoncpp jsoncpp)

target_link_libraries(TestProject jsoncpp)

set_target_properties(TestProject PROPERTIES
  CXX_STANDARD_REQUIRED ON
  CXX_STANDARD 17
  CXX_EXTENSIONS OFF
  )

这是 CMake 的输出

Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.17134.
The C compiler identification is MSVC 19.16.27026.1
The CXX compiler identification is MSVC 19.16.27026.1
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.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:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
JsonCpp Version: 1.8.4
Looking for C++ include clocale
Looking for C++ include clocale - found
Looking for localeconv
Looking for localeconv - found
Looking for C++ include sys/types.h
Looking for C++ include sys/types.h - found
Looking for C++ include stdint.h
Looking for C++ include stdint.h - found
Looking for C++ include stddef.h
Looking for C++ include stddef.h - found
Check size of lconv
Check size of lconv - done
Performing Test HAVE_DECIMAL_POINT
Performing Test HAVE_DECIMAL_POINT - Success
Found PythonInterp: C:/Users/USERNAME/emacs-26.1-x86_64/bin/python2.exe (found suitable version "2.7.14", minimum required is "2.6") 
Configuring done
Generating done

我对 Visual Studio 不是很熟悉,但查看错误时,它显示“无法打开”而不是“找不到”,所以我的第一个想法可能是权限问题。

所以我用管理员权限启动了 VS,但错误仍然存​​在。

我想也许有一些编译器问题,所以我使用 nmake.exe 作为编译器(在生成 nmake makefile 之后),但也没有运气。

我不确定这是否是链接器问题,因为 CMake 能够找到并链接 jsoncpp 库,所以我怀疑 CMake 在构建 Makefile 时犯了错误。

我不确定我应该如何调试这个问题。

标签: c++visual-studiovisual-c++cmakec++17

解决方案


推荐阅读