Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linker error when compiling on windows #379

Open
fiwippi opened this issue Apr 20, 2020 · 7 comments
Open

Linker error when compiling on windows #379

fiwippi opened this issue Apr 20, 2020 · 7 comments

Comments

@fiwippi
Copy link

fiwippi commented Apr 20, 2020

Hi,

Just as a preface, this issue might be related to #326 and #265

So when I compile a go script using the portaudio library, ld is able to find the portaudio.dll. In the same way, when compiling ui code, go is able to compile it. However, when I try to compile portaudio and ui code, ld is not able to find the portaudio dll.

After checking the verbose output, the difference is that when it does not work, g++ is used instead of gcc when calling the external linker.

What I don't understand is that when calling ld from the command line with the -L flag to specify the linker library, it is able to find the portaudio dll, But when compiling with go build, the linker is not able to find the dll and it is confusing the hell out of me haha.

For reference, this is the failed build script:

WORK=C:\Users\nadav\AppData\Local\Temp\go-build899281014
mkdir -p $WORK\b001\
cat >$WORK\b001\importcfg.link << 'EOF' # internal
packagefile command-line-arguments=C:\Users\nadav\AppData\Local\go-build\82\824845ce2bbcbae9b36bc0262184eb3f4721048c3da4ed0f1adf54d458d910c7-d
packagefile github.com/andlabs/ui=C:\Users\nadav\go\pkg\windows_amd64\github.com\andlabs\ui.a
packagefile github.com/andlabs/ui/winmanifest=C:\Users\nadav\go\pkg\windows_amd64\github.com\andlabs\ui\winmanifest.a
packagefile github.com/gordonklaus/portaudio=C:\Users\nadav\go\pkg\windows_amd64\github.com\gordonklaus\portaudio.a
packagefile runtime=c:\go\pkg\windows_amd64\runtime.a
packagefile errors=c:\go\pkg\windows_amd64\errors.a
packagefile image=c:\go\pkg\windows_amd64\image.a
packagefile sync=c:\go\pkg\windows_amd64\sync.a
packagefile time=c:\go\pkg\windows_amd64\time.a
packagefile runtime/cgo=c:\go\pkg\windows_amd64\runtime\cgo.a
packagefile syscall=c:\go\pkg\windows_amd64\syscall.a
packagefile fmt=c:\go\pkg\windows_amd64\fmt.a
packagefile os=c:\go\pkg\windows_amd64\os.a
packagefile reflect=c:\go\pkg\windows_amd64\reflect.a
packagefile internal/bytealg=c:\go\pkg\windows_amd64\internal\bytealg.a
packagefile internal/cpu=c:\go\pkg\windows_amd64\internal\cpu.a
packagefile runtime/internal/atomic=c:\go\pkg\windows_amd64\runtime\internal\atomic.a
packagefile runtime/internal/math=c:\go\pkg\windows_amd64\runtime\internal\math.a
packagefile runtime/internal/sys=c:\go\pkg\windows_amd64\runtime\internal\sys.a
packagefile internal/reflectlite=c:\go\pkg\windows_amd64\internal\reflectlite.a
packagefile bufio=c:\go\pkg\windows_amd64\bufio.a
packagefile image/color=c:\go\pkg\windows_amd64\image\color.a
packagefile io=c:\go\pkg\windows_amd64\io.a
packagefile strconv=c:\go\pkg\windows_amd64\strconv.a
packagefile sync/atomic=c:\go\pkg\windows_amd64\sync\atomic.a
packagefile internal/race=c:\go\pkg\windows_amd64\internal\race.a
packagefile internal/syscall/windows/registry=c:\go\pkg\windows_amd64\internal\syscall\windows\registry.a
packagefile internal/oserror=c:\go\pkg\windows_amd64\internal\oserror.a
packagefile internal/syscall/windows/sysdll=c:\go\pkg\windows_amd64\internal\syscall\windows\sysdll.a
packagefile unicode/utf16=c:\go\pkg\windows_amd64\unicode\utf16.a
packagefile internal/fmtsort=c:\go\pkg\windows_amd64\internal\fmtsort.a
packagefile math=c:\go\pkg\windows_amd64\math.a
packagefile unicode/utf8=c:\go\pkg\windows_amd64\unicode\utf8.a
packagefile internal/poll=c:\go\pkg\windows_amd64\internal\poll.a
packagefile internal/syscall/windows=c:\go\pkg\windows_amd64\internal\syscall\windows.a
packagefile internal/testlog=c:\go\pkg\windows_amd64\internal\testlog.a
packagefile unicode=c:\go\pkg\windows_amd64\unicode.a
packagefile bytes=c:\go\pkg\windows_amd64\bytes.a
packagefile math/bits=c:\go\pkg\windows_amd64\math\bits.a
packagefile sort=c:\go\pkg\windows_amd64\sort.a
EOF
mkdir -p $WORK\b001\exe\
cd .
"c:\\go\\pkg\\tool\\windows_amd64\\link.exe" -o "C:\\Users\\nadav\\AppData\\Local\\Temp\\go-build899281014\\b001\\exe\\a.out.exe" -importcfg "C:\\Users\\nadav\\AppData\\Local\\Temp\\go-build899281014\\b001\\importcfg.link" -buildmode=exe -buildid=qbWb98bCfThjdneEe-oJ/fi_1RHQuG_COvVrnh5VK/Yvcj_mZYbyoyFkQvTr_g/qbWb98bCfThjdneEe-oJ -extldflags "-LC:\\TDM-GCC-64\\lib\\ --verbose" -extld=g++ "C:\\Users\\nadav\\AppData\\Local\\go-build\\82\\824845ce2bbcbae9b36bc0262184eb3f4721048c3da4ed0f1adf54d458d910c7-d"
# command-line-arguments
c:\go\pkg\tool\windows_amd64\link.exe: running g++ failed: exit status 1
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/TDM-GCC-64/bin/../libexec/gcc/x86_64-w64-mingw32/9.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-git-9.2.0/configure --build=x86_64-w64-mingw32 --enable-targets=all --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-cxx-flags=-DWINPTHREAD_STATIC --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --enable-large-address-aware --disable-rpath --disable-symvers --prefix=/mingw64tdm --with-local-prefix=/mingw64tdm --with-pkgversion=tdm64-1 --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 9.2.0 (tdm64-1) 
COMPILER_PATH=C:/TDM-GCC-64/bin/../libexec/gcc/x86_64-w64-mingw32/9.2.0/;C:/TDM-GCC-64/bin/../libexec/gcc/;C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/;C:/TDM-GCC-64/bin/../lib/gcc/;C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/;C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../lib/;C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/;C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../
COLLECT_GCC_OPTIONS='-m64' '-mconsole' '-o' 'C:\Users\nadav\AppData\Local\Temp\go-build899281014\b001\exe\a.out.exe' '-g' '-O2' '-static' '-static-libgcc' '-g' '-O2' '-LC:/MinGW/msys/1.0/local/lib' '-g' '-O2' '-no-pie' '-LC:\TDM-GCC-64\lib\' '-v' '-mtune=generic' '-march=x86-64'
 C:/TDM-GCC-64/bin/../libexec/gcc/x86_64-w64-mingw32/9.2.0/collect2.exe -plugin C:/TDM-GCC-64/bin/../libexec/gcc/x86_64-w64-mingw32/9.2.0/liblto_plugin-0.dll -plugin-opt=C:/TDM-GCC-64/bin/../libexec/gcc/x86_64-w64-mingw32/9.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\nadav\AppData\Local\Temp\cceGIZXB.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -m i386pep --exclude-libs=libpthread.a --subsystem console -Bstatic -o $WORK\b001\exe\a.out.exe C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/crtbegin.o -LC:/MinGW/msys/1.0/local/lib -LC:\TDM-GCC-64\lib\ -LC:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0 -LC:/TDM-GCC-64/bin/../lib/gcc -LC:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib -LC:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../lib -LC:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib -LC:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../.. C:\Users\nadav\AppData\Local\Temp\go-link-100908443\go.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000000.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000001.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000002.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000003.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000004.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000005.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000006.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000007.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000008.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000009.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000010.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000011.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000012.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000013.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000014.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000015.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000016.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000017.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000018.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000019.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000020.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000021.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000022.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000023.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000024.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000025.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000026.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000027.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000028.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000029.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000030.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000031.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000032.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000033.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000034.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000035.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000036.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000037.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000038.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000039.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000040.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000041.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000042.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000043.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000044.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000045.o C:\Users\nadav\AppData\Local\Temp\go-link-100908443\000046.o C:/Users/nadav/go/src/github.com/andlabs/ui/libui_windows_amd64.a -luser32 -lkernel32 -lgdi32 -lcomctl32 -luxtheme -lmsimg32 -lcomdlg32 -ld2d1 -ldwrite -lole32 -loleaut32 -loleacc -luuid -lwindowscodecs -lportaudio -lwinmm -lm -lole32 -luuid -T C:\Users\nadav\AppData\Local\Temp\go-link-100908443\fix_debug_gdb_scripts.ld --start-group -lmingwex -lmingw32 --end-group -lkernel32 -lstdc++ -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/default-manifest.o C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/crtend.o
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lportaudio
collect2.exe: error: ld returned 1 exit status

Go Env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\nadav\AppData\Local\go-build
set GOENV=C:\Users\nadav\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\nadav\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\nadav\AppData\Local\Temp\go-build214969006=/tmp/go-build -gno-record-gcc-switches

For reference, I am using the TDM-GCC compiler

@andlabs
Copy link
Owner

andlabs commented Apr 20, 2020

Hm, can we somehow get g++ to be super-verbose about its linking decisions?

@fiwippi
Copy link
Author

fiwippi commented Apr 20, 2020

Hey so I managed to get verbose output for the g++ linker and I have identified the error sorta although I am not sure what it means.

The ld looker searches for libportuadio.a and libportaudio.lib as opposed to .dll. I don't really know what this means. When compiling without an ui code, the linker also searches for .dll files which in turn means it actually finds the .dll file.

I omitted the full output because it is meaningless and very large. If you would like it I can include it below.

Edit: This issue might be a duplicate of #347

Edit: Edit: After using the -Bdynamic flag as suggested in the issue which #347 references, portaudio is found but not found at the same time. I am so confused. The output is given below:

attempt to open C:\TDM-GCC-64\lib\/libportaudio.dll succeeded
C:\TDM-GCC-64\lib\/libportaudio.dllC:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lportaudio
collect2.exe: error: ld returned 1 exit status

@andlabs
Copy link
Owner

andlabs commented Apr 20, 2020

I don't understand why it would be trying to do this but I'm betting it's trying to only use static libraries OR dynamic libraries. I wonder if specifying both -static and -dynamic or -Bdynamic before the respective libraries will do it. If that doesn't work, I wonder if the grouping arguments (whose names I don't remember) can be used to make some libraries static and some dynamic.

@fiwippi
Copy link
Author

fiwippi commented Apr 20, 2020

I found a static version of the dlI needed which meant ui stopped causing problems.

Thanks. I will close the issue now.

@fiwippi fiwippi closed this as completed Apr 20, 2020
@andlabs andlabs reopened this Apr 21, 2020
@andlabs
Copy link
Owner

andlabs commented Apr 21, 2020

Then yes, that is what's happening. This is worth investigating, but I'm somewhat sure it's me doing something wrong.

@fiwippi
Copy link
Author

fiwippi commented Apr 27, 2020

Hey, I did some more research into why the problem was occuring. Essentialy, I was adding a -Bdynamic -lportaudio option in the command line options. Instead of making g++ linker look for a dynamic file instead of a static file, it simply added the dynamic version of the file to the list of files it was searching so that it was looking for portaudio.lib and portaudio.dll. This was solved by removing the -Bdynamic flag and then simply providing the static library.

Hopefully this helped at all.
Thanks

@andlabs
Copy link
Owner

andlabs commented Apr 27, 2020

Yeah, but there's no reason why we shouldn't allow a mix of static and dynamic dependencies for different projects. I'm pretty sure I'm doing something wrong, and I know the GNU linker has grouping options specifically for this purpose, but I'll have to investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants