Reputation: 810
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 :
#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 :
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 ..
. The program compiles and works well on Release (cmake --build . --config Release
). But in Debug (cmake --build . --config Debug
, I have the following error :
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. 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". What am I missing ?
Upvotes: 1
Views: 879
Reputation: 21514
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).
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:
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.
Upvotes: 1