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

Undefined symbols for architecture arm64: "_MTLCopyAllDevices" #1344

Closed
lixianmin opened this issue Oct 5, 2023 · 6 comments · Fixed by #1530
Closed

Undefined symbols for architecture arm64: "_MTLCopyAllDevices" #1344

lixianmin opened this issue Oct 5, 2023 · 6 comments · Fixed by #1530

Comments

@lixianmin
Copy link

I am using "Apple M1 Max", to compile the lastest commit 91c0b23, it reports the following error:

---------------command line:
cd bindings/go
make examples

-------------- error message:

Mkdir build
Mkdir models
Build whisper
I whisper.cpp build info: 
I UNAME_S:  Darwin
I UNAME_P:  arm
I UNAME_M:  arm64
I CFLAGS:   -I.              -O3 -DNDEBUG -std=c11   -fPIC -D_XOPEN_SOURCE=600 -D_DARWIN_C_SOURCE -pthread -DGGML_USE_ACCELERATE -DGGML_USE_METAL
I CXXFLAGS: -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -D_XOPEN_SOURCE=600 -D_DARWIN_C_SOURCE -pthread -DGGML_USE_METAL
I LDFLAGS:   -framework Accelerate -framework Foundation -framework Metal -framework MetalKit
I CC:       Apple clang version 14.0.0 (clang-1400.0.29.202)
I CXX:      Apple clang version 14.0.0 (clang-1400.0.29.202)

cc  -I.              -O3 -DNDEBUG -std=c11   -fPIC -D_XOPEN_SOURCE=600 -D_DARWIN_C_SOURCE -pthread -DGGML_USE_ACCELERATE -DGGML_USE_METAL   -c ggml.c -o ggml.o
cc  -I.              -O3 -DNDEBUG -std=c11   -fPIC -D_XOPEN_SOURCE=600 -D_DARWIN_C_SOURCE -pthread -DGGML_USE_ACCELERATE -DGGML_USE_METAL   -c ggml-alloc.c -o ggml-alloc.o
c++ -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -D_XOPEN_SOURCE=600 -D_DARWIN_C_SOURCE -pthread -DGGML_USE_METAL -c whisper.cpp -o whisper.o
cc -I.              -O3 -DNDEBUG -std=c11   -fPIC -D_XOPEN_SOURCE=600 -D_DARWIN_C_SOURCE -pthread -DGGML_USE_ACCELERATE -DGGML_USE_METAL -c ggml-metal.m -o ggml-metal.o
ar rcs libwhisper.a ggml.o ggml-alloc.o whisper.o ggml-metal.o
Build example go-model-download
Build example go-whisper
# github.com/ggerganov/whisper.cpp/bindings/go/examples/go-whisper
/usr/local/go/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
Undefined symbols for architecture arm64:
  "_MTLCopyAllDevices", referenced from:
      _ggml_metal_init in libwhisper.a(ggml-metal.o)
  "_MTLCreateSystemDefaultDevice", referenced from:
      _ggml_metal_init in libwhisper.a(ggml-metal.o)
  "_OBJC_CLASS_$_MTLComputePassDescriptor", referenced from:
      objc-class-ref in libwhisper.a(ggml-metal.o)
  "_OBJC_CLASS_$_NSBundle", referenced from:
      objc-class-ref in libwhisper.a(ggml-metal.o)
  "_OBJC_CLASS_$_NSString", referenced from:
      objc-class-ref in libwhisper.a(ggml-metal.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

if I modify whisper.go line 13 to the following code, the error message will disappear, and will produce libwhisper.a:

#cgo darwin LDFLAGS: -framework Accelerate -framework Foundation -framework Metal

however, when I compile my golang code with whiper.go, it also complains the following error message:

Undefined symbols for architecture arm64:
  "_MTLCopyAllDevices", referenced from:
      _ggml_metal_init in libwhisper.a(ggml-metal.o)
  "_MTLCreateSystemDefaultDevice", referenced from:
      _ggml_metal_init in libwhisper.a(ggml-metal.o)
  "_OBJC_CLASS_$_MTLComputePassDescriptor", referenced from:
      objc-class-ref in libwhisper.a(ggml-metal.o)
  "_OBJC_CLASS_$_NSBundle", referenced from:
      objc-class-ref in libwhisper.a(ggml-metal.o)
  "_OBJC_CLASS_$_NSString", referenced from:
      objc-class-ref in libwhisper.a(ggml-metal.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


Compilation finished with exit code 1

I do not know how to resolve such an error, can you provide any help tips?

@i11
Copy link

i11 commented Oct 7, 2023

I'm a random person here, so please take whatever I write with a grain of salt. That said, it seems like libwhisper.a is linked with a few frameworks, that are missing in the examples makefile. Check https://github.com/ggerganov/whisper.cpp/blob/master/Makefile which frameworks are added for your particular environment.
For these specific symbols add -framework Foundation -framework Metal -framework MetalKit into LDFLAGS

@lixianmin
Copy link
Author

lixianmin commented Oct 8, 2023 via email

@lixianmin
Copy link
Author

lixianmin commented Oct 8, 2023 via email

@petercsiba
Copy link

petercsiba commented Nov 14, 2023

Encountering the same.
Taking i11ones wild guess, I made it work on my M2 verified with make test in the bindings/go directory.

For the lack of better way sharing the raw diff:

diff --git a/bindings/go/Makefile b/bindings/go/Makefile
+EXT_LDFLAGS := -framework Foundation -framework Metal -framework MetalKit
 
 test: model-small whisper modtidy
-       @C_INCLUDE_PATH=${INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} go test -v .
-       @C_INCLUDE_PATH=${INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} go test -v ./pkg/whisper/...
+       @C_INCLUDE_PATH=${INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} go test -ldflags "-extldflags '$(EXT_LDFLAGS)'" -v .
+       @C_INCLUDE_PATH=${INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} go test -ldflags "-extldflags '$(EXT_LDFLAGS)'" -v ./pkg/whisper/...
 
@@ -21,7 +22,7 @@ model-small: mkdir examples/go-model-download
 
 $(EXAMPLES_DIR): mkdir whisper modtidy
        @echo Build example $(notdir $@)
-       @C_INCLUDE_PATH=${INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} go build ${BUILD_FLAGS} -o ${BUILD_DIR}/$(notdir $@) ./$@
+       @C_INCLUDE_PATH=${INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} go build ${BUILD_FLAGS} -ldflags "-extldflags '$(EXT_LDFLAGS)'" -o ${BUILD_DIR}/$(notdir $@) ./$@

You might need cp ../../ggml-metal.metal .

For reference, my chatgpt discussion to overcome the problem: https://chat.openai.com/share/31b7f205-4c10-4cdf-a8ed-9ccbd6282dea

gleicon added a commit to gleicon/whisper.cpp that referenced this issue Nov 20, 2023
@gleicon
Copy link
Contributor

gleicon commented Nov 20, 2023

Thanks, that worked for me. I'm creating a PR and letting it fixed here in case anyone else needs it at #1530

@bobqianic bobqianic linked a pull request Nov 20, 2023 that will close this issue
ggerganov added a commit that referenced this issue Nov 22, 2023
* Fixed Makefile for MacOS ARM 64 based on #1344 + proper ggml-metal env var setting

* conditional to fix broken non-macos compilation

* spaces -> tab

* make : fix whitespaces

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
@c1ngular
Copy link

c1ngular commented Dec 8, 2023

i still got link error on Apple M1 Pro 14.1.1 (23B81) :

/usr/local/go/pkg/tool/darwin_arm64/link: running clang failed: exit status 1 ld: Undefined symbols: _MTLCopyAllDevices, referenced from: _ggml_metal_init in libwhisper.a[7](ggml-metal.o) _MTLCreateSystemDefaultDevice, referenced from: _ggml_metal_init in libwhisper.a[7](ggml-metal.o) _OBJC_CLASS_$_MTLComputePassDescriptor, referenced from: in libwhisper.a[7](ggml-metal.o) _OBJC_CLASS_$_NSBundle, referenced from: in libwhisper.a[7](ggml-metal.o) _OBJC_CLASS_$_NSProcessInfo, referenced from: in libwhisper.a[7](ggml-metal.o) _OBJC_CLASS_$_NSString, referenced from: in libwhisper.a[7](ggml-metal.o) clang: error: linker command failed with exit code 1 (use -v to see invocation)

i set the env to where .a and .h file located :
export C_INCLUDE_PATH="/Users/lijiangtv/whisper.cpp"
export LIBRARY_PATH="/Users/lijiangtv/whisper.cpp"

landtanin pushed a commit to landtanin/whisper.cpp that referenced this issue Dec 16, 2023
* Fixed Makefile for MacOS ARM 64 based on ggerganov#1344 + proper ggml-metal env var setting

* conditional to fix broken non-macos compilation

* spaces -> tab

* make : fix whitespaces

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
ryanrapp pushed a commit to ryanrapp/attentional-ios that referenced this issue Jan 9, 2024
* Fixed Makefile for MacOS ARM 64 based on ggerganov/whisper.cpp#1344 + proper ggml-metal env var setting

* conditional to fix broken non-macos compilation

* spaces -> tab

* make : fix whitespaces

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
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

Successfully merging a pull request may close this issue.

5 participants