0

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?

2
  • This is somewhat uncommon setup. Are you sure statically compiling C bits won't work for you? Commented Apr 21, 2023 at 8:51
  • @arrowd Let me explain it: I am writing a JIT compiler that can use external functions from libCbits following Implementing a JIT. I've tried statically compiling C bits but it didn't work. I am not quite sure if I am doing it the right way though. Commented May 3, 2023 at 8:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the corrections! I am following Implementing a JIT and in chapter 4 the author manually compiled a .so so that the JIT can call extern functions during runtime. I just want to automate the compilation using stack.
@pe200012 Huh. Could there be a typo in your link? My browser claims it can't resolve that domain to an IP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.