Skip to content

cmd/link: showing many ld warnings of "building for macOS, but linking in object file"  #33214

Open
@avence12

Description

@avence12

What version of Go are you using (go version)?

$ go version go1.12.7 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

Mac Mojave 10.14.5

Result of go env:

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/avence12/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/avence12/.gvm/pkgsets/go1.12.7/global:/Users/avence12/devel/go"
GOPROXY=""
GORACE=""
GOROOT="/Users/avence12/.gvm/gos/go1.12.7"
GOTMPDIR=""
GOTOOLDIR="/Users/avence12/.gvm/gos/go1.12.7/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9n/_tfc4bt12ps45qjyzh40jt1h0000gn/T/go-build525129295=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

  1. Install and use go1.12.7 by GVM
  2. Get into my project and run go build ./...
  3. The build is successful but seeing lots of ld warning. The same situation when I run go test ./...
  4. When I setup export CGO_ENABLED=0, the warning is gone. However I would like to know how to resolve the issue.

What did you expect to see?

No ld warnings when CGO_ENABLED=1

What did you see instead?

$ go build ./cmd/...
# github.com/avence12/myproj/cmd/client
ld: warning: building for macOS, but linking in object file (/var/folders/9n/_tfc4bt12ps45qjyzh40jt1h0000gn/T/go-link-703159202/go.o) built for 
# github.com/avence12/myproj/cmd/server
ld: warning: building for macOS, but linking in object file (/var/folders/9n/_tfc4bt12ps45qjyzh40jt1h0000gn/T/go-link-846047020/go.o) built for 

Activity

changed the title [-]Showing many ld warnings of "building for macOS, but linking in object file" [/-] [+]cmd/link: showing many ld warnings of "building for macOS, but linking in object file" [/+] on Jul 22, 2019
ianlancetaylor

ianlancetaylor commented on Jul 22, 2019

@ianlancetaylor
Contributor
added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Jul 22, 2019
modified the milestones: 1.14, Go1.14 on Jul 22, 2019
ianlancetaylor

ianlancetaylor commented on Jul 22, 2019

@ianlancetaylor
Contributor

Which version of XCode do you have installed?

thanm

thanm commented on Jul 22, 2019

@thanm
Contributor

Is that the complete warning (e.g. "built for" and then nothing more)? Wondering if the output got truncated somehow.

avence12

avence12 commented on Jul 23, 2019

@avence12
Author

@ianlancetaylor:
I did not install XCode when I saw the issue. After I install XCode Version 10.2.1, the issue remains.

@thanm:
I add the result of go env and complete warning to origin message. There is nothing after "built for".

juev

juev commented on Jul 23, 2019

@juev

The same issue for me.

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/d.evsyukov/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/d.evsyukov/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/3k/hqxhm_y10vx682dnsbb64l7r0000gp/T/go-build135546081=/tmp/go-build -gno-record-gcc-switches -fno-common"
$ go get -u github.com/saibing/bingo
# github.com/saibing/bingo
ld: warning: building for macOS, but linking in object file (/var/folders/3k/hqxhm_y10vx682dnsbb64l7r0000gp/T/go-link-834839606/go.o) built for
xuhrc

xuhrc commented on Jul 24, 2019

@xuhrc

ld: warning: building for macOS, but linking in object file (/var/folders/78/6hfbqt9x3z3bvm41rhn81smhx0t1sf/T/go-link-034794354/go.o) built for

go env:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPROXY="https://goproxy.io"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/78/6hfbqt9x3z3bvm41rhn81smhx0t1sf/T/go-build817734651=/tmp/go-build -gno-record-gcc-switches -fno-common"

same issue for me

The warning disappeared after i installed command line tools beta 6

thanm

thanm commented on Jul 24, 2019

@thanm
Contributor

I'm not having any luck reproducing this.

I'm working with a MacOS 10.14.5 macbook, Xcode 10.3, installed go 1.12.7 using "gvm", but when I build the projects above I don't see any warnings.

It might help to know what version of the linker you're working with, since it's the linker issuing the warning.

Please try this small experiment: run clang on a tiny C file to invoke the linker, then using the linker path you see in the output, run "ld -v" to see what version it claims to be:

$ echo "int main() { return 0; }" > small.c
$ clang -v small.c 1> transcript.txt 2>&1
$ fgrep /ld transcript
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.14.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -o a.out /var/folders/y1/14s910p95pj3vb71ywd53wk8008r__/T/small-788b80.o -L/usr/local/lib -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.osx.a
$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -v

@(#)PROGRAM:ld  PROJECT:ld64-450.3
BUILD 18:16:53 Apr  5 2019
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 10.0.1, (clang-1001.0.46.4) (static support for 22, runtime is 22)
TAPI support using: Apple TAPI version 10.0.1 (tapi-1001.0.4.1)
juev

juev commented on Jul 24, 2019

@juev
$ echo "int main() { return 0; }" > small.c
$ clang -v small.c 1> transcript.txt 2>&1
$ ll
total 16K
-rwxr-xr-x 1 d.evsyukov 4.2K Jul 24 19:16 a.out
-rw-r--r-- 1 d.evsyukov   25 Jul 24 19:16 small.c
-rw-r--r-- 1 d.evsyukov 2.5K Jul 24 19:16 transcript.txt

$ grep /ld transcript.txt
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.14.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -o a.out /var/folders/ss/g0zcdcj13p54s6fj2353vlxh0000gn/T/small-f8f73a.o -L/usr/local/lib -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.osx.a

$ /Library/Developer/CommandLineTools/usr/bin/ld -v
@(#)PROGRAM:ld  PROJECT:ld64-450.3
BUILD 18:45:16 Apr  4 2019
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 10.0.1, (clang-1001.0.46.4) (static support for 22, runtime is 22)
TAPI support using: Apple TAPI version 10.0.1 (tapi-1001.0.4.1)
thanm

thanm commented on Jul 24, 2019

@thanm
Contributor

Thanks @juev . Looks like your linker is more or less the same vintage as mine, so nothing notable there.

I also tried deleting Xcode from my Mac and to just the downloaded command line tools, but that didn't help.

How about this as another possible experiment to try: redo the link with -tmpdir set, which should capture the object files feeding into the external linker, then run 'file' on one of the ones the linker is complaining about to see what it looks like. Demo:

$ cd $GOPATH/src/github.com/saibing/bingo
$ go clean -cache ; rm -f bingo
$ mkdir /tmp/xxx
$ go build -ldflags=-tmpdir=/tmp/xxx .
$ ls /tmp/xxx
000000.o	000005.o	000010.o	000015.o	go.dwarf
000001.o	000006.o	000011.o	000016.o	go.o
000002.o	000007.o	000012.o	000017.o	trivial.c
000003.o	000008.o	000013.o	000018.o
000004.o	000009.o	000014.o	a.out
$ file /tmp/xxx/go.o
/tmp/xxx/go.o: Mach-O 64-bit object x86_64
$
avence12

avence12 commented on Jul 25, 2019

@avence12
Author

@thanm My ld is the same as your version.

$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -v
@(#)PROGRAM:ld  PROJECT:ld64-450.3
BUILD 18:16:53 Apr  5 2019
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 10.0.1, (clang-1001.0.46.4) (static support for 22, runtime is 22)
TAPI support using: Apple TAPI version 10.0.1 (tapi-1001.0.4.1)

Another test:

$ go get  github.com/saibing/bingo
# github.com/saibing/bingo
ld: warning: building for macOS, but linking in object file (/var/folders/9n/_tfc4bt12ps45qjyzh40jt1h0000gn/T/go-link-110299481/go.o) built for

$ cd $GOPATH/src/github.com/saibing/bingo
$ go clean -cache ; rm -f bingo
$ mkdir /tmp/xxx
$ go build -ldflags=-tmpdir=/tmp/xxx .
# github.com/saibing/bingo
ld: warning: building for macOS, but linking in object file (/tmp/xxx/go.o) built for
$ ls /tmp/xxx
000000.o	000005.o	000010.o	000015.o	go.dwarf
000001.o	000006.o	000011.o	000016.o	go.o
000002.o	000007.o	000012.o	000017.o	trivial.c
000003.o	000008.o	000013.o	000018.o
000004.o	000009.o	000014.o	a.out
$ file /tmp/xxx/go.o
/tmp/xxx/go.o: Mach-O 64-bit object x86_64

One thing may be related, the gcc version:

$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.20.17)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

4 remaining items

caseylucas

caseylucas commented on Aug 7, 2019

@caseylucas

I was having the same problem:

ld: warning: building for macOS, but linking in object file (/var/folders/fm/l0kfkxsn7lqgl9wdwtz_5lk00000gn/T/go-link-305394611/go.o) built for

when using:

pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
package-id: com.apple.pkg.CLTools_Executables
version: 11.0.0.0.1.1559496560
volume: /
location: /
install-time: 1559652597
groups: com.apple.FindSystemFiles.pkg-group

After upgrading to "Command Line Tools for Xcode 11 Beta 5" (available at https://developer.apple.com/download/more/) the problem went away:

pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
package-id: com.apple.pkg.CLTools_Executables
version: 11.0.0.0.1.1564155526
volume: /
location: /
install-time: 1565197651
groups: com.apple.FindSystemFiles.pkg-group
thanm

thanm commented on Aug 9, 2019

@thanm
Contributor

@avence12 , @caseylucas thanks for finding those workarounds.

oshankkumar

oshankkumar commented on Aug 30, 2019

@oshankkumar

I am also facing a similar issue

ld: warning: building for macOS, but linking in object file (/var/folders/kr/2n4cw21x2rb76jw6ln6qprdw0000gn/T/go-link-005534797/go.o) built for

oshankkumar@Oshanks-MacBook-Pro:~$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/oshankkumar/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/oshankkumar/Go-Workspace"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kr/2n4cw21x2rb76jw6ln6qprdw0000gn/T/go-build929797784=/tmp/go-build -gno-record-gcc-switches -fno-common"
modified the milestones: Go1.14, Backlog on Oct 9, 2019
jacob-carlborg

jacob-carlborg commented on Oct 12, 2019

@jacob-carlborg

I ran into the same issue with the D compiler.

The issue is that the linker is looking for a load command in each object file which contains the platform, platform version and SDK version. Clang outputs this load command but the D compiler doesn't. I'm guessing the Go compiler isn't outputting this load command either. I'm guessing the output looks cut off because the linker doesn't find this load command and therefore prints nothing.

I'm guessing the linker is doing this to verify that the all the object files being linked are compatible. The linker can also make sure that all features are supported by the target platform. For example, if thread local storage are being used but targeting macOS 10.6 (TLS was added in 10.7) the linker will fail to link the executable.

Have a look at the build_version_command struct in the mach-o/loader.h header file.

moved this to Triage Backlog in Go Compiler / Runtimeon Jul 15, 2022
Clivern

Clivern commented on Feb 22, 2023

@Clivern

I see something similar. still looking for the main cause!

$ sw_vers
ProductName:		macOS
ProductVersion:		13.2
BuildVersion:		22D49

$ go version
go version go1.19 darwin/amd64


# command-line-arguments
ld: warning: -no_pie is deprecated when targeting new OS versions
ld: warning: non-standard -pagezero_size is deprecated when targeting macOS 13.0 or later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.OS-Darwincompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Triage Backlog

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rsc@juev@jacob-carlborg@caseylucas@Clivern

        Issue actions

          cmd/link: showing many ld warnings of "building for macOS, but linking in object file" · Issue #33214 · golang/go