如何从build.zig运行'zig cc'?

How to run `zig cc` from build.zig?

提问人:MKaama 提问时间:11/10/2023 最后编辑:Anton KorobeynikovMKaama 更新时间:11/10/2023 访问量:62

问:

动机:我想在同一个项目中使用 Tcl 和 zig。zig 是一个多功能的编译器,Tcl 具有非常紧凑且易于理解的语法。第一步是使用 zig 编译 Tcl C 源代码。代

cc32 = zig cc

在 makefile.vc 和 rules.vc 中,我从中获取所有对象 (*.obj) 文件

nmake -f makefile.vc OPTS=static DISABLE_IMPLICIT_RULES=1

然后我可以链接到

zig cc -IC:\tcl8.7a5\win\ -DSTATIC_BUILD -IC:\tcl8.7a5\generic\ C:\tcl8.7a5\win\tclAppInit.c     tclAlloc.obj tclAssembly.obj tclAsync.obj tclBasic.obj tclBinary.obj tclCkalloc.obj tclClock.obj tclCmdAH.obj tclCmdIL.obj tclCmdMZ.obj tclCompCmds.obj tclCompCmdsGR.obj tclCompCmdsSZ.obj tclCompExpr.obj tclCompile.obj tclConfig.obj tclDate.obj tclDictObj.obj tclDisassemble.obj tclEncoding.obj tclEnsemble.obj tclEnv.obj tclEvent.obj tclExecute.obj tclFCmd.obj tclFileName.obj tclGet.obj tclHash.obj tclHistory.obj tclIndexObj.obj tclInterp.obj tclIO.obj tclIOCmd.obj tclIOGT.obj tclIORChan.obj tclIORTrans.obj tclIOSock.obj tclIOUtil.obj tclLink.obj tclListObj.obj tclLiteral.obj tclLoad.obj tclMain.obj tclMainW.obj tclNamesp.obj tclNotify.obj tclObj.obj tclOO.obj tclOOBasic.obj tclOOCall.obj tclOODefineCmds.obj tclOOInfo.obj tclOOMethod.obj tclOOStubInit.obj tclOOStubLib.obj tclOptimize.obj tclPanic.obj tclParse.obj tclPathObj.obj tclPipe.obj tclPkg.obj tclPkgConfig.obj tclPosixStr.obj tclPreserve.obj tclProc.obj tclProcess.obj tclRegexp.obj tclResolve.obj tclResult.obj tclScan.obj tclStringObj.obj tclStrToD.obj tclStubInit.obj tclStubLib.obj tclThread.obj tclThreadAlloc.obj tclThreadJoin.obj tclThreadStorage.obj tclTimer.obj tclTomMathInterface.obj tclTomMathStubLib.obj tclTrace.obj tclUtf.obj tclUtil.obj tclVar.obj tclWin32Dll.obj tclWinChan.obj tclWinConsole.obj tclWinDde.obj tclWinError.obj tclWinFCmd.obj tclWinFile.obj tclWinInit.obj tclWinLoad.obj tclWinNotify.obj tclWinPanic.obj tclWinPipe.obj tclWinReg.obj tclWinSerial.obj tclWinSock.obj tclWinThrd.obj tclWinTime.obj tclZipfs.obj tclZlib.obj adler32.obj deflate.obj inflate.obj zutil.obj inffast.obj infback.obj inftrees.obj trees.obj uncompr.obj crc32.obj -L"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x64" ws2_32.lib bn_mp_add.obj bn_mp_add_d.obj bn_mp_and.obj bn_mp_clamp.obj bn_mp_clear.obj bn_mp_clear_multi.obj bn_mp_cmp.obj bn_mp_cmp_d.obj bn_mp_cmp_mag.obj bn_mp_cnt_lsb.obj bn_mp_copy.obj bn_mp_count_bits.obj bn_mp_div.obj bn_mp_div_2.obj bn_mp_div_2d.obj bn_mp_div_3.obj bn_mp_div_d.obj bn_mp_exch.obj bn_mp_expt_u32.obj bn_mp_get_mag_u64.obj bn_mp_grow.obj bn_mp_init.obj bn_mp_init_copy.obj bn_mp_init_i64.obj bn_mp_init_multi.obj bn_mp_init_set.obj bn_mp_init_size.obj bn_mp_init_u64.obj bn_mp_lshd.obj bn_mp_mod.obj bn_mp_mod_2d.obj bn_mp_mul.obj bn_mp_mul_2.obj bn_mp_mul_2d.obj bn_mp_mul_d.obj bn_mp_neg.obj bn_mp_or.obj bn_mp_radix_size.obj bn_mp_radix_smap.obj bn_mp_read_radix.obj bn_mp_rshd.obj bn_mp_set_i64.obj bn_mp_set_u64.obj bn_mp_shrink.obj bn_mp_signed_rsh.obj bn_mp_sqr.obj bn_mp_sqrt.obj bn_mp_sub.obj bn_mp_sub_d.obj bn_mp_to_radix.obj bn_mp_to_ubin.obj bn_mp_ubin_size.obj bn_mp_unpack.obj bn_mp_xor.obj bn_mp_zero.obj bn_s_mp_add.obj bn_s_mp_balance_mul.obj bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj bn_s_mp_mul_digs.obj bn_s_mp_mul_digs_fast.obj bn_s_mp_reverse.obj bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj netapi32.lib regcomp.obj regerror.obj regexec.obj regfree.obj userenv.lib mysqltcl.obj -L. -llibmaria.dll -ozigtcl.exe

这会产生一个正常工作的 zigtcl.exe

但这是一个半手动的过程。我想使用 build.zig 和标准的 zig 构建过程。我的build.zig是:zig build

const std = @import("std");

pub fn build(b: *std.Build) void {
const target=b.standardTargetOptions(.{});
const optimize=b.standardOptimizeOption(.{});
const exe=b.addExecutable(.{.name = "tclzig",.target=target,.optimize=optimize,});

const includes=[_][]const u8{"-IC:/tcl8.7a5/win","-IC:/tcl8.7a5/generic","-IC:/tcl8.7a5/libtommath"};
const cflags=[_][]const u8{""};
const prj_defines=[_][]const u8{"-DTCL_TOMMATH","-DMP_PREC=4","-Dinline=__inline","-DHAVE_ZLIB=1","-DMP_FIXED_CUTOFFS"};
const use_stubs_defs=[_][]const u8{""};
const optdefines=[_][]const u8{"-DSTDC_HEADERS","-DMP_NO_STDINT=1","-DSTATIC_BUILD","-DNDEBUG","-DTCL_CFG_OPTIMIZED","-DNO_STRTOI64=1"};
const appcflags=cflags++includes++prj_defines++optdefines++use_stubs_defs;
const pkgcflags=appcflags++[_][]const u8{"-DBUILD_tcl"};
const stubscflags=cflags++prj_defines++optdefines++includes++use_stubs_defs;

exe.linkLibC();
exe.addCSourceFile(.{.file=.{.path="../win/tclAppInit.c"},.flags=&(appcflags++[_][]const u8{"-DUNICODE","-D_UNICODE"})});
//tclstub87.lib:
exe.addCSourceFile(.{.file=.{.path="../generic/tclStubLib.c"},.flags=&stubscflags});
exe.addCSourceFile(.{.file=.{.path="../generic/tclTomMathStubLib.c"},.flags=&stubscflags});
exe.addCSourceFile(.{.file=.{.path="../generic/tclOOStubLib.c"},.flags=&stubscflags});
exe.addCSourceFile(.{.file=.{.path="../win/tclWinPanic.c"},.flags=&stubscflags});
//tcl87sx.lib:
exe.addCSourceFile(.{.file=.{.path="../generic/tclMain.c"},.flags=&pkgcflags++[_][]const u8{"-DUNICODE","-D_UNICODE"}});
exe.addCSourceFile(.{.file=.{.path="../generic/tclPkgConfig.c"},.flags=&pkgcflags++[_][]const u8{
    "-DCFG_INSTALL_LIBDIR=\"C:\\Tcl\\lib\"",
    "-DCFG_INSTALL_BINDIR=\"C:\\Tcl\\bin\"",
    "-DCFG_INSTALL_SCRDIR=\"C:\\Tcl\\lib\\tcl8.7\"",
    "-DCFG_INSTALL_INCDIR=\"C:\\Tcl\\include\"",
    "-DCFG_INSTALL_DOCDIR=\"C:\\Tcl\\doc\"",
    "-DCFG_RUNTIME_LIBDIR=\"C:\\Tcl\\lib\"",
    "-DCFG_RUNTIME_BINDIR=\"C:\\Tcl\\bin\"",
    "-DCFG_RUNTIME_SCRDIR=\"C:\\Tcl\\lib\\tcl8.7\"",
    "-DCFG_RUNTIME_INCDIR=\"C:\\Tcl\\include\"",
    "-DCFG_RUNTIME_DOCDIR=\"C:\\Tcl\\doc\"",
    "-DCFG_RUNTIME_DLLFILE=\"\"",
}});//tclPkgConfig.c
exe.addCSourceFile(.{.file=.{.path="../generic/tclZipfs.c"},.flags=&pkgcflags++[_][]const u8{"-IC:/vk/tcl8.7a5/compat/zlib","-IC:/vk/tcl8.7a5/compat/zlib/contrib/minizip"}});
exe.addCSourceFile(.{.file=.{.path="../generic/tclZlib.c"},.flags=&pkgcflags++[_][]const u8{"-IC:/vk/tcl8.7a5/compat/zlib"}});
exe.addCSourceFile(.{.file=.{.path="../win/tclWinReg.c"},.flags=&appcflags++[_][]const u8{"-DUSE_TCL_STUBS"}});
exe.addCSourceFile(.{.file=.{.path="../win/tclWinDde.c"},.flags=&appcflags++[_][]const u8{"-DUSE_TCL_STUBS"}});
const genericfiles=[_][]const u8{"../generic/regcomp.c","../generic/regerror.c","../generic/regexec.c","../generic/regfree.c","../generic/tclAlloc.c","../generic/tclAssembly.c","../generic/tclAsync.c","../generic/tclBasic.c","../generic/tclBinary.c","../generic/tclCkalloc.c","../generic/tclClock.c","../generic/tclCmdAH.c","../generic/tclCmdIL.c","../generic/tclCmdMZ.c","../generic/tclCompCmds.c","../generic/tclCompCmdsGR.c","../generic/tclCompCmdsSZ.c","../generic/tclCompExpr.c","../generic/tclCompile.c","../generic/tclConfig.c","../generic/tclDate.c","../generic/tclDictObj.c","../generic/tclDisassemble.c","../generic/tclEncoding.c","../generic/tclEnsemble.c","../generic/tclEnv.c","../generic/tclEvent.c","../generic/tclExecute.c","../generic/tclFCmd.c","../generic/tclFileName.c","../generic/tclGet.c","../generic/tclHash.c","../generic/tclHistory.c","../generic/tclIndexObj.c","../generic/tclInterp.c","../generic/tclIO.c","../generic/tclIOCmd.c","../generic/tclIOGT.c","../generic/tclIOSock.c","../generic/tclIOUtil.c","../generic/tclIORChan.c","../generic/tclIORTrans.c","../generic/tclLink.c","../generic/tclListObj.c","../generic/tclLiteral.c","../generic/tclLoad.c","../generic/tclMain.c","../generic/tclNamesp.c","../generic/tclNotify.c","../generic/tclOO.c","../generic/tclOOBasic.c","../generic/tclOOCall.c","../generic/tclOODefineCmds.c","../generic/tclOOInfo.c","../generic/tclOOMethod.c","../generic/tclOOStubInit.c","../generic/tclObj.c","../generic/tclOptimize.c","../generic/tclPanic.c","../generic/tclParse.c","../generic/tclPathObj.c","../generic/tclPipe.c","../generic/tclPkg.c","../generic/tclPosixStr.c","../generic/tclPreserve.c","../generic/tclProc.c","../generic/tclProcess.c","../generic/tclRegexp.c","../generic/tclResolve.c","../generic/tclResult.c","../generic/tclScan.c","../generic/tclStringObj.c","../generic/tclStrToD.c","../generic/tclStubInit.c","../generic/tclThread.c","../generic/tclThreadAlloc.c","../generic/tclThreadJoin.c","../generic/tclThreadStorage.c","../generic/tclTimer.c","../generic/tclTomMathInterface.c","../generic/tclTrace.c","../generic/tclUtf.c","../generic/tclUtil.c","../generic/tclVar.c"};
exe.addCSourceFiles(&genericfiles,&pkgcflags);//CCPKGCMD
const zlibfiles=[_][]const u8{"../compat/zlib/adler32.c","../compat/zlib/compress.c","../compat/zlib/crc32.c","../compat/zlib/deflate.c","../compat/zlib/infback.c","../compat/zlib/inffast.c","../compat/zlib/inflate.c","../compat/zlib/inftrees.c","../compat/zlib/trees.c","../compat/zlib/uncompr.c","../compat/zlib/zutil.c"};
exe.addCSourceFiles(&zlibfiles,&pkgcflags);
const tommathfiles=[_][]const u8{"../libtommath/bn_mp_add.c","../libtommath/bn_mp_add_d.c","../libtommath/bn_mp_and.c","../libtommath/bn_mp_clamp.c","../libtommath/bn_mp_clear.c","../libtommath/bn_mp_clear_multi.c","../libtommath/bn_mp_cmp.c","../libtommath/bn_mp_cmp_d.c","../libtommath/bn_mp_cmp_mag.c","../libtommath/bn_mp_cnt_lsb.c","../libtommath/bn_mp_copy.c","../libtommath/bn_mp_count_bits.c","../libtommath/bn_mp_div.c","../libtommath/bn_mp_div_d.c","../libtommath/bn_mp_div_2.c","../libtommath/bn_mp_div_2d.c","../libtommath/bn_mp_div_3.c","../libtommath/bn_mp_exch.c","../libtommath/bn_mp_expt_u32.c","../libtommath/bn_mp_get_mag_u64.c","../libtommath/bn_mp_grow.c","../libtommath/bn_mp_init.c","../libtommath/bn_mp_init_copy.c","../libtommath/bn_mp_init_i64.c","../libtommath/bn_mp_init_multi.c","../libtommath/bn_mp_init_set.c","../libtommath/bn_mp_init_size.c","../libtommath/bn_mp_init_u64.c","../libtommath/bn_mp_lshd.c","../libtommath/bn_mp_mod.c","../libtommath/bn_mp_mod_2d.c","../libtommath/bn_mp_mul.c","../libtommath/bn_mp_mul_2.c","../libtommath/bn_mp_mul_2d.c","../libtommath/bn_mp_mul_d.c","../libtommath/bn_mp_neg.c","../libtommath/bn_mp_or.c","../libtommath/bn_mp_radix_size.c","../libtommath/bn_mp_radix_smap.c","../libtommath/bn_mp_read_radix.c","../libtommath/bn_mp_rshd.c","../libtommath/bn_mp_set_i64.c","../libtommath/bn_mp_set_u64.c","../libtommath/bn_mp_shrink.c","../libtommath/bn_mp_sqr.c","../libtommath/bn_mp_sqrt.c","../libtommath/bn_mp_sub.c","../libtommath/bn_mp_sub_d.c","../libtommath/bn_mp_signed_rsh.c","../libtommath/bn_mp_to_ubin.c","../libtommath/bn_mp_to_radix.c","../libtommath/bn_mp_ubin_size.c","../libtommath/bn_mp_unpack.c","../libtommath/bn_mp_xor.c","../libtommath/bn_mp_zero.c","../libtommath/bn_s_mp_add.c","../libtommath/bn_s_mp_balance_mul.c","../libtommath/bn_s_mp_karatsuba_mul.c","../libtommath/bn_s_mp_karatsuba_sqr.c","../libtommath/bn_s_mp_mul_digs.c","../libtommath/bn_s_mp_mul_digs_fast.c","../libtommath/bn_s_mp_reverse.c","../libtommath/bn_s_mp_sqr.c","../libtommath/bn_s_mp_sqr_fast.c","../libtommath/bn_s_mp_sub.c","../libtommath/bn_s_mp_toom_sqr.c","../libtommath/bn_s_mp_toom_mul.c"};
exe.addCSourceFiles(&tommathfiles,&pkgcflags);
//const winfiles=[_][]const u8{"../win/tclWin32Dll.c","../win/tclWinChan.c","../win/tclWinConsole.c","../win/tclWinError.c","../win/tclWinFCmd.c","../win/tclWinFile.c","../win/tclWinInit.c","../win/tclWinLoad.c","../win/tclWinNotify.c","../win/tclWinPipe.c","../win/tclWinSerial.c","../win/tclWinSock.c","../win/tclWinThrd.c","../win/tclWinTime.c"};
const winfiles=[_][]const u8{"../win/tclWin32Dll.c","../win/tclWinChan.c","../win/tclWinConsole.c","../win/tclWinError.c","../win/tclWinFCmd.c","../win/tclWinFile.c","../win/tclWinInit.c","../win/tclWinLoad.c","../win/tclWinNotify.c","../win/tclWinPipe.c","../win/tclWinSerial.c","../win/tclWinThrd.c","../win/tclWinTime.c"};
exe.addCSourceFiles(&winfiles,&(pkgcflags++[_][]const u8{"-DHAVE_NO_SEH"}));
//const obj=b.addObject(.{.name="notbase64",.root_source_file=.{.path="../win/tclWinSock.c"},.optimize=optimize,.target=target});
//  exe.addObject(obj);
    exe.addObjectFile(.{.path="C:/tcl8.7a5/win/Releasezig_VC800/tcl_StaticX/tclWinSock.obj"});
    exe.linkSystemLibrary("netapi32");
    exe.linkSystemLibrary("userenv");
    exe.linkSystemLibrary("ws2_32");
    //exe.linkSystemLibrary("advapi32");
    b.installArtifact(exe);
}

它失败了

构建摘要:0/3 步成功;1 失败(使用 --summary 禁用 无)
安装 transitive failure └─ install tclzig transitive failure

└─ zig build-exe tclzig Debug native 2 errors
error: lld-link: undefined symbol: in6addr_any
note: referenced by C:\tcl8.7a5\win\Releasezig_VC800\tcl_StaticX\tclWinSock.obj:(.refptr.in6addr_any) 错误:lld-link:未定义的符号:Win主
注释:由 C:\WinProg\zig\lib\libc\mingw\crt\crt0_c.c:18
注释引用:mingw32.lib(crt0_c.obj):(main)

问题出在 tclWinSock.c 或 tclWinSock.obj 上,尽管完全相同的对象在第一个构建过程中链接正常。为了进行比较,Microsoft产生了一个tclWinSock.obj,该导入了 strightforward ,在 ws2_32.dll 中找到(如图所示)。zig 生成的对象文件以更复杂的方式导入,但与 .它不适用于 (链接) ,这发生在我的 build.zig 和 .我可以强制 zig 从 build.zig 发出 a 吗?cl_in6addr_anydumpbin /symbols tclWinSock.objin6addr_anyzig cczig build-exezig build --verbosezig cc

我的版本是 Windows10,zig 0.12.0-dev.790+ad6f8e3a5,tcl87a5-src.zip

构建 TCL Zig

评论

2赞 Donal Fellows 11/10/2023
如果您只需要为一个更大的项目工作,请将 Tcl 构建为一个动态库,并与之链接。否则。。。这需要一个了解 Zig 构建系统的人,而那个人不是我!cl

答: 暂无答案