In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the "Go language non-main package how to compile into a static library and use" related knowledge, Xiaobian through the actual case to show you the process of operation, the method of operation is simple and fast, practical, hope that this "Go language non-main package how to compile into a static library and use" article can help you solve the problem.
I. Environment
The author uses VSCode to view the environment variables of Go in the terminal, mainly GOROOT and GOPATH. The author's GOROOT is in C:\ Program Files\ Go.
II. Conventional engineering
Create a new directory anywhere, assuming test, and open it using VSCode. We create the following three project files:
Main.go
Package mainimport ("test/libDemo") func main () {libDemo.CallMe ()}
Go.mod
Module testgo 1.16
LibDemo.go
Package libDemoimport "fmt" func CallMe () {fmt.Println ("libDemo Be Call...")}
The directory structure is shown in the following figure:
In the main.go file window, press F5 to run, and you can see the normal execution result:
Compile libDemo to a static library
1. Mobile libDemo
Move the libDemo directory to ${GOROOT}\ src, and the author's is C:\ Program Files\ Go\ src. If you run it again at this time, you will get an error:
The main reason is that the path of import is wrong, so just get rid of test/.
two。 Compile libDemo to a static library
$go install libDemo
You can see the corresponding static library file libDemo.an in C:\ Program Files\ Go\ pkg\ windows_amd64
3. Move the libDemo source code from C:\ Program Files\ Go\ src to simulate a situation where the user has no source code and only a static library.
Running again at this time, there will be an error that libDemo can not be found, so it can not be run directly.
4. Compile main.go
Go to the directory where main.go is located and compile main.go:
Go tool compile-I "C:\ Program Files\ Go\ pkg\ windows_amd64" main.go
When compiling, you need to add the-I option to include the directory path "C:\ Program Files\ Go\ pkg\ windows_amd64". There are spaces in the path that need to be quoted. After successful compilation, main.o will be generated.
5. Link main.o
Go tool link-o test.exe-L "C:\ Program Files\ Go\ pkg\ windows_amd64" main.o
The-L option specifies the library path to be searched when linking. A successful link will generate a test.exe.
6. Run test.exe
You can see the normal output:
$. / test.exe
LibDemo Be Call...
If we delete C:\ Program Files\ Go\ pkg\ windows_amd64\ libDemo.a, the link will report an error:
C:\ Program Files\ Go\ pkg\ tool\ windows_amd64\ link.exe: cannot open file C:\ Program Files\ Go\ pkg\ windows_amd64\ libDemo.a: open C:\ Program Files\ Go\ pkg\ windows_amd64\ libDemo.a: The system cannot find the file specified.
This is the end of the introduction to "how to compile Go language non-main packages into static libraries and use them". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.