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

MacOS arm64 toolchain - C++ link error: (.text._getentropy_r+0x12): undefined reference to `_getentropy' #37

Open
MarcoBonino opened this issue Oct 11, 2024 · 5 comments

Comments

@MarcoBonino
Copy link

Hello!

I'm trying to use the toolchain 14.2.0 (also tried with 13.2.0). I'm always getting the same link error when my code use some c++ std things (like cout or vector).

I downloaded the toolchain but also tried to install it via package manager. Same error.

See the example:

$ cat main.cpp                                                                                                                                                                                                                               
#include <iostream>

int main() {
  std::cout << "Hello!" << std::endl;
  return 0;
}

Compilation:

$ /Users/makko/Library/xPacks/@xpack-dev-tools/riscv-none-elf-gcc/14.2.0-2.1/.content/bin/riscv-none-elf-g++ -o hello main.cpp 
/Users/makko/Library/xPacks/@xpack-dev-tools/riscv-none-elf-gcc/14.2.0-2.1/.content/bin/../lib/gcc/riscv-none-elf/14.2.0/../../../../riscv-none-elf/bin/ld: /Users/makko/Library/xPacks/@xpack-dev-tools/riscv-none-elf-gcc/14.2.0-2.1/.content/bin/../lib/gcc/riscv-none-elf/14.2.0/../../../../riscv-none-elf/lib/libc.a(libc_a-getentropyr.o): in function `_getentropy_r':
(.text._getentropy_r+0x12): undefined reference to `_getentropy'
collect2: error: ld returned 1 exit status

Am I doing something wrong ? Could you help on this ?

Thanks!

@ilg-ul
Copy link
Contributor

ilg-ul commented Oct 11, 2024

Assuming the link passed, what do you plan to do with the resulting binary?

@MarcoBonino
Copy link
Author

Assuming the link passed, what do you plan to do with the resulting binary?

Run it! For now it will be on simulators, like spike, qemu...

@ilg-ul
Copy link
Contributor

ilg-ul commented Oct 11, 2024

Run it! For now it will be on simulators, like spike, qemu...

And how do you expect the output stream resulting from writing to cout to reach you?

@MarcoBonino
Copy link
Author

Run it! For now it will be on simulators, like spike, qemu...

And how do you expect the output stream resulting from writing to cout to reach you?

Not sure how it works...
I was using riscv64-unknown-elf-g++ from https://github.com/riscv-software-src/homebrew-riscv/blob/HEAD/riscv-gnu-toolchain.rb before. That was able to compile and I could run and see the print using "spike pk my_binary".

In general I would like to link all statically in the final binary, so that It should run without extra libraries (if I'm correct).

@ilg-ul
Copy link
Contributor

ilg-ul commented Oct 11, 2024

I could run and see the print using "spike pk my_binary".

That binary worked because that toolchain is specific to spike.

Generally you have to add code to redirect the output to something your environment supports, like semihosting, which is supported well by QEMU.

But things are not that obvious as you may think, you have to define a startup, linker scripts, etc.

You can try to instantiate a project from my hello-world-qemu-template-xpack project template and take a look at the resulting code.

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