Error building cgo: cannot parse $WORK/b046/_cgo_.o as ELF, Mach-O, PE or XCOFF #6

Closed
opened 2024-04-23 11:05:41 +00:00 by annie-elequin · 3 comments
annie-elequin commented 2024-04-23 11:05:41 +00:00 (Migrated from github.com)

Info
Macbook Pro M3Pro (2024)
Sonoma 14.4.1
Node: v21.7.3
Go: 1.22.2

I have added the example files, but when I try to build the go to create "example.node", I get this error:
github.com/akshayganeshen/napi-go: invalid flag in #cgo LDFLAGS: -Wl,-no_pie

I am new to go so I'm not quite sure how to fix this, any help would be greatly appreciated!

I've uploaded what I have to this repo:

https://github.com/annie-elequin/napi-go-test

Info Macbook Pro M3Pro (2024) Sonoma 14.4.1 Node: v21.7.3 Go: 1.22.2 I have added the example files, but when I try to build the go to create "example.node", I get this error: `github.com/akshayganeshen/napi-go: invalid flag in #cgo LDFLAGS: -Wl,-no_pie` I am new to go so I'm not quite sure how to fix this, any help would be greatly appreciated! I've uploaded what I have to this repo: https://github.com/annie-elequin/napi-go-test
annie-elequin commented 2024-04-24 11:02:22 +00:00 (Migrated from github.com)

Slight progress. Here's what I did...

  1. Fork this repo
  2. Comment out the flags (that didn't work)
  3. Put the flags back in

I then got a different error even though I hadn't changed anything.

New error:

# [github.com/Automattic/napi-go](http://github.com/Automattic/napi-go)

../../../go/pkg/mod/github.com/!automattic/napi-go@v0.0.0-20240423155444-8561b86b99a1/instance_data.go:5:10: fatal error: 'node/node_api.h' file not found
#include <node/node_api.h>
^~~~~~~~~~~~~~~~~
1 error generated.

Figured that out - it was because napi-go expects node to be at /usr/local/include/node and mine is under .nvm.

So I did this:

sudo ln -s /Users/<username>/.nvm/versions/node/v21.7.3/include/node /usr/local/include/node

(I also added node-gyp with these commands, and my simple binding.gyp file that does nothing is in the above linked repo)

  • yarn add -D node-gyp
  • then add a binding.gyp file
  • node-gyp configure (requires the binding.gyp file)
  • node-gyp build

SO NOW

I run this:

go build -buildmode=c-shared -o "example.node" .

and we have this error:

cgo: cannot parse $WORK/b046/_cgo_.o as ELF, Mach-O, PE or XCOFF

and I think I might post on stack overflow? I see some other issues of this online but I haven't been able to figure out the solution

Slight progress. Here's what I did... 1. Fork this repo 2. Comment out the flags (that didn't work) 3. Put the flags back in I then got a different error even though I hadn't changed anything. New error: ``` # [github.com/Automattic/napi-go](http://github.com/Automattic/napi-go) ../../../go/pkg/mod/github.com/!automattic/napi-go@v0.0.0-20240423155444-8561b86b99a1/instance_data.go:5:10: fatal error: 'node/node_api.h' file not found #include <node/node_api.h> ^~~~~~~~~~~~~~~~~ 1 error generated. ``` Figured that out - it was because napi-go expects node to be at `/usr/local/include/node` and mine is under `.nvm`. So I did this: `sudo ln -s /Users/<username>/.nvm/versions/node/v21.7.3/include/node /usr/local/include/node` (I also added node-gyp with these commands, and my simple binding.gyp file that does nothing is in the above linked repo) - `yarn add -D node-gyp` - then add a `binding.gyp` file - `node-gyp configure` (requires the binding.gyp file) - `node-gyp build` ### SO NOW I run this: `go build -buildmode=c-shared -o "example.node" .` and we have this error: `cgo: cannot parse $WORK/b046/_cgo_.o as ELF, Mach-O, PE or XCOFF` and I think I might post on stack overflow? I see [some other issues of this online](https://forum.golangbridge.org/t/error-cgo-cannot-parse-work-b003-cgo-o-as-elf-mach-o-pe-or-xcoff/30740/2) but I haven't been able to figure out the solution
ACenterA commented 2024-05-08 02:44:16 +00:00 (Migrated from github.com)

@annie-elequin are you on mac arm ?

We had a similar issue and we fixed it by editing the various node_api_...flags to adjust the -arch x86_64

Note: We also used go1.18 to make it work though, not sure if it works on 1.19+

@annie-elequin are you on mac arm ? We had a similar issue and we fixed it by editing the various node_api_...flags to adjust the `-arch x86_64` Note: We also used go1.18 to make it work though, not sure if it works on 1.19+
annie-elequin commented 2024-05-09 20:44:25 +00:00 (Migrated from github.com)

@ACenterA
yes that did it!
here are the changes to our flags that we made that worked:
image

@ACenterA yes that did it! here are the changes to our flags that we made that worked: <img width="354" alt="image" src="https://github.com/akshayganeshen/napi-go/assets/20155539/be076333-652b-4b5e-a7a9-e20969a1f915">
Sign in to join this conversation.
No description provided.