Search code examples
c++yamlsublimetext3sublimetextyaml-cpp

How to build yaml-cpp on Windows/Cygwin on Sublime Text?


How to build yaml-cpp on Windows/Cygwin on Sublime Text?

I am on windows with Cygwin:

$ g++ --version
g++ (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

undefined reference to `YAML::Parser::~Parser()

#include "yaml-cpp/yaml.h"

#include <iostream>
#include <sstream>
#include <cstdlib>

#include <fstream>
#include <string>
#include <vector>

int main( int argumentsCount, char* argumentsStringList[] )
{
    std::ifstream fin("test.beauty-blocks");
    YAML::Parser parser(fin);
}

Command:

g++ --std=c++11 main.cpp -I . -I ./libraries/yaml-cpp/include -L./libraries/yaml-cpp -lyaml-cpp -o main

-->

g++ --std=c++11 main.cpp -I . -I ./libraries/yaml-cpp/include -L./libraries/yaml-cpp -lyaml-cpp -o main
C:\Users\PROFES~1\AppData\Local\Temp\ccRhYWZh.o:main.cpp:(.text+0x1b1): undefined reference to `YAML::Parser::Parser(std::istream&)'
C:\Users\PROFES~1\AppData\Local\Temp\ccRhYWZh.o:main.cpp:(.text+0x2d8): undefined reference to `YAML::Parser::~Parser()'
C:\Users\PROFES~1\AppData\Local\Temp\ccRhYWZh.o:main.cpp:(.text+0x38e): undefined reference to `YAML::Parser::~Parser()'
collect2.exe: error: ld returned 1 exit status

Related to https://github.com/jbeder/yaml-cpp/issues/456

I built yaml-cpp with:

$ cmake -G "Unix Makefiles"
$ make
Scanning dependencies of target yaml-cpp
[  1%] Building CXX object CMakeFiles/yaml-cpp.dir/src/binary.cpp.o
[  3%] Building CXX object CMakeFiles/yaml-cpp.dir/src/convert.cpp.o
[  5%] Building CXX object CMakeFiles/yaml-cpp.dir/src/directives.cpp.o
[  6%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emit.cpp.o
[  8%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emitfromevents.cpp.o
[ 10%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emitter.cpp.o
[ 11%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emitterstate.cpp.o
[ 13%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emitterutils.cpp.o
[ 15%] Building CXX object CMakeFiles/yaml-cpp.dir/src/exceptions.cpp.o
[ 16%] Building CXX object CMakeFiles/yaml-cpp.dir/src/exp.cpp.o
[ 18%] Building CXX object CMakeFiles/yaml-cpp.dir/src/memory.cpp.o
[ 20%] Building CXX object CMakeFiles/yaml-cpp.dir/src/node.cpp.o
[ 21%] Building CXX object CMakeFiles/yaml-cpp.dir/src/node_data.cpp.o
[ 23%] Building CXX object CMakeFiles/yaml-cpp.dir/src/nodebuilder.cpp.o
[ 25%] Building CXX object CMakeFiles/yaml-cpp.dir/src/nodeevents.cpp.o
[ 26%] Building CXX object CMakeFiles/yaml-cpp.dir/src/null.cpp.o
[ 28%] Building CXX object CMakeFiles/yaml-cpp.dir/src/ostream_wrapper.cpp.o
[ 30%] Building CXX object CMakeFiles/yaml-cpp.dir/src/parse.cpp.o
[ 31%] Building CXX object CMakeFiles/yaml-cpp.dir/src/parser.cpp.o
[ 33%] Building CXX object CMakeFiles/yaml-cpp.dir/src/regex_yaml.cpp.o
[ 35%] Building CXX object CMakeFiles/yaml-cpp.dir/src/scanner.cpp.o
[ 36%] Building CXX object CMakeFiles/yaml-cpp.dir/src/scanscalar.cpp.o
[ 38%] Building CXX object CMakeFiles/yaml-cpp.dir/src/scantag.cpp.o
[ 40%] Building CXX object CMakeFiles/yaml-cpp.dir/src/scantoken.cpp.o
[ 41%] Building CXX object CMakeFiles/yaml-cpp.dir/src/simplekey.cpp.o
[ 43%] Building CXX object CMakeFiles/yaml-cpp.dir/src/singledocparser.cpp.o
[ 45%] Building CXX object CMakeFiles/yaml-cpp.dir/src/stream.cpp.o
[ 46%] Building CXX object CMakeFiles/yaml-cpp.dir/src/tag.cpp.o
[ 48%] Building CXX object CMakeFiles/yaml-cpp.dir/src/contrib/graphbuilder.cpp.o
[ 50%] Building CXX object CMakeFiles/yaml-cpp.dir/src/contrib/graphbuilderadapter.cpp.o
[ 51%] Linking CXX static library libyaml-cpp.a
[ 51%] Built target yaml-cpp
Scanning dependencies of target gmock
[ 53%] Building CXX object test/gmock-1.7.0/CMakeFiles/gmock.dir/gtest/src/gtest-all.cc.o
[ 55%] Building CXX object test/gmock-1.7.0/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 56%] Linking CXX static library libgmock.a
[ 56%] Built target gmock
Scanning dependencies of target run-tests
[ 58%] Building CXX object test/CMakeFiles/run-tests.dir/main.cpp.o
[ 60%] Building CXX object test/CMakeFiles/run-tests.dir/ostream_wrapper_test.cpp.o
[ 61%] Building CXX object test/CMakeFiles/run-tests.dir/regex_test.cpp.o
[ 63%] Building CXX object test/CMakeFiles/run-tests.dir/integration/emitter_test.cpp.o
[ 65%] Building CXX object test/CMakeFiles/run-tests.dir/integration/encoding_test.cpp.o
[ 66%] Building CXX object test/CMakeFiles/run-tests.dir/integration/gen_emitter_test.cpp.o
[ 68%] Building CXX object test/CMakeFiles/run-tests.dir/integration/handler_spec_test.cpp.o
[ 70%] Building CXX object test/CMakeFiles/run-tests.dir/integration/handler_test.cpp.o
In file included from D:/source/libraries/yaml-cpp/test/integration/handler_test.cpp:2:0:
D:/source/libraries/yaml-cpp/test/specexamples.h:2:13: warning: ‘{anonymous}::ex2_1’ defined but not used [-Wunused-variable]
 const char *ex2_1 =
             ^
D:/source/libraries/yaml-cpp/test/specexamples.h:7:13: warning: ‘{anonymous}::ex2_2’ defined but not used [-Wunused-variable]
 const char *ex2_2 =
             ^
...
D:/source/libraries/yaml-cpp/test/specexamples.h:839:13: warning: ‘{anonymous}::ex8_22’ defined but not used [-Wunused-variable]
 const char *ex8_22 =
             ^
cc1plus: warning: unrecognized command line option ‘-Wno-c99-extensions’
[ 71%] Building CXX object test/CMakeFiles/run-tests.dir/integration/load_node_test.cpp.o
[ 73%] Building CXX object test/CMakeFiles/run-tests.dir/integration/node_spec_test.cpp.o
[ 75%] Building CXX object test/CMakeFiles/run-tests.dir/node/node_test.cpp.o
[ 76%] Linking CXX executable run-tests.exe
[ 76%] Built target run-tests
Scanning dependencies of target gmock_main
[ 78%] Building CXX object test/gmock-1.7.0/CMakeFiles/gmock_main.dir/gtest/src/gtest-all.cc.o
[ 80%] Building CXX object test/gmock-1.7.0/CMakeFiles/gmock_main.dir/src/gmock-all.cc.o
[ 81%] Building CXX object test/gmock-1.7.0/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 83%] Linking CXX static library libgmock_main.a
[ 83%] Built target gmock_main
Scanning dependencies of target gtest
[ 85%] Building CXX object test/gmock-1.7.0/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 86%] Linking CXX static library libgtest.a
[ 86%] Built target gtest
Scanning dependencies of target gtest_main
[ 88%] Building CXX object test/gmock-1.7.0/gtest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[ 90%] Linking CXX static library libgtest_main.a
[ 90%] Built target gtest_main
Scanning dependencies of target parse
[ 91%] Building CXX object util/CMakeFiles/parse.dir/parse.cpp.o
[ 93%] Linking CXX executable parse.exe
[ 93%] Built target parse
Scanning dependencies of target sandbox
[ 95%] Building CXX object util/CMakeFiles/sandbox.dir/sandbox.cpp.o
[ 96%] Linking CXX executable sandbox.exe
[ 96%] Built target sandbox
Scanning dependencies of target read
[ 98%] Building CXX object util/CMakeFiles/read.dir/read.cpp.o
[100%] Linking CXX executable read.exe
[100%] Built target read

Originally posted on https://github.com/jbeder/yaml-cpp/issues/481


Update

I just noticed this error only happens if I call it from a shell script called by Sublime Text .sublime-build file. If I directly call the command g++ --std=c++11 main.cpp -I. -I./libraries/yaml-cpp/include -L./libraries/yaml-cpp -lyaml-cpp -o main on the command line, it works.

These are the Sublime Text .sublime-project:

{
    "folders":
    [
        {
            "path": ".",
        },
    ],
    "build_systems":
    [
        {
            "file_regex": "^(..[^:]*):([\\d+]):?(\\d+)?:? (.*)$|^\\(.*\\)\\((\\d+)\\)(.*)$",
            // "selector": "source.c, source.c++, source.c++11", // comment this to build from everywhere

            "name": "Build Main file",
            "cmd": ["sh", "make_run.sh", "main"],

            "working_dir": "$project_path/source",

            "target": "exec",
            "syntax": "Packages/Text/Plain text.tmLanguage",
        }
    ]
}

And this is the make_run.sh file:

g++ --std=c++11 main.cpp -I. -I./libraries/yaml-cpp/include -L./libraries/yaml-cpp -lyaml-cpp -o main
exit 0

Was also opened a issue on https://github.com/SublimeTextIssues/Core/issues/1632 _ Why Sublime Text is making the linker ld.exe not link?


Solution

  • I found out to instead of running things inside the buggy Sublime Text environment, call/open a shell window to run the shell script:

    #!/bin/sh
    
    
    CURRENT_PATH=$(pwd)
    COMMAND_TO_RUN="pwd; echo $CURRENT_PATH; cd $CURRENT_PATH; sh make_run.sh $@"
    
    echo "choose_a_compiler.sh: $CURRENT_PATH"
    
    
    current_terminal="mintty"
    
    if command -v $current_terminal >/dev/null 2>&1; then
        /bin/$current_terminal -w max -h always -e /bin/bash --login -i -c "$COMMAND_TO_RUN"
        exit 0
    fi
    
    
    current_terminal="xfce4-terminal"
    
    if command -v $current_terminal >/dev/null 2>&1; then
        /usr/bin/$current_terminal --maximize --hold --command="$COMMAND_TO_RUN"
        exit 0
    fi
    
    
    # TODO
    # "konsole" "gnome-terminal" "xterm"
    

    Just replaced the "make_run.sh on the .sublime-build file by this script choose_a_compiler.sh, and inside this script, call your make_run.sh. Now everything is building fine, but sadly it is on a new windows, instead of on the Sublime Text build output console.