【问题标题】:With haskell shake build tool. Use a local static c library使用 haskell 摇动构建工具。使用本地静态 c 库
【发布时间】:2020-03-16 10:53:17
【问题描述】:

在一个 Shake c 项目中,我创建了一个名为 libbuild3a.a 的静态库

我想通过直接在源文件中提供它来在单独的 Shake c 项目中使用它。当它尝试编译将成为 exe 的 main.c 时出现以下错误。

命令行:gcc -c src3b/main.c -Lsrc3b/lib -lbuild3a -o build3b/src3b/main.o -MMD -MF build3b/src3b/main.m 退出代码:1 标准错误: src3b/main.c:2:10:致命错误:add.h:没有这样的文件或目录 #包括

这里是摇动项目。这是一个测试项目,致力于使用 haskell ffi 调用 c。

build3b :: IO ()
build3b = shakeArgs shakeOptions{shakeFiles="build3b"} $ do
  let
    buildDir = "build3b"
    srcDir = "src3b"
  want [buildDir </> "run3b" <.> exe]

  buildDir </> "run3b" <.> exe %> \out -> do
        putInfo "============== entering exe creation ====================="

        --Do I need to load the static library?
        cs_main <- getDirectoryFiles "" [srcDir </>  "main.c", srcDir </> "lib" </> "libbuild3a.a"]
        need cs_main

        putInfo "showing main source file"
        mapM putInfo cs_main

        putInfo "starting compile of main.c main file"
        let
          os_main = [buildDir </> c -<.> "o" | c <- cs_main]
        need os_main
        mapM putInfo os_main

        --cmd_ "gcc -o" [out] os_main
        --cmd_ "gcc -o -Llib -lbuild3a" [out] os_main
        cmd_ "gcc -o -Lsrc3b/lib -lbuild3a" [out] os_main

  buildDir </> srcDir </>  "main.o" %> \out -> do
        putInfo $ "buildDir/main.o: " ++ show out
        let c = dropDirectory1 $ out -<.> "c"
        let m = out -<.> "m"
        cmd_ "gcc -c" [c] "-o" [out] "-MMD -MF" [m]
        --cmd_ "gcc -c" [c] "-Lsrc3b/lib -lbuild3a -o" [out] "-MMD -MF" [m]
        neededMakefileDependencies m

  {-I added this as shake complained about not having a rule for libbuild3a.o
    I tried to deconstruct the library useing `ar`. Got rid of the no rule failure,
    but did not help in finding add.h in main.c-}     
  buildDir </> "src3b" </> "lib" </> "libbuild3a.o" %> \out -> do
        putInfo $ "buildDir/lib/libbuild3a.o: " ++ show out
        let c = dropDirectory1 $ out

        libs <- getDirectoryFiles "" [srcDir </> "lib" </> "libbuild3a.a"]
        need libs
        --cmd_ "ar rcs" [out] os_tq84
        --[cmd_ "ar -xv l add.o answer.o" [out] l | l <- libs]
        cmd_ "ar -xv libbuild3a.a add.o answer.o" [out] libs

这里是 main.c 试图使用它。这将是 exe。

#include <stdio.h>
#include <add.h>
#include <answer.h>

int main(int argc, char* argv[]) {

  setSummand(5);

  printf("5 + 7 = %d\n", add(7));

  printf("And the answer is: %d\n", answer());

  return 0;
}

只使用源文件而不用 .a 库会更好吗?或者我们 ar 将库分解成它的 .o 文件?我在最后一条规则中尝试过,但没有效果。

【问题讨论】:

标签: haskell shake


【解决方案1】:

我通过链接静态库和 ghc 选项解决了这个问题。

现在的问题是 main_exe 看不到 libsupply.a 文件中的更改。我需要弄清楚如何对它提出需求,即使是创建库的调用

cmd_ "ar rcs" [out] co

是规则中的最后一个表达式。

这是新的来源:

build4 :: IO ()
build4 = shakeArgs shakeOptions{shakeFiles="build4"} $ do
  let buildDir = "build4"
      srcDir = "src4"
  want [srcDir </>  "libsupply.a"]
  want [buildDir </> "main_exe" <.> exe]

  buildDir </> "main_exe" <.> exe %> \out -> do

      cmd_
          "ghc"
          ("src4" </> "main.hs")
          "-isrc"
          "-Lsrc4"
          "-lsupply"
          "-outputdir"
          "build4"
          "-o"
          out


  srcDir </> "libsupply.a" %> \out -> do
    putInfo $ "in build4/libsupply.a: " ++ ( show out)
    cs <- getDirectoryFiles "" [srcDir </> "*.c"]
    need  cs

    let co = [buildDir </> c -<.> "o" | c <- cs]
    need co

    cmd_ "ar rcs" [out] co


  --build the .c files required for the library
  buildDir </> "//*.o" %> \out -> do
    putInfo $ "in build_dir </> //*.o: " ++ ( show out)
    let c = dropDirectory1 $ out -<.> "c"
    let m = out -<.> "m"
    cmd_ "gcc -c" [c] "-o" [out] "-MMD -MF" [m]
    neededMakefileDependencies m

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多