I'm trying to work through the YAML-Cpp tutorials and ended trying to replicate some SO posts to see if I get the same outputs as those posts.
This SO post that seems to be the same issue but I'm on Windows; and my error messages look slightly different. When I run+build in Code::Blocks it tells me there's a problem at the line where I try to use YAML::LoadFile(). Could this be related to how I "installed" (built?) yaml-cpp into a static library? Or is this a compiler thing like the SO post?
Code
#include <iostream>
#include <fstream> // Left over from a previous example I tried.
#include "yaml-cpp/yaml.h"
int main(int argc, const char *argv[]) {
YAML::Node config = YAML::LoadFile("example_file.yaml");
std::cout << config << std::endl;
return 0;
}
Note: "example_file.yaml" is the same as this one
Command line compilation
g++ -lyaml-cpp -L../../../src/libs/yaml-cpp/build -I../../../src/libs/yaml-cpp/include -std=c++17 main.cpp
Note: I'm using MSYS C++ compiler
Folder structure
myProjectFolder
├───src
│ └───libs
│ └───yaml-cpp
│ ├─── #... other stuff omitted
│ └───build
│ ├─── #... other stuff omitted
│ └───libyaml-cpp.a
└───examples
└───yaml-cpp_example
└───yaml_cpp_examples
├─── # Compile command is run in this directory
├─── #... other stuff omitted
├───example_file.yaml
└───main.cpp
Resulting output
I end up with a bunch of stuff after running the above g++ command. I think most of it is just warnings from the compiler but the last 3 lines look like error messages. I'll replicate the last 3 lines here and put all of the output at the bottom of the post.
C:\Users\myUserName\AppData\Local\Temp\ccbmBxYh.o:main.cpp:(.text+0xc3): undefined reference to `_imp___ZN4YAML8LoadFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE'
C:\Users\myUserName\AppData\Local\Temp\ccbmBxYh.o:main.cpp:(.text+0xec): undefined reference to `_imp___ZN4YAMLlsERSoRKNS_4NodeE'
collect2.exe: error: ld returned 1 exit status
Entire command line output
In file included from ../../../src/libs/yaml-cpp/include/yaml-cpp/parser.h:13:0,
from ../../../src/libs/yaml-cpp/include/yaml-cpp/yaml.h:10,
from main.cpp:22:
../../../src/libs/yaml-cpp/include/yaml-cpp/dll.h:22:65: note: #pragma message: Defining YAML_CPP_API for DLL import
# pragma message( "Defining YAML_CPP_API for DLL import" )
^
In file included from ../../../src/libs/yaml-cpp/include/yaml-cpp/yaml.h:17:0,
from main.cpp:22:
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:19:8: warning: 'YAML::Node::Node()' redeclared without dllimport attribute after being referenced with dll linkage
inline Node::Node()
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:45:8: warning: 'YAML::Node::Node(const YAML::Node&)' redeclared without dllimport attribute after being referenced with dll linkage
inline Node::Node(const Node&) = default;
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:47:8: warning: 'YAML::Node::Node(YAML::Node::Zombie)' redeclared without dllimport attribute after being referenced with dll linkage
inline Node::Node(Zombie)
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:56:8: warning: 'YAML::Node::~Node()' redeclared without dllimport attribute after being referenced with dll linkage
inline Node::~Node() = default;
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:68:13: warning: 'bool YAML::Node::IsDefined() const' redeclared without dllimport attribute after being referenced with dll linkage
inline bool Node::IsDefined() const {
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:82:24: warning: 'YAML::NodeType::value YAML::Node::Type() const' redeclared without dllimport attribute after being referenced with dll linkage
inline NodeType::value Node::Type() const {
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:166:27: warning: 'const string& YAML::Node::Scalar() const' redeclared without dllimport attribute after being referenced with dll linkage
inline const std::string& Node::Scalar() const {
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:209:14: warning: 'YAML::Node& YAML::Node::operator=(const YAML::Node&)' redeclared without dllimport attribute after being referenced with dll linkage
inline Node& Node::operator=(const Node& rhs) {
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:246:13: warning: 'void YAML::Node::AssignData(const YAML::Node&)' redeclared without dllimport attribute after being referenced with dll linkage
inline void Node::AssignData(const Node& rhs) {
^~~~
../../../src/libs/yaml-cpp/include/yaml-cpp/node/impl.h:254:13: warning: 'void YAML::Node::AssignNode(const YAML::Node&)' redeclared without dllimport attribute after being referenced with dll linkage
inline void Node::AssignNode(const Node& rhs) {
^~~~
C:\Users\myUserName\AppData\Local\Temp\ccbmBxYh.o:main.cpp:(.text+0xc3): undefined reference to `_imp___ZN4YAML8LoadFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE'
C:\Users\myUserName\AppData\Local\Temp\ccbmBxYh.o:main.cpp:(.text+0xec): undefined reference to `_imp___ZN4YAMLlsERSoRKNS_4NodeE'
collect2.exe: error: ld returned 1 exit status