VS2019 uses yaml third-party library, LNK2001 error occurs (mainly dynamic and static library, 32-bit, 64-bit problem)

1. Preliminary work preparation

1. Download the yaml third-party library source code

Git down in the newly created directory (the git environment has been configured)

git clone https://github.com/jbeder/yaml-cpp

Or go directly to this website to download the zip and extract it to your own directory.

2. Download cmake

https://cmake.org/download/

Basically, the next step is to install

3. cmake generation

 The third step is to configure the specific content of the environment

 4. Enter the build directory and generate the yaml-cpp.lib file

 Generate lib files under the directory release.

5. The project uses the lib file

        Configure the relevant environment, refer to  vs2013 to refer to the third-party dynamic link library, set include, lib, dll path issues_cbzhunian's blog-CSDN blog

The header file of yaml is in

library files in 

 test file

#include <iostream>
#include <fstream>
#include "yaml-cpp/yaml.h"
#pragma comment(lib, "yaml-cpp.lib")

int main()
{
    YAML::Emitter out;
    out << "Hello, World!";

    std::cout << "Here's the output YAML: " << out.c_str();
    std::cout << std::endl;

    YAML::Node config = YAML::LoadFile("config.yaml");

    if (config["lastLogin"]) {
        std::cout << "Last logged in: " << config["lastLogin"].as<std::string>() << std::endl;
    }

    const std::string username = config["username"].as<std::string>();
    const std::string password = config["password"].as<std::string>();

    config["lastLogin"] = "2022-05-24 10:26:10";


    std::cout << "username: " << username << ", password: " << password << std::endl;

    std::ofstream fout("config.yaml");
    fout << config;

    return 0;
}

yaml file

lastLogin: 2022-05-24 10:26:10
username: root
password: 123

Compile will appear LNK2001 error 

Two, LNK2001 error occurs

        An error occurs because the lib file compiled above. The default is a static library, click on the yaml header file to view it.

 There is a keyword of __declspec(dllimport), but this keyword cannot appear in a static library, so it will cause a LNK2001 error.

Recompile the yaml file as a dynamic library

 Compile again successfully, remember to copy the dll file to the running directory of the exe file, or refer to vs2013 to reference the third-party dynamic link library, set include, lib, dll path problems_cbzhunian's blog-CSDN blog

to configure. 

Guess you like

Origin blog.csdn.net/qq_38295645/article/details/124943296