小编And*_*res的帖子

Swift:如何调用从dylib加载的C函数

有没有办法调用从Swift的dylib加载的C函数?

这是我的dylib文件:

cppdemofile.cpp

#include "cppdemofile.h"

int add(int a, int b) {
    return a + b;
}
Run Code Online (Sandbox Code Playgroud)

cppdemofile.h

#ifndef __CppDemoLibrary__cppdemofile__
#define __CppDemoLibrary__cppdemofile__

#pragma GCC visibility push(default)

extern "C" int add(int a, int b);

#pragma GCC visibility pop

#endif
Run Code Online (Sandbox Code Playgroud)

编译到dylib并检查:

nm -gU libCppDemoLibrary.dylib
0000000000000f80 T _add
Run Code Online (Sandbox Code Playgroud)

......复制libCppDemoLibrary.dylib~/lib......

Swift程序:

@IBAction func buttonClick(sender: NSButton) {
    let handle = dlopen("libCppDemoLibrary.dylib", RTLD_NOW)
    if (handle != nil) {
        var sym = dlsym(handle, "add")
        if (sym != nil) {
            let pointer …
Run Code Online (Sandbox Code Playgroud)

c macos dylib swift

9
推荐指数
1
解决办法
3391
查看次数

Scala 使用猫 IO/Either 而不是 Future/Exceptions

我正在采用IO/在适用的情况下Either替换Future/ Exception,但我需要以下代码的帮助:

// some Java library
def dbLoad(id: Int): Int = {
  throw new Exception("db exception")
}

// my scala code
sealed trait DbError extends Exception with Product

object DbError {
  case object SomeError extends DbError
}

val load: Int => IO[Either[DbError, Int]] = { id =>
  IO.fromFuture { IO { Future {
    try { Right(dbLoad(id)) } catch { case NonFatal(e) => Left(SomeError) }
  } } }
}

val loadAll: IO[Either[DbError, (Int, Int, …
Run Code Online (Sandbox Code Playgroud)

scala cats-effect

2
推荐指数
1
解决办法
1548
查看次数

PHP cURL返回错误505

我正在尝试使cURL(或get_file_contents)从以下URL获取内容:

http://localhost:8080/solr/select?q={!geofilt score=distance sfield=geo pt=20.570529,-100.408635 d=20}&sort=score asc&fq=restaurantes&defType=!edismax&wt=json&indent=true&start=0&rows=20&fl=*,score
Run Code Online (Sandbox Code Playgroud)

我从两种方法(cURL和get_file_contents)得到的都是HTTP 505错误.但是,如果我将链接复制并粘贴到像Firefox这样的浏览器上,它可以正常工作......

我尝试的最后一件事是迫使cURL使用不同的HTTP版本,如下所示:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
Run Code Online (Sandbox Code Playgroud)

没有运气.

我的代码适用于其他网址,如下所示:

http://localhost:8080/solr/select?q=restaurantes&defType=edismax&qf=category^20.0+name^10.0+keywords^10.0+address^2.0&wt=json&indent=true&start=0&rows=20&fl=*,score
Run Code Online (Sandbox Code Playgroud)

明显的区别在于{,=,}等字符!在URL中.但是,为什么Firefox有效?

php curl solr

1
推荐指数
1
解决办法
2638
查看次数

标签 统计

c ×1

cats-effect ×1

curl ×1

dylib ×1

macos ×1

php ×1

scala ×1

solr ×1

swift ×1