2023-05-14 23:35:07 +00:00
# Compile the document
2021-05-26 11:05:53 +00:00
2023-05-14 23:35:07 +00:00
## Preparatory Work
2021-05-26 11:05:53 +00:00
2023-05-14 23:35:07 +00:00
Xray uses [Golang ](https://golang.org/ ) as its programming language, so you need to install the latest version of Golang first in order to compile.
2021-05-26 11:05:53 +00:00
::: tip TIP
2023-05-14 23:35:07 +00:00
Install Golang: [golang.org/doc/install ](https://golang.org/doc/install )
2021-05-26 11:05:53 +00:00
:::
2023-05-14 23:35:07 +00:00
If you happen to use Windows, please **make sure** to use Powershell.
2021-05-26 11:05:53 +00:00
2023-05-14 23:35:07 +00:00
## Pull Xray source code
2021-05-26 11:05:53 +00:00
```bash
git clone https://github.com/XTLS/Xray-core.git
cd Xray-core & & go mod download
```
2023-05-14 23:35:07 +00:00
If you have free time, you can try GitHub's official tool: `gh repo clone XTLS/Xray-core`
2021-05-26 11:05:53 +00:00
2023-05-14 23:35:07 +00:00
Note: In a network environment where Google cannot be accessed normally, dependencies cannot be pulled normally, and `GOPROXY` needs to be set first:
2021-05-26 11:05:53 +00:00
```bash
go env -w GOPROXY=https://goproxy.io,direct
```
2023-05-14 23:35:07 +00:00
## Build Binary
2021-05-26 11:05:53 +00:00
:::warning
2023-05-14 23:35:07 +00:00
This command needs to be executed within Xray root directory.
2021-05-26 11:05:53 +00:00
:::
### Windows(Powershell):
```powershell
$env:CGO_ENABLED=0
go build -o xray.exe -trimpath -ldflags "-s -w -buildid=" ./main
```
### macOS, Linux:
```bash
CGO_ENABLED=0 go build -o xray -trimpath -ldflags "-s -w -buildid=" ./main
```
2023-05-14 23:35:07 +00:00
Running the above command will generate an xray executable file in the directory.
2021-05-26 11:05:53 +00:00
::: tip
2023-05-14 23:35:07 +00:00
If you need to compile a program that can be debugged, i.e., you can use dlv to attach to the running program for debugging, please remove the '-w -s' options from the ldflags.
2021-05-26 11:05:53 +00:00
2023-05-14 23:35:07 +00:00
- w option disables the generation of debug information. After using this option, gdb cannot be used for debugging.
- s option disables the symbol table.
2023-05-19 15:06:51 +00:00
PS: Actually, debugging with vscode or other IDEs seems to be more convenient.
2021-05-26 11:05:53 +00:00
2023-05-14 23:35:07 +00:00
## Cross compilation:
2021-05-26 11:05:53 +00:00
2023-05-14 23:35:07 +00:00
Here, we take the example of compiling to a Linux server in a Windows (Powershell) environment:
2021-05-26 11:05:53 +00:00
```powershell
$env:CGO_ENABLED=0
$env:GOOS="linux"
$env:GOARCH="amd64"
```
2023-05-14 23:35:07 +00:00
go build -o xray -trimpath -ldflags "-s -w -buildid=" ./main```
After uploading to the server, remember to execute `chmod +x xray` in the server terminal.
2021-05-26 11:05:53 +00:00
::: tip
2023-05-14 23:35:07 +00:00
Execute `go tool dist list` to view all supported systems and architectures.
2021-05-26 11:05:53 +00:00
:::
2023-05-14 23:35:07 +00:00
## Reproducible Build:
2021-05-26 11:05:53 +00:00
2023-05-14 23:35:07 +00:00
Following the above steps, it is possible to compile and release an identical binary file as the one in Release.
2021-05-26 11:05:53 +00:00
::: warning
2023-05-14 23:35:07 +00:00
Please confirm that you are using the same Golang version as the one used to compile the release.
2023-05-19 15:06:51 +00:00
:::