Error with x86_64-elf-g++

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
acer
Posts: 5
Joined: Sat Dec 21, 2013 4:23 pm

Error with x86_64-elf-g++

Post by acer »

I am trying to compile a hello world kernel:

Code: Select all

x86_64-elf-g++ -c kernel.cpp -o kernel.o -ffreestanding -O2 -Wall -Wextra -fno-exceptions -fno-rtti
However it gives me this error message:

Code: Select all

/home/tjex/GCC_Cross-Compilers/x86_64-elf-4.8.1/bin/../libexec/gcc/x86_64-elf/4.8.1/cc1plus: error while loading shared libraries: libmpc.so.2: cannot open shared object file: No such file or directory
I searched up on it and it has to do with changing the environment variable LD_LIBRARY_PATH so it can find libmpc.so.2, but that file doesn't exist anywhere in the gcc directory.

What should I do?
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Error with x86_64-elf-g++

Post by Nable »

Something has changed in your setup since you've compiled your crosscompiler.
Verify that you have all dependencies and rebuild it.
Even if you didn't build it yourselves and just downloaded binary version, then most probably you also need all the required libraries (gmp, mpfr, mpc).

You can also try looking for missing libs in /usr/local or even /usr and add their paths to LD_LIBRARY_PATH environment variable.
acer
Posts: 5
Joined: Sat Dec 21, 2013 4:23 pm

Re: Error with x86_64-elf-g++

Post by acer »

Nable,

I've checked and I have all the required packages. The libmpc.so.2 file does not exist anywhere on the filesystem. Should I reinstall those packages?

Also the LD_LIBRARY_PATH is empty.
acer
Posts: 5
Joined: Sat Dec 21, 2013 4:23 pm

Re: Error with x86_64-elf-g++

Post by acer »

Update: I checked in the /usr/lib64 directory and there IS a libmpc.so.3 file. I'm pretty sure this is what I'm looking for, but how do I get gcc to recognize it?
User avatar
xenos
Member
Member
Posts: 1144
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
GitHub: https://github.com/xenos1984
Contact:

Re: Error with x86_64-elf-g++

Post by xenos »

It seems that you updated MPC since you compiled your cross compiler. The cleanest way would probably to re-compile the cross compiler.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
acer
Posts: 5
Joined: Sat Dec 21, 2013 4:23 pm

Re: Error with x86_64-elf-g++

Post by acer »

Just so you know: I did not compile this version of gcc, I downloaded it from the GCC Cross Compiler page. I had problems with compiling before and this was much easier.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Error with x86_64-elf-g++

Post by Brynet-Inc »

acer wrote:Just so you know: I did not compile this version of gcc, I downloaded it from the GCC Cross Compiler page. I had problems with compiling before and this was much easier.
Clearly whoever compiled those binaries did so against their hosts systems mpc/mpfr libraries. That is the preferred way, but ideally you wouldn't be distributing the resulting binaries.

So, either install compatible versions of those libraries in the appropriate place.. or follow the tutorial on the Wiki and compile it all yourself.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
sortie
Member
Member
Posts: 935
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Error with x86_64-elf-g++

Post by sortie »

Hi,

You are making the crucial mistake of being lazy and not following instructions. I'm assuming you are new to OS development, which means that failing to follow instructions will lead you into trouble like this countless times. I'm assuming you are following the Bare_Bones tutorial on the wiki. Allow me to point out some of the errors you've made because you were lazy and didn't read the instructions and assumed things you didn't know.

The first mistake you made was failing to give crucial information when you asked for help, such that you didn't build the toolchain yourself. At first your problem looked like something entirely else than it really was.

The second mistake you made was being lazy and not compiling your own cross-compiler, which is a valuable experience that will help you further in your osdev career. Instead, you decided to download the the unofficial prebuilt toolchain that explicitly stated that they were not supported or they might not even work.

The third mistake you made was not following the instructions from the Bare Bones tutorial that told you to get a toolchain that targets 'i586-elf', but instead you downloaded a toolchain that targets 'x86_64-elf'. I'm assuming you are using a x86_64-linux-gnu system and you thought the 'x86_64-elf' toolchain was the single one that ran on your 'x86_64-linux-gnu' system, but the desired 'i686-elf' toolchain you really wanted (i586 vs. i686 doesn't really matter for most purposes) also runs on your 'x86_64-linux-gnu' system. The result is that you are using a compiler that produces the wrong kind of machine code (x86_64) instead of the 32-bit x86 code that the Bare Bones tutorial boots using GRUB, i.e. your kernel will never boot with this configuration.

The fourth mistake you made was when your unsupported toolchain broke (failed to find a dependency), you didn't realize the toolchain was unsupported and that you should not have been lazy and just built the i586-elf toolchain yourself.

The fifth mistake you made was substituting in x86_64-elf-g++ instead of the i586-elf-g++ stated in the Bare Bones tutorial. This will produce you a 64-bit multiboot kernel, however GRUB and multiboot are buggy and you cannot boot such a kernel with GRUB (and because x86_64 requires paging on, I'm not even sure it can be theoretically done with multiboot being silly). If you tried to boot the kernel, you would get a weird error message and we'd likely be having another such forum topic because you picked the wrong compiler target. You can boot x86_64 kernels with multiboot, however, but it requires a few trick that are not covered in the Bare Bones tutorial. I don't recommend doing a x86_64 kernel until you get some basic experience with osdev work and feel confident understanding the necessary methods to boot such a kernel (you'll have to enter 64-bit mode manually and trick GRUB into loading the 64-bit code as if it was 32-bit code). If, however, you are applying such tricks and feel confident in the process, please mentally optimize away all my mentions of you using the wrong target.

I realize that the Bare Bones tutorial doesn't explicitly enough tell you which target to use, I'll be sure to improve it in a minute to help others not make the same mistake you made.

So, to sum up, my advise is to not be lazy and take the time to learn and appreciate building your own cross-compiler and to take the time to carefully follow instructions. You should also not download and run randomly built programs, as they could easily contained malicious code, and might not even work. After all, the time spent building a cross-compiler is nothing compared to all the time you'll spend having fun coding and debugging your operating system.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: Error with x86_64-elf-g++

Post by Antti »

It would be good to run tools on a different architecture than the target. It would make sure the cross-compiler is used. Host-specific things would not "leak" so easily.
User avatar
sortie
Member
Member
Posts: 935
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Error with x86_64-elf-g++

Post by sortie »

Antti wrote:It would be good to run tools on a different architecture than the target. It would make sure the cross-compiler is used. Host-specific things would not "leak" so easily.
Uh, that's not what's happening here. Indeed, actually since he is on x86_64-linux-gnu and the Bare Bones needs something like i586-elf, the architectures are already distinct. Besides, such leaks will never happen if it uses a cross-compiler. The only way it could happen was if you used the system gcc or g++ accidentally, but you'd have to screw up your build system considerably for that to happen.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: Error with x86_64-elf-g++

Post by Antti »

My previous post was a general notion about this. You are right. There is still a problem: you have written about the importance of using a cross-compiler numerous times and you will need to do that again in the future. As long as the host compiler works somehow (using the same CPU architecture and executable format), there are beginners using it directly. If it not worked at all (being totally different), we would not have this problem.
Post Reply