如何使 Haskell Stack 与 C 动态链接?

huangapple go评论50阅读模式
英文:

How to get Haskell Stack to dynamically link with C?

问题

假设我们有以下结构的项目:

.
├── CHANGELOG.md
├── LICENSE
├── README.md
├── Setup.hs
├── app
│    └── Main.hs
├── cbits
│    ├── include
│    │    └── libCbits.c
│    └── lib
│         └── libCbits.so
├── dummy.cabal
├── package.yaml
├── src
├── stack.yaml
└── stack.yaml.lock

每次更新时,需要将文件 libCbits.c 编译成 .so 文件,并与 Main.hs 进行链接。

我尝试向 package.yaml 添加 extra-lib-dirsextra-include-dirs,但未成功。

如何实现这个目标?

英文:

Suppose we have a project with the following structure

.
├── CHANGELOG.md
├── LICENSE
├── README.md
├── Setup.hs
├── app
│   └── Main.hs
├── cbits
│   ├── include
│   │   └── libCbits.c
│   └── lib
│       └── libCbits.so
├── dummy.cabal
├── package.yaml
├── src
├── stack.yaml
└── stack.yaml.lock

The file libCbits.c needs to be compiled to a .so file every time it updates with which Main.hs be linked.

I've tried adding extra-lib-dirs and extra-include-dirs to package.yml, which did not work.

How do I achieve this goal?

答案1

得分: 1

通常的做法是采用以下目录结构代替:

├── cbits
│   └── libCbits.c
├── dummy.cabal

这里有两个更改:

  1. 在正常情况下,include 目录只包含 .h 文件。这是C的惯例。如果你真的想要另一层目录,可以命名为 cbits/srccbits/lib 或类似的。
  2. 手动管理 .so 文件非常不寻常。

然后在相应的 cabal 文件中添加如下内容:

c-sources: cbits/libCbits.c
cc-options: -O3(或其他选项)

你可能处于非常不寻常的情况,这种设置不起作用。如果是这样,为了获得更具针对性的建议,你可能需要说明为何这种设置不适用。

英文:

The normal way to do this has the following directory structure instead:

⋮
├── cbits
│   └── libCbits.c
├── dummy.cabal
⋮

There are two changes here:

  1. Under normal circumstances, an include directory has .h files only. This is a C convention. If you really want another layer of directory, you could name it cbits/src or cbits/lib or similar instead.
  2. It is extremely unusual to manually manage a .so.

Then one puts some stuff like this in the appropriate cabal file:

c-sources: cbits/libCbits.c
cc-options: -O3 (or whatever)

You may be in an extremely unusual situation, where this kind of setup does not work. If so, to get more targeted advice you will probably have to say what you are trying to do that makes this setup unsuitable.

huangapple
  • 本文由 发表于 2023年4月19日 23:01:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定