在Mac上使用Golang创建服务器时遇到的错误

在Mac上使用Golang创建服务器时遇到的错误 以下是我的代码……

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	fmt.Printf("Starting server at port 8080\n")

	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}

第一行显示错误,当我尝试运行它时,出现以下错误……

# runtime/cgo
ld: warning: ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libpthread.tbd, file was built for unsupported file format ( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) which is not the architecture being linked (x86_64): /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libpthread.tbd
ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libSystem.tbd, file was built for unsupported file format ( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) which is not the architecture being linked (x86_64): /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libSystem.tbd
Undefined symbols for architecture x86_64:
  "___stack_chk_fail", referenced from:
      _x_cgo_init in _x004.o
      __cgo_sys_thread_start in _x004.o
  "___stack_chk_guard", referenced from:
      _x_cgo_init in _x004.o
      __cgo_sys_thread_start in _x004.o
  "___stderrp", referenced from:
      __cgo_sys_thread_start in _x004.o
      _x_cgo_sys_thread_create in _x005.o
      _x_cgo_thread_start in _x008.o
  "_abort", referenced from:
      __cgo_sys_thread_start in _x004.o
      _x_cgo_sys_thread_create in _x005.o
      _x_cgo_thread_start in _x008.o
  "_fprintf", referenced from:
      __cgo_sys_thread_start in _x004.o
      _x_cgo_sys_thread_create in _x005.o
  "_free", referenced from:
      _threadentry in _x004.o
  "_fwrite", referenced from:
      _x_cgo_thread_start in _x008.o
  "_malloc", referenced from:
      _x_cgo_thread_start in _x008.o
  "_nanosleep", referenced from:
      _x_cgo_sys_thread_create in _x005.o
      __cgo_try_pthread_create in _x005.o
  "_pthread_attr_destroy", referenced from:
      _x_cgo_init in _x004.o
  "_pthread_attr_getstacksize", referenced from:
      _x_cgo_init in _x004.o
      __cgo_sys_thread_start in _x004.o
  "_pthread_attr_init", referenced from:
      _x_cgo_init in _x004.o
      __cgo_sys_thread_start in _x004.o
  "_pthread_cond_broadcast", referenced from:
      _x_cgo_notify_runtime_init_done in _x005.o
  "_pthread_cond_wait", referenced from:
      __cgo_wait_runtime_init_done in _x005.o
  "_pthread_create", referenced from:
      _x_cgo_sys_thread_create in _x005.o
      __cgo_try_pthread_create in _x005.o
     (maybe you meant: __cgo_try_pthread_create)
  "_pthread_detach", referenced from:
      _x_cgo_sys_thread_create in _x005.o
      __cgo_try_pthread_create in _x005.o
  "_pthread_mutex_lock", referenced from:
      __cgo_wait_runtime_init_done in _x005.o
      _x_cgo_notify_runtime_init_done in _x005.o
      _x_cgo_set_context_function in _x005.o
      __cgo_get_context_function in _x005.o
  "_pthread_mutex_unlock", referenced from:
      __cgo_wait_runtime_init_done in _x005.o
      _x_cgo_notify_runtime_init_done in _x005.o
      _x_cgo_set_context_function in _x005.o
      __cgo_get_context_function in _x005.o
  "_pthread_sigmask", referenced from:
      __cgo_sys_thread_start in _x004.o
  "_setenv", referenced from:
      _x_cgo_setenv in _x006.o
     (maybe you meant: _x_cgo_setenv)
  "_strerror", referenced from:
      __cgo_sys_thread_start in _x004.o
      _x_cgo_sys_thread_create in _x005.o
  "_unsetenv", referenced from:
      _x_cgo_unsetenv in _x006.o
     (maybe you meant: _x_cgo_unsetenv)
ld: symbol(s) not found for architecture x86_64
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)

请告诉我应该怎么做……提前感谢……


更多关于在Mac上使用Golang创建服务器时遇到的错误的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

如何运行它?是直接使用 go run main.go 还是其他方式?

func main() {
    fmt.Println("hello world")
}

更多关于在Mac上使用Golang创建服务器时遇到的错误的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


这个错误是由于macOS SDK版本不匹配导致的。你的代码本身是正确的,但编译环境有问题。以下是解决方案:

首先检查你的Go版本和macOS版本:

package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Printf("Go version: %s\n", runtime.Version())
    fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
}

运行这个程序查看你的Go环境信息。

要解决这个问题,需要重新安装或更新Xcode命令行工具:

# 1. 移除现有的命令行工具
sudo rm -rf /Library/Developer/CommandLineTools

# 2. 重新安装命令行工具
xcode-select --install

# 3. 接受许可协议
sudo xcodebuild -license accept

# 4. 设置正确的SDK路径
sudo xcode-select -switch /Library/Developer/CommandLineTools

如果问题仍然存在,可以尝试指定CGO编译标志:

// 在编译时设置环境变量
// 在终端中运行:
CGO_ENABLED=0 go build -o server main.go

或者创建一个更完整的服务器示例来测试:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Server is working!")
}

func main() {
    http.HandleFunc("/", handler)
    
    fmt.Println("Starting server at port 8080")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal("Server failed to start:", err)
    }
}

如果还是遇到问题,可以尝试完全重新安装Go:

# 卸载Go
sudo rm -rf /usr/local/go

# 重新下载并安装最新版Go
# 从 https://golang.org/dl/ 下载适合你系统的安装包

编译时也可以尝试禁用CGO:

# 使用静态编译
go build -ldflags="-s -w" -tags netgo -installsuffix netgo main.go

这些步骤应该能解决你的编译问题。

回到顶部