简体   繁体   English

使用 Visual Studio 2019 和 cmake 调试 Yaml_cpp

[英]Yaml_cpp in debug with visual studio 2019 and cmake

I'm trying to understand how to use my linux project on windows with visual studio 2019. So I write a simple test for yaml_cpp:我试图了解如何使用 Visual Studio 2019 在 windows 上使用我的 linux 项目。所以我为 yaml_cpp 编写了一个简单的测试:

#include "yaml-cpp/yaml.h"

#include <iostream>
#include <string>
#include <cassert>

int main() 
{
    try 
    {
        assert(1 == 2);
        YAML::Node config = YAML::LoadFile("config.yaml");
        std::cerr << config["hello"].as<std::string>() << std::endl;

        
    }
    catch (std::exception& e)
    {
        std::cerr << "Caught " << e.what() << std::endl;
        std::cerr << "Type " << typeid(e).name() << std::endl;
    };

    return 0;
}

and I write a basic CMakeLists.txt file:我写了一个基本的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.1)

project (test)

set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE) 

get_filename_component(PARENT_DIR_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} DIRECTORY)

find_package(yaml-cpp REQUIRED PATHS "${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/share/cmake/yaml-cpp")

add_executable(test
  main.cpp)
  
  target_include_directories(test
  PUBLIC
  ${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/include >
  )

target_link_libraries(test debug yaml-cppd optimized yaml-cpp)

I generate the project with cmake -G "Visual Studio 16 2019" -Ax64.. .我使用cmake -G "Visual Studio 16 2019" -Ax64..生成项目。 The program compiles and works well on Release ( cmake --build. --config Release ).该程序在 Release ( cmake --build. --config Release ) 上编译并运行良好。 But in Debug ( cmake --build. --config Debug , I have the following error:但是在调试( cmake --build. --config Debug ,我有以下错误:

LINK : fatal error LNK1104: impossible d'ouvrir le fichier 'yaml-cppd.lib' [C:\Users\kafka\dev\C++\test\yaml_for_vs\bui
ld\test.vcxproj]

"Impossible d'ouvrir le fichier" means the file 'yaml-cppd.lib' can't be open. “Impossible d'ouvrir le fichier”表示文件“yaml-cppd.lib”无法打开。 I have compile yaml-cpp in release and debug and the file 'yaml-cppd.lib' is present in "C:\Program Files (x86)\YAML_CPP\lib".我已经在发布和调试中编译了 yaml-cpp,并且文件“yaml-cppd.lib”存在于“C:\Program Files (x86)\YAML_CPP\lib”中。 What am I missing?我错过了什么?

It's definitely wierd that release file is found while debug is not.在没有调试的情况下找到发布文件肯定很奇怪。 You should look at Visual Studio's test properties page (under "Editeur de liens", check "Dependances supplémentaires" in "entrée", and "Répertoire de bibliothèques supplémentaires" in "Général", then switch between "Debug" and "Release" config and see what's different).您应该查看 Visual Studio 的test属性页面(在“Editeur de Liens”下,检查“entrée”中的“Dependances supplémentaires”和“Général”中的“Répertoire de bibliothèques supplémentaires”,然后在“Debug”和“Release”配置之间切换看看有什么不同)。

Anyway, here are two workarounds that may fix your issue:无论如何,这里有两种解决方法可以解决您的问题:

Explicitely specify where libraries should be picked-up from:明确指定应从何处获取库:

link_directories( "${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/lib" )

Or, explictely specify.lib file to be linked:或者,明确指定要链接的.lib 文件:

target_link_libraries(test debug ${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/lib/yaml-cppd.lib optimized ${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/lib/yaml-cpp.lib)

This last one has the disadvantage of not being cross-platform (under Linux, it should be replaced by target_link_libraries(test debug ${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/lib/libyaml-cppd.so optimized ${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/lib/libyaml-cpp.so) . But it may be an acceptable workaround if the first one does not fix your issue.最后一个的缺点是不能跨平台(Linux下,应该换成target_link_libraries(test debug ${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/lib/libyaml-cppd.so optimized ${PARENT_DIR_INSTALL_PREFIX}/YAML_CPP/lib/libyaml-cpp.so) 。但如果第一个不能解决您的问题,这可能是一个可以接受的解决方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM