I would like to use "zig cc" for easily cross compiling cgo dependent go code, however the "drop-in" c compiler replacement doesn't work.
main.go
package main
//int Add(int a, int b){
// return a+b;
//}
import "C"
import "fmt"
func main() {
a := C.int(10)
b := C.int(20)
c := C.Add(a, b)
fmt.Println(c) // 30
}
go build main.go //build works fine (uses gcc)
CC="clang" go build main.go //build works fine (uses clang)
$ CC="zig cc" go build main.go
# runtime/cgo
info: Usage: zig [command] [options]
Commands:
build Build project from build.zig
build-exe Create executable from source or object files
build-lib Create library from source or object files
build-obj Create object from source or assembly
cc Use Zig as a drop-in C compiler
c++ Use Zig as a drop-in C++ compiler
env Print lib path, std path, compiler id and version
fmt Parse file and render in canonical zig format
init-exe Initialize a `zig build` application in the cwd
init-lib Initialize a `zig build` library in the cwd
libc Display native libc paths file or validate one
run Create executable and run immediately
translate-c Convert C code to Zig code
targets List available compilation targets
test Create and run a test build
version Print version number and exit
zen Print zen of zig and exit
General Options:
--help Print command-specific usage
error: unknown command: -E
go version go1.15.5 linux/amd64
So this looks like go is inserting flags before it passes cc. This will either need to be fixed in go, or you will have to work around this by creating a shell script like this:
#!/bin/sh
zig cc $@
And then putting that as your CC. This way zig gets invoked with zig cc -E ... instead of zig -E ...
Downstream issue filed: https://github.com/golang/go/issues/43078
#!/bin/sh zig cc $@
This indeed works, but another weird thing is, that I get these errors:
$ CC="zig-wrap" go build main.go
# runtime/cgo
error: unable to create compilation: AccessDenied
# runtime/cgo
gcc_context.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_fatalf.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_libinit.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_linux_amd64.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_mmap.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_setenv.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_sigaction.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_traceback.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_util.c:1:1: error: unable to build C object: AccessDenied
# runtime/cgo
gcc_amd64.S:1:1: error: unable to build C object: AccessDenied
Unless I use sudo, which produces a working binary:
$ sudo CC="zig-wrap" go build main.go
# command-line-arguments
warning: unsupported linker arg: --compress-debug-sections=zlib-gnu
That error message would be more useful if it printed the path, sorry about that. Anyway that's a separate issue. Could you try using strace to find out which file paths are getting AccessDenied?
My hunch is that it is the global cache, and go is running the compiler within a chroot or something. You can try messing with these options within your wrapper script:
--cache-dir [path] Override the local cache directory
--global-cache-dir [path] Override the global cache directory
Make sure they are writable paths. They are allowed to be the same.
strace example of what went wrong (I think, never used strace before)
59864 openat(AT_FDCWD, "zig-cache", O_RDONLY|O_CLOEXEC|O_PATH|O_DIRECTORY) = 5
...
59864 openat(5, "h", O_RDONLY|O_CLOEXEC|O_PATH|O_DIRECTORY) = 6
...
59864 openat(6, "a1156d94bd5502d8ca9db1c6a76c576f.txt", O_RDWR|O_CREAT|O_CLOEXEC, 0666) = -1 EACCES (Permission denied)
My hunch is that it is the global cache, and go is running the compiler within a chroot or something. You can try messing with these options within your wrapper script:
--cache-dir [path] Override the local cache directory --global-cache-dir [path] Override the global cache directory
My zig cc doesn't have those options:
error: Unknown Clang option: '--cache-dir=
Thanks, that strace output is exactly what I needed to see. This is a bit confusing - zig successfully was able to open zig-cache/o/ but then when it tried to create zig-cache/o/a1156d94bd5502d8ca9db1c6a76c576f.txt it got EACCES. When you ran the strace output, I think you might have had leftover cache artifacts from when you ran the command with sudo (never use sudo to work around an EACCES problem, you will only create more problems for yourself). Can you try this again with all the root-owned file system stuff deleted?
My
zig ccdoesn't have those options:
Ah, sorry about that. I forgot this was the zig cc CLI not the regular zig CLI. I'll need to introduce environment variables to control those paths when using zig cc.
I'll look into this myself and see if I can get it working. Thanks for your help.
Can you try this again with all the root-owned file system stuff deleted?
Then it's just:
62606 mkdirat(AT_FDCWD, "zig-cache", 0755 = -1 EACCES (Permission denied)
This is because the permissions of /usr/lib/go/src/runtime/cgo are restricted to root access:
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Thanks - I'll look into a solution for this. I'm not sure why Go is preventing Zig from using a project-local directory as a cache folder. That seems like a reasonable thing for a compiler to do. But I'll add support for configuring the cache directory via an environment variable so that it can be modified when using zig cc.
It's not because of a project local cache folder, it's because zig tries to create a directory inside a stdlib package.
I think zig cc should put all caches inside --global-cache-dir.
After 2759313263d58dba324788dbe346ed1506b239dc (which I just pushed to master) and the following zcc script:
#!/bin/sh
ZIG_LOCAL_CACHE_DIR="$HOME/misc/gozig" zig cc $@
(where $HOME/misc/gozig is my project path)
It produced a viable binary.
[nix-shell:~/misc/gozig]$ ./main
30
It gave the following harmless linker warning:
warning: unsupported linker arg: --compress-debug-sections=zlib-gnu
You could also try passing /tmp or any other writable directory for that value.
I think zig cc should put all caches inside
--global-cache-dir.
I reviewed the logic for choosing a local cache directory, and I do think there is room to modify it when only compiling C objects:
https://github.com/ziglang/zig/blob/2759313263d58dba324788dbe346ed1506b239dc/src/main.zig#L1641-L1668
You can see that in some cases, for example zig run, we do select the global cache directory as the local one. Clearly in this case with cgo, zig is not making an effective choice for the local cache directory.
https://github.com/ziglang/zig/wiki/Building-Zig-From-Source#option-a-use-your-system-installed-build-tools
I tried to compile zig from the current master source code, but I got the following errors, I followed the above procedure:
Logs:
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_cc1_main.cpp.o): warning: relocation against `_ZTVN5clang28ObjectFilePCHContainerReaderE' in read-only section `.text._ZN5clang28ObjectFilePCHContainerReaderC2Ev[_ZN5clang28ObjectFilePCHContainerReaderC5Ev]'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangSourceManager_getFilename':
/home/luap/zig/src/zig_clang.cpp:1636: undefined reference to `clang::SourceManager::getFilename(clang::SourceLocation) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangSourceManager_getSpellingLineNumber':
/home/luap/zig/src/zig_clang.cpp:1643: undefined reference to `clang::SourceManager::getSpellingLineNumber(clang::SourceLocation, bool*) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangSourceManager_getSpellingColumnNumber':
/home/luap/zig/src/zig_clang.cpp:1649: undefined reference to `clang::SourceManager::getSpellingColumnNumber(clang::SourceLocation, bool*) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangSourceManager_getCharacterData':
/home/luap/zig/src/zig_clang.cpp:1655: undefined reference to `clang::SourceManager::getCharacterData(clang::SourceLocation, bool*) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangASTContext_getPointerType':
/home/luap/zig/src/zig_clang.cpp:1659: undefined reference to `clang::ASTContext::getPointerType(clang::QualType) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangASTUnit_visitLocalTopLevelDecls':
/home/luap/zig/src/zig_clang.cpp:1679: undefined reference to `clang::ASTUnit::visitLocalTopLevelDecls(void*, bool (*)(void*, clang::Decl const*))'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangASTUnit_getLocalPreprocessingEntities_begin':
/home/luap/zig/src/zig_clang.cpp:1687: undefined reference to `clang::ASTUnit::getLocalPreprocessingEntities() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangASTUnit_getLocalPreprocessingEntities_end':
/home/luap/zig/src/zig_clang.cpp:1694: undefined reference to `clang::ASTUnit::getLocalPreprocessingEntities() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangVarDecl_getAlignedAttribute':
/home/luap/zig/src/zig_clang.cpp:1767: undefined reference to `clang::AlignedAttr::getAlignment(clang::ASTContext&) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangFieldDecl_getAlignedAttribute':
/home/luap/zig/src/zig_clang.cpp:1777: undefined reference to `clang::AlignedAttr::getAlignment(clang::ASTContext&) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangFunctionDecl_getAlignedAttribute':
/home/luap/zig/src/zig_clang.cpp:1787: undefined reference to `clang::AlignedAttr::getAlignment(clang::ASTContext&) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangParmVarDecl_getOriginalType':
/home/luap/zig/src/zig_clang.cpp:1794: undefined reference to `clang::ParmVarDecl::getOriginalType() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangDecl_getDeclKindName':
/home/luap/zig/src/zig_clang.cpp:1839: undefined reference to `clang::Decl::getDeclKindName() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangFunctionDecl_doesDeclarationForceExternallyVisibleDefinition':
/home/luap/zig/src/zig_clang.cpp:1908: undefined reference to `clang::FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangType_getPointeeType':
/home/luap/zig/src/zig_clang.cpp:2026: undefined reference to `clang::Type::getPointeeType() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangType_getTypeClassName':
/home/luap/zig/src/zig_clang.cpp:2076: undefined reference to `clang::Type::getTypeClassName() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangType_getAsRecordType':
/home/luap/zig/src/zig_clang.cpp:2087: undefined reference to `clang::Type::getAsStructureType() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangType_getAsUnionType':
/home/luap/zig/src/zig_clang.cpp:2093: undefined reference to `clang::Type::getAsUnionType() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangStmt_getBeginLoc':
/home/luap/zig/src/zig_clang.cpp:2099: undefined reference to `clang::Stmt::getBeginLoc() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangExpr_getBeginLoc':
/home/luap/zig/src/zig_clang.cpp:2124: undefined reference to `clang::Stmt::getBeginLoc() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangExpr_EvaluateAsBooleanCondition':
/home/luap/zig/src/zig_clang.cpp:2132: undefined reference to `clang::Expr::EvaluateAsBooleanCondition(bool&, clang::ASTContext const&, bool) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangExpr_EvaluateAsFloat':
/home/luap/zig/src/zig_clang.cpp:2142: undefined reference to `clang::Expr::EvaluateAsFloat(llvm::APFloat&, clang::ASTContext const&, clang::Expr::SideEffectsKind, bool) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangExpr_EvaluateAsConstantExpr':
/home/luap/zig/src/zig_clang.cpp:2151: undefined reference to `clang::Expr::EvaluateAsConstantExpr(clang::Expr::EvalResult&, clang::Expr::ConstExprUsage, clang::ASTContext const&, bool) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangAPValue_getLValueBase':
/home/luap/zig/src/zig_clang.cpp:2260: undefined reference to `clang::APValue::getLValueBase() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangLoadFromCommandLine':
/home/luap/zig/src/zig_clang.cpp:2267: undefined reference to `clang::CompilerInstance::createDiagnostics(clang::DiagnosticOptions*, clang::DiagnosticConsumer*, bool, clang::CodeGenOptions const*)'
/usr/bin/ld: /home/luap/zig/src/zig_clang.cpp:2278: undefined reference to `clang::ASTUnit::LoadFromCommandLine(char const**, char const**, std::shared_ptr<clang::PCHContainerOperations>, llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>, llvm::StringRef, bool, clang::CaptureDiagsKind, llvm::ArrayRef<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, llvm::MemoryBuffer*> >, bool, unsigned int, clang::TranslationUnitKind, bool, bool, bool, clang::SkipFunctionBodiesScope, bool, bool, bool, bool, llvm::Optional<llvm::StringRef>, std::unique_ptr<clang::ASTUnit, std::default_delete<clang::ASTUnit> >*, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>)'
/usr/bin/ld: /home/luap/zig/src/zig_clang.cpp:2326: undefined reference to `clang::FullSourceLoc::getFileLoc() const'
/usr/bin/ld: /home/luap/zig/src/zig_clang.cpp:2334: undefined reference to `clang::SourceManager::getPresumedLoc(clang::SourceLocation, bool) const'
/usr/bin/ld: /home/luap/zig/src/zig_clang.cpp:2347: undefined reference to `clang::FullSourceLoc::getBufferData(bool*) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangASTUnit_delete':
/home/luap/zig/src/zig_clang.cpp:2369: undefined reference to `clang::ASTUnit::~ASTUnit()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangVarDecl_getTLSKind':
/home/luap/zig/src/zig_clang.cpp:2389: undefined reference to `clang::VarDecl::getTLSKind() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangVarDecl_hasInit':
/home/luap/zig/src/zig_clang.cpp:2409: undefined reference to `clang::VarDecl::hasInit() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangVarDecl_evaluateValue':
/home/luap/zig/src/zig_clang.cpp:2414: undefined reference to `clang::VarDecl::evaluateValue() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangFloatingLiteral_getValueAsApproximateDouble':
/home/luap/zig/src/zig_clang.cpp:2499: undefined reference to `clang::FloatingLiteral::getValueAsApproximateDouble() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangMacroQualifiedType_getModifiedType':
/home/luap/zig/src/zig_clang.cpp:2579: undefined reference to `clang::MacroQualifiedType::getModifiedType() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangIntegerLiteral_EvaluateAsInt':
/home/luap/zig/src/zig_clang.cpp:2611: undefined reference to `clang::Expr::EvaluateAsInt(clang::Expr::EvalResult&, clang::ASTContext const&, clang::Expr::SideEffectsKind, bool) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangIntegerLiteral_isZero':
/home/luap/zig/src/zig_clang.cpp:2627: undefined reference to `clang::Expr::EvaluateAsInt(clang::Expr::EvalResult&, clang::ASTContext const&, clang::Expr::SideEffectsKind, bool) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangRecordDecl_field_begin':
/home/luap/zig/src/zig_clang.cpp:2954: undefined reference to `clang::RecordDecl::field_begin() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `ZigClangFieldDecl_isAnonymousStructOrUnion':
/home/luap/zig/src/zig_clang.cpp:2968: undefined reference to `clang::FieldDecl::isAnonymousStructOrUnion() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::RecordType::getDecl() const':
/usr/include/clang/AST/Type.h:4628: undefined reference to `clang::TagType::getDecl() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::EnumType::getDecl() const':
/usr/include/clang/AST/Type.h:4651: undefined reference to `clang::TagType::getDecl() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::QualType::getUnqualifiedType() const':
/usr/include/clang/AST/Type.h:6486: undefined reference to `clang::QualType::getSplitUnqualifiedTypeImpl(clang::QualType)'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::Type::getAsArrayTypeUnsafe() const':
/usr/include/clang/AST/Type.h:7215: undefined reference to `clang::Type::getUnqualifiedDesugaredType() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::APValue::~APValue()':
/usr/include/clang/AST/APValue.h:342: undefined reference to `clang::APValue::DestroyDataAndMakeUninit()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::DeclContext::getRedeclContext() const':
/usr/include/clang/AST/DeclBase.h:1972: undefined reference to `clang::DeclContext::getRedeclContext()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::VarDecl::getInit() const':
/usr/include/clang/AST/Decl.h:1230: undefined reference to `clang::VarDecl::getInit()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::FunctionDecl::isDefined() const':
/usr/include/clang/AST/Decl.h:2035: undefined reference to `clang::FunctionDecl::isDefined(clang::FunctionDecl const*&) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::FunctionDecl::getDefinition()':
/usr/include/clang/AST/Decl.h:2041: undefined reference to `clang::FunctionDecl::isDefined(clang::FunctionDecl const*&) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::FunctionDecl::isThisDeclarationADefinition() const':
/usr/include/clang/AST/Decl.h:2074: undefined reference to `clang::Decl::hasDefiningAttr() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::FunctionDecl::getParamDecl(unsigned int) const':
/usr/include/clang/AST/Decl.h:2426: undefined reference to `clang::FunctionDecl::getNumParams() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::EnumDecl::getDefinition() const':
/usr/include/clang/AST/Decl.h:3605: undefined reference to `clang::TagDecl::getDefinition() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::EnumDecl::enumerator_begin() const':
/usr/include/clang/AST/Decl.h:3638: undefined reference to `clang::DeclContext::decls_begin() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::RecordDecl::getDefinition() const':
/usr/include/clang/AST/Decl.h:3976: undefined reference to `clang::TagDecl::getDefinition() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::ASTContext::getTypeInfo(clang::QualType) const':
/usr/include/clang/AST/ASTContext.h:2070: undefined reference to `clang::ASTContext::getTypeInfo(clang::Type const*) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::ASTContext::getAsConstantArrayType(clang::QualType) const':
/usr/include/clang/AST/ASTContext.h:2437: undefined reference to `clang::ASTContext::getAsArrayType(clang::QualType) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::SourceManager::getFileID(clang::SourceLocation) const':
/usr/include/clang/Basic/SourceManager.h:1068: undefined reference to `clang::SourceManager::getFileIDSlow(unsigned int) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::SourceManager::getSpellingLoc(clang::SourceLocation) const':
/usr/include/clang/Basic/SourceManager.h:1178: undefined reference to `clang::SourceManager::getSpellingLocSlowCase(clang::SourceLocation) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::SourceManager::getLoadedSLocEntry(unsigned int, bool*) const':
/usr/include/clang/Basic/SourceManager.h:1662: undefined reference to `clang::SourceManager::loadSLocEntry(unsigned int, bool*) const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::PreprocessingRecord::iterator::operator*() const':
/usr/include/clang/Lex/PreprocessingRecord.h:449: undefined reference to `clang::PreprocessingRecord::getPreprocessedEntity(clang::PreprocessingRecord::PPEntityID)'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::UnaryOperator::getBeginLoc() const':
/usr/include/clang/AST/Expr.h:2256: undefined reference to `clang::Stmt::getBeginLoc() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::ImplicitCastExpr::getBeginLoc() const':
/usr/include/clang/AST/Expr.h:3517: undefined reference to `clang::Stmt::getBeginLoc() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::BinaryOperator::getBeginLoc() const':
/usr/include/clang/AST/Expr.h:3699: undefined reference to `clang::Stmt::getBeginLoc() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::Expr::EvalResult::EvalResult(clang::Expr::EvalResult const&)':
/usr/include/clang/AST/Expr.h:606: undefined reference to `clang::APValue::APValue(clang::APValue const&)'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::Mergeable<clang::FieldDecl>::getFirstDecl() const':
/usr/include/clang/AST/Redeclarable.h:331: undefined reference to `clang::getPrimaryMergedDecl(clang::Decl*)'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::SectionAttr* clang::Decl::getAttr<clang::SectionAttr>() const':
/usr/include/clang/AST/DeclBase.h:544: undefined reference to `clang::Decl::getAttrs() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::PackedAttr* clang::Decl::getAttr<clang::PackedAttr>() const':
/usr/include/clang/AST/DeclBase.h:544: undefined reference to `clang::Decl::getAttrs() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `clang::AlignedAttr* clang::Decl::getAttr<clang::AlignedAttr>() const':
/usr/include/clang/AST/DeclBase.h:544: undefined reference to `clang::Decl::getAttrs() const'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `std::default_delete<clang::ASTUnit>::operator()(clang::ASTUnit*) const':
/usr/include/c++/10.2.0/bits/unique_ptr.h:85: undefined reference to `clang::ASTUnit::~ASTUnit()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `llvm::RefCountedBase<clang::DiagnosticsEngine>::Release() const':
/usr/include/llvm/ADT/IntrusiveRefCntPtr.h:82: undefined reference to `clang::DiagnosticsEngine::~DiagnosticsEngine()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang.cpp.o): in function `void __gnu_cxx::new_allocator<clang::PCHContainerOperations>::construct<clang::PCHContainerOperations>(clang::PCHContainerOperations*)':
/usr/include/c++/10.2.0/ext/new_allocator.h:150: undefined reference to `clang::PCHContainerOperations::PCHContainerOperations()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_driver.cpp.o): in function `CreateAndPopulateDiagOpts(llvm::ArrayRef<char const*>, bool&)':
/home/luap/zig/src/zig_clang_driver.cpp:277: undefined reference to `clang::driver::getDriverOptTable()'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:282: undefined reference to `clang::ParseDiagnosticArgs(clang::DiagnosticOptions&, llvm::opt::ArgList&, clang::DiagnosticsEngine*, bool)'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_driver.cpp.o): in function `ZigClang_main':
/home/luap/zig/src/zig_clang_driver.cpp:340: undefined reference to `clang::noteBottomOfStack()'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:352: undefined reference to `clang::driver::ToolChain::getTargetAndModeFromProgramName(llvm::StringRef)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:463: undefined reference to `clang::TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream&, clang::DiagnosticOptions*, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:466: undefined reference to `clang::DiagnosticIDs::DiagnosticIDs()'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:468: undefined reference to `clang::DiagnosticsEngine::DiagnosticsEngine(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs>, llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions>, clang::DiagnosticConsumer*, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:473: undefined reference to `clang::serialized_diags::create(llvm::StringRef, clang::DiagnosticOptions*, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:474: undefined reference to `clang::DiagnosticsEngine::setClient(clang::DiagnosticConsumer*, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:478: undefined reference to `clang::ProcessWarningOptions(clang::DiagnosticsEngine&, clang::DiagnosticOptions const&, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:480: undefined reference to `clang::driver::Driver::Driver(llvm::StringRef, llvm::StringRef, clang::DiagnosticsEngine&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:494: undefined reference to `clang::driver::Driver::BuildCompilation(llvm::ArrayRef<char const*>)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:499: undefined reference to `clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:533: undefined reference to `clang::driver::Driver::generateCompilationDiagnostics(clang::driver::Compilation&, clang::driver::Command const&, llvm::StringRef, clang::driver::Driver::CompilationDiagnosticReport*)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_driver.cpp:468: undefined reference to `clang::DiagnosticsEngine::~DiagnosticsEngine()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_driver.cpp.o): in function `clang::DiagnosticBuilder::Emit()':
/usr/include/clang/Basic/Diagnostic.h:1121: undefined reference to `clang::DiagnosticsEngine::EmitCurrentDiagnostic(bool)'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_driver.cpp.o): in function `clang::DiagnosticConsumer::DiagnosticConsumer()':
/usr/include/clang/Basic/Diagnostic.h:1532: undefined reference to `vtable for clang::DiagnosticConsumer'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_driver.cpp.o): in function `clang::ChainedDiagnosticConsumer::ChainedDiagnosticConsumer(std::unique_ptr<clang::DiagnosticConsumer, std::default_delete<clang::DiagnosticConsumer> >, std::unique_ptr<clang::DiagnosticConsumer, std::default_delete<clang::DiagnosticConsumer> >)':
/usr/include/clang/Frontend/ChainedDiagnosticConsumer.h:32: undefined reference to `vtable for clang::ChainedDiagnosticConsumer'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_driver.cpp.o): in function `std::default_delete<clang::driver::Compilation>::operator()(clang::driver::Compilation*) const':
/usr/include/c++/10.2.0/bits/unique_ptr.h:85: undefined reference to `clang::driver::Compilation::~Compilation()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_driver.cpp.o): in function `llvm::RefCountedBase<clang::DiagnosticIDs>::Release() const':
/usr/include/llvm/ADT/IntrusiveRefCntPtr.h:82: undefined reference to `clang::DiagnosticIDs::~DiagnosticIDs()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_cc1_main.cpp.o): in function `cc1_main(llvm::ArrayRef<char const*>, char const*, void*)':
/home/luap/zig/src/zig_clang_cc1_main.cpp:187: undefined reference to `clang::CompilerInstance::CompilerInstance(std::shared_ptr<clang::PCHContainerOperations>, clang::InMemoryModuleCache*)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:188: undefined reference to `clang::DiagnosticIDs::DiagnosticIDs()'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:205: undefined reference to `clang::DiagnosticsEngine::DiagnosticsEngine(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs>, llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions>, clang::DiagnosticConsumer*, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:206: undefined reference to `clang::CompilerInvocation::CreateFromArgs(clang::CompilerInvocation&, llvm::ArrayRef<char const*>, clang::DiagnosticsEngine&, char const*)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:221: undefined reference to `clang::CompilerInvocation::GetResourcesPath[abi:cxx11](char const*, void*)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:224: undefined reference to `clang::CompilerInstance::createDiagnostics(clang::DiagnosticConsumer*, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:233: undefined reference to `clang::TextDiagnosticBuffer::FlushDiagnostics(clang::DiagnosticsEngine&) const'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:240: undefined reference to `clang::ExecuteCompilerInvocation(clang::CompilerInstance*)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:256: undefined reference to `clang::CompilerInstance::createOutputFile(llvm::StringRef, bool, bool, llvm::StringRef, llvm::StringRef, bool, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:262: undefined reference to `clang::CompilerInstance::clearOutputFiles(bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1_main.cpp:205: undefined reference to `clang::DiagnosticsEngine::~DiagnosticsEngine()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_cc1_main.cpp.o): in function `clang::TextDiagnosticBuffer::TextDiagnosticBuffer()':
/usr/include/clang/Frontend/TextDiagnosticBuffer.h:25: undefined reference to `vtable for clang::TextDiagnosticBuffer'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_cc1_main.cpp.o): in function `clang::ObjectFilePCHContainerWriter::ObjectFilePCHContainerWriter()':
/usr/include/clang/CodeGen/ObjectFilePCHContainerOperations.h:18: undefined reference to `vtable for clang::ObjectFilePCHContainerWriter'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_cc1_main.cpp.o): in function `clang::ObjectFilePCHContainerReader::ObjectFilePCHContainerReader()':
/usr/include/clang/CodeGen/ObjectFilePCHContainerOperations.h:34: undefined reference to `vtable for clang::ObjectFilePCHContainerReader'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_cc1as_main.cpp.o): in function `(anonymous namespace)::AssemblerInvocation::CreateFromArgs((anonymous namespace)::AssemblerInvocation&, llvm::ArrayRef<char const*>, clang::DiagnosticsEngine&)':
/home/luap/zig/src/zig_clang_cc1as_main.cpp:180: undefined reference to `clang::driver::getDriverOptTable()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_cc1as_main.cpp.o): in function `cc1as_main(llvm::ArrayRef<char const*>, char const*, void*)':
/home/luap/zig/src/zig_clang_cc1as_main.cpp:566: undefined reference to `clang::TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream&, clang::DiagnosticOptions*, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1as_main.cpp:568: undefined reference to `clang::DiagnosticIDs::DiagnosticIDs()'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1as_main.cpp:569: undefined reference to `clang::DiagnosticsEngine::DiagnosticsEngine(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs>, llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions>, clang::DiagnosticConsumer*, bool)'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1as_main.cpp:582: undefined reference to `clang::driver::getDriverOptTable()'
/usr/bin/ld: /home/luap/zig/src/zig_clang_cc1as_main.cpp:569: undefined reference to `clang::DiagnosticsEngine::~DiagnosticsEngine()'
/usr/bin/ld: zigcpp/libzigcpp.a(zig_clang_cc1as_main.cpp.o): in function `clang::getLastArgIntValue(llvm::opt::ArgList const&, llvm::opt::OptSpecifier, int, clang::DiagnosticsEngine&, unsigned int)':
/usr/include/clang/Driver/OptionUtils.h:40: undefined reference to `clang::getLastArgIntValue(llvm::opt::ArgList const&, llvm::opt::OptSpecifier, int, clang::DiagnosticsEngine*, unsigned int)'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/zig.dir/build.make:371: zig] Error 1
make[1]: *** [CMakeFiles/Makefile2:168: CMakeFiles/zig.dir/all] Error 2
make: *** [Makefile:149: all] Error 2
This looks like your LLVM, Clang, LLD libraries were compiled with a different compiler than you used to compile zig. If you are annoyed by this, welcome to the club, this is what C++ forces us to do.
Is there maybe a docker container or sth, which makes this easier?
https://github.com/ziglang/docker-zig
I don't think you need this though. The instructions have been tested on nearly every system out there. Anyway, in an hour or two the CI will have finished preparing a master branch tarball that includes the commit.
If you want to try a master branch binary tarball, the download page is now up-to-date, including the commit that adds support for environment variables to control cache directories.
Most helpful comment
So this looks like go is inserting flags before it passes
cc. This will either need to be fixed in go, or you will have to work around this by creating a shell script like this:And then putting that as your CC. This way zig gets invoked with
zig cc -E ...instead ofzig -E ...