1

For some reasons I need to use "go tool compile" to compile go files instead of using the automatic go build. "go tool compile" works fine with one file or multiple files in the same package, but if my project structure is like:

-package1
--p1.go
-package2
--p2.go
main.go
go.mod

where main.go uses symbols defined in package1 and package2. Then how am I supposed to use :go tool compile" to eventually get an executable program?

ddaa
  • 49
  • 2
  • 1
    Not at all: Combining several object files into an executable is done via go tool link. – Volker May 11 '22 at 09:19
  • 1
    I understand what you mean. But my problem is that main.go uses symbols in package1 and package2. So I simply cannot successfully compile main.go. What I need is a way to tell the compiler that there is a symbol table of package1 and package2 to help with compiling. And then I can use go link – ddaa May 11 '22 at 09:45
  • If I use "go tool compile ./package1/p1.go main.go", compiler will report message like "expected package package1 but got package main". If I only compile main.go, It cannot find the symbols and got syntax error ("could not import package1 (file not found)"). – ddaa May 11 '22 at 09:51
  • You have to compile p1 first, then main providing the right arguments, then link. Invoke go build with -x to see the commands. It's complicated. BTW: "[f]for some reasons I need to use "go tool compile" to compile go files" sound extremely fishy. – Volker May 11 '22 at 11:13

0 Answers0