1

I built yaml-cpp staticly using vs2019 and got lib from path yaml-cpp-master\build\Release\yaml-cpp.lib, now I intend to add it to my own project, simple code follows:

#include <yaml-cpp/yaml.h>
#include <string>

using namespace std;

int main() {
    YAML::Node config = YAML::LoadFile("test.yaml");
    if (config["test1"]) {
        cout << config["test1"].as<string>() << endl;
    }
    return 0;
}

I've set my project properties following How to add static libraries to a Visual studio project, but I meet LNK2001 error and C4251 warning when building:

LNK2001 "__declspec(dllimport) public: __cdecl YAML::Node::Node(void)" (__imp_??0Node@YAML@@QEAA@XZ)

it seems the linker still wants dll...which is not my intention, is there any possible solutions?

1 Answer 1

3

You need to #define YAML_CPP_STATIC_DEFINE before you include any yaml-cpp files or in your project processor settings.

5
  • cool, I never had a thought on it... thank you for your answer! Commented Feb 16, 2022 at 7:01
  • I still have linker errors even after defining this. I defined it for yaml-cpp project which I build as a static library. I have several linker errors for some functions that I am using inside my classes. Mostly it's unresolved external symbol declspec(dllimport) stuff. Commented May 26, 2022 at 17:23
  • @ŽarkoTomičić I suggest you post a new question with a minimal reproducible example though the answer is probably in stackoverflow.com/questions/12573816/… Commented May 26, 2022 at 17:33
  • This worked for me, but where is this documented? How is one supposed to know to do this without Googling and ending up here? Commented Dec 15, 2023 at 1:29
  • @Arrow_Raider it doesn't seem to be, you could always raise an issue or pull request on the yaml-cpp project to fix it. They probably just expect people to import their library via cmake which i assume will set this automatically Commented Dec 15, 2023 at 5:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.