【发布时间】: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 文件?我在最后一条规则中尝试过,但没有效果。
【问题讨论】:
-
我投票结束这个问题,因为它看起来应该迁移到代码审查
-
对谁来说“更好”?代码的最终目的是什么?
-
@MichaelLitchard 这可能是 CR 的主题,但请不要以此作为 VTC 的理由。而是将其标记为主持人干预并解释它是如何在那里成为主题的。请参阅Does being on-topic at another Stack Exchange site automatically make a question off-topic for Stack Overflow?、Migration of code questions from Stack Overflow to Code Review 和 this answer to A guide to Code Review for Stack Overflow users
-
不知道题外话、CR 和 VTC 是什么意思。只是它需要以不同的方式标记吗?我正在使用 haskell 摇动构建模块。代码的重点是学习使用shake构建haskell项目,该项目还生成和使用静态c库。
-
VTC 撤回..