英文:
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-dirs 和 extra-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
⋮
这里有两个更改:
- 在正常情况下,
include目录只包含.h文件。这是C的惯例。如果你真的想要另一层目录,可以命名为cbits/src或cbits/lib或类似的。 - 手动管理
.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:
- Under normal circumstances, an
includedirectory has.hfiles only. This is a C convention. If you really want another layer of directory, you could name itcbits/srcorcbits/libor similar instead. - 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论