Undefined reference to popen

Hi,
I downloaded the riscv64-unknown-elf-gcc-8.2.0-2019.05.3-x86_64-linux-ubuntu14.tar.gz to compile codes which will call popen, but I always got failed when link, the riscv64-unknown-elf/bin/ld reported “undefined reference to popen” and “undefined reference to pclose”.

Can the latest RISC-V toolchain support popen/pclose?
What next can I do, do I have to build the cross-compiler for RISC-V by myself?

The -elf- toolchains are for embedded systems running on bare metal hardware. You don’t have pipes when you don’t have an OS on the target. Perhaps you want a linux toolchain? Or maybe an RTEMS toolchain? SiFive only distributes pre-built embedded elf toolchains.

If you want a linux toolchain, then if you have a recent Linux OS you can probably apt install one. Or you can try building one from sources. There are multiple ways to do that. You can use crosstool-ng (enable experimental features to get the risc-v support). You can use github.com/riscv/riscv-gnu-toolchain. You can use github.com/riscv/meta-riscv if you want OpenEmbedded/Yocto. Etc.

I would ask what you want a linux cross compiler for though. If you use a cross compiler that has a different glibc version than the target, then it likely won’t run unless you static link. So if you have a particular linux target in mind, then you need to choose a linux cross compiler that is compatible with that linux target. And if you do have a particular linux target in mind, it is likely much easier to boot that linux target and compile natively on the target than to use a cross compiler.

Hi Jim,
Thank you very much for quickly reply!

I have not installed any popular OS like Fedora, Debian on HiFive Unleashed, yet. It’s the first day I get the HiFive Unleashed board, currently I want to do some benchmark to measure the CPU performance. So I’m trying to cross-compile “nbench” now.

Is there any pre-built linux toolchain? Or do I have to build it from source codes?

In addition, by the way, do you have any suggestion for my intention that measuring the CPU performance?

The software that ships with the HiFive Unleashed is built from github.com/sifive/freedom-u-sdk from the legacy v1_0 branch. This is old software, pre-upstream glibc, kernel, buildroot, old binutils and gcc versions. Etc. You can get a cross toolchain if you check this out and build it. You will need to use this toolchain if you want to compile code that runs on the system as shipped. However, what you have is not a very useful system, as it is a minimal buildroot environment built from mostly pre-upstream sources. You can make it more useful by putting a real distro in the second sdcard partition and chrooting to it. If you use this toolchain, programs compiled with it won’t run on Fedora/Debian/OpenSuse/OpenEmbedded or any useful OS installed on the system. Well, they might if you static link and it is simple enough. But basically what you have now is not really a very useful system, and you should replace it with something useful if you want to do useful work on it. Once you get a useful system on the board you should use the compiler that come with that system and build natively. Note that the current github.com/sifive/freedom-u-sdk master branch can be used to get a much better system running on the board, using mostly upstream sources, and offers a debian rootfs, but it is rather different than what you have now, and you should not expect user binaries that run on the legacy v1_0 system to work on the current system. This also gets you a cross compiler you can use.

If you still want a cross compiler, and are using Debian/Fedora/etc, then I would suggest getting your system of choice running on the board, and then extract a sysroot from it (/usr/include, /usr/lib, /lib, etc) and use the sysroot to build a cross compiler. Different systems may have different glibc versions, and different glibc configurations, so this works best if you build a cross compiler using the target glibc via a sysroot.

Got it with many thanks!
I will try freedom-u-sdk.