Skip to content

Instantly share code, notes, and snippets.

@laurynas
Last active November 6, 2016 15:37
Show Gist options
  • Save laurynas/df1ffe0451523e7e3b094d09567e7da9 to your computer and use it in GitHub Desktop.
Save laurynas/df1ffe0451523e7e3b094d09567e7da9 to your computer and use it in GitHub Desktop.

> crystal test.cr

Undefined symbols for architecture x86_64:
  "std::string::_Rep::_M_destroy(std::allocator<char> const&)", referenced from:
      Hello::hello(char const*) in hello.a(libhello.o)
  "std::string::_Rep::_S_empty_rep_storage", referenced from:
      Hello::hello(char const*) in hello.a(libhello.o)
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
      Hello::hello(char const*) in hello.a(libhello.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of command failed with code: 1: `cc -o "/Users/laurynas/.cache/crystal/crystal-run-test.tmp" "${@}"  -rdynamic  /Users/laurynas/temp/test_crystal/build/Release/hello.a -lstdc++ -lpcre -lgc -lpthread /usr/local/Cellar/crystal-lang/0.19.4/src/ext/libcrystal.a -levent -liconv -ldl -L/usr/lib -L/usr/local/lib`

test.cr

require "./lib_hello"

LibHello.hello("Bob")

lib_hello.cr

@[Link("stdc++", ldflags: "/Users/laurynas/temp/test_crystal/build/Release/hello.a")]
lib LibHello
  fun hello = hello_wrapper(name : UInt8*)
end

libhello_wrapper.cpp

#include "libhello.h"

extern "C" {
  void hello_wrapper(const char* name) {
    Hello::hello(name);
  }
}

libhello.cpp

#include <stdio.h>
#include <string>

namespace Hello {
  void hello(const char* name) {
    std::string test(name);
    printf("Hello %s.\n", test.c_str());
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment