Fedora Linux Support Community & Resources Center
  #1  
Old 5th April 2017, 04:13 PM
stratacast1 Offline
Registered User
 
Join Date: Apr 2014
Location: Washington
Posts: 271
linuxfirefox
cross compiling with gcc help

I need to cross compile some code for the arm architecture for an assignment and I'm having troubles getting it done. I know I'm missing some cross-compile dependencies so I need some help knowing which one to get. The code is for a Raspberry Pi. Once I get the right dependencies, will this be the right command?

gcc -march=arm -o mycode mycode.c
Reply With Quote
  #2  
Old 6th April 2017, 06:51 AM
nobody Offline
Banned
 
Join Date: Mar 2012
Location: München, Deutschland
Posts: 863
windows_98_nt_2000chrome
Talking Re: cross compiling with gcc help

Hello kid,

Here is what I did to try to help you: wrote comprehensive one-line C example: void main(void) {printf ("Hello World\n");}

And here is what I am getting on my Fedora 25:

Code:
[user@localhost c-examples]$ uname -r
4.10.8-200.fc25.x86_64
[user@localhost c-examples]$ gcc --version
gcc (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[user@localhost c-examples]$ cat hw.c
void main(void) {printf ("Hello World\n");}
[user@localhost c-examples]$ gcc hw.c
hw.c: In function ‘main’:
hw.c:1:18: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
 void main(void) {printf ("Hello World\n");}
                  ^~~~~~
hw.c:1:18: warning: incompatible implicit declaration of built-in function ‘printf’
hw.c:1:18: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
[user@localhost c-examples]$ ./a.out
Hello World
[user@localhost c-examples]$ gcc -march=armv8-a hw.c
hw.c:1:0: error: bad value (armv8-a) for -march= switch
 void main(void) {printf ("Hello World\n");}
 
[user@localhost c-examples]$
For some reason, none of the switches in: https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html work!

For you also to read: http://raspberrypi.stackexchange.com...n-raspberry-pi

Maybe somebody else could chime in?!

_nobody_
Reply With Quote
  #3  
Old 6th April 2017, 11:52 AM
HaydnH Offline
Registered User
 
Join Date: Feb 2005
Location: London, UK
Posts: 471
windows_7chrome
Re: cross compiling with gcc help

I haven't done any cross compiling for about 10 years, I used to do a lot of compiling for a ARM based NAS box but these days there seem to be packages already compiled around. However...

To cross compile you'd need a full toolchain, you can't just ask gcc to compile for arm out of the box. There are toolchains prebuilt and also tools to build your own, see here:

http://elinux.org/Toolchains

I believe Fedora has cross compilers in the repos, look for binutils-arm-linux-gnu gcc-arm-linux-gnu or similar, hopefully they'dd give you all the dependencies tyou need.

The gcc command will depend on what you're compiling, e.g: if what you're compiling needs additional libraries then they will need to be cross compiled first and the gcc command would need to know where to find them. Usually ./configure <options> would handle the locations if the package has a configure script.

I would've thought the gcc command would be more specific about the target environment though? Something like: arm-linux-gnueabi-gcc?
Reply With Quote
  #4  
Old 6th April 2017, 12:25 PM
nobody Offline
Banned
 
Join Date: Mar 2012
Location: München, Deutschland
Posts: 863
windows_98_nt_2000chrome
Talking Re: cross compiling with gcc help

Even better "C", which is more complicated! int main(void) {return 111;}

Here it is (with asm produced):

Code:
user@localhost c-examples]$ cat example.c
int main(void) {return 111;}
[user@localhost c-examples]$ gcc example.c
[user@localhost c-examples]$ ./a.out
[user@localhost c-examples]$ echo $?
111
[user@localhost c-examples]$ gcc -S example.c
[user@localhost c-examples]$ cat example.s
	.file	"example.c"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$111, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 6.3.1 20161221 (Red Hat 6.3.1-1)"
	.section	.note.GNU-stack,"",@progbits
[user@localhost c-examples]$


_nobody_
Reply With Quote
  #5  
Old 7th April 2017, 12:56 AM
gordon64
Guest
 
Posts: n/a
linuxfirefox
Re: cross compiling with gcc help

I will chime in but don't have a RPi and I am not a coder

the maintainer of the RPi distro for Tinycore compiles natively exept for the kernel

Quote:
Spent few weeks with cross compilation in the beginning but it always failed for some reason
ref
http://forum.tinycorelinux.net/index...mcH3#msg115651
Reply With Quote
  #6  
Old 7th April 2017, 02:34 AM
nobody Offline
Banned
 
Join Date: Mar 2012
Location: München, Deutschland
Posts: 863
windows_98_nt_2000chrome
Talking Re: cross compiling with gcc help

Quote:
Originally Posted by gordon64 View Post
Ah, I see... Gordon! What you have suggested here is to use RPi as a development platform, as well as a target system.

Quote:
Offline bmarkus
Administrator
Hero Member
*****

Posts: 6840
My Community Forum

Re: Cross Compile Toolchain (for building outside TC)
« Reply #3 on: September 18, 2015, 12:33:15 AM »
Regarding ARM, I'm cross compiling only the kernel for piCore, everything is native. Spent few weeks with cross compilation in the beginning but it always failed for some reason so finally I dropped. Now with 4 CPU cores and 1GB RAM it is smooth to make a native build even of larger packages like Qt, mono, etc.

BTW, Raspbian was always built on ARM machine. Not Raspberry Pi, but on a more powerful ARM system.

Cross compilation also makes your dev procedure more complicated and time consuming, slowing you down and generating unnecessary task and bugs to fix. It is useful only in the very beginning to port to a new architecture or when target system has limited resources.

In TC you can setup the same dev environment as on any other LINUX system.
_______

Gitarist,

why you simply do not install Fedora 25 on RPi?
https://opensource.com/article/17/3/...n-raspberry-pi
https://fedoraproject.org/wiki/Raspberry_Pi

_nobody_
Reply With Quote
  #7  
Old 7th April 2017, 08:33 AM
gordon64
Guest
 
Posts: n/a
linuxfirefox
Re: cross compiling with gcc help

yes.....as Bela Markus has been the maintainer of the TC distro for RPi he should know what he is talking about it.
I lack those skills but recognise gurus when I see em.
Reply With Quote
  #8  
Old 12th April 2017, 06:03 AM
stratacast1 Offline
Registered User
 
Join Date: Apr 2014
Location: Washington
Posts: 271
linuxfirefox
Re: cross compiling with gcc help

I gave it a try again after finding the right (I think) dependencies. I got binutils-arm-linux-gnu gcc-arm-linux-gnu, which, I had before and ran this command below and got this output:

arm-linux-gnu-gcc test_linux.c
test_linux.c:1:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.

I also tried it again by adding the -march argument, and got something a little different:

arm-linux-gnu-gcc -march=armv7 test_linux.c
test_linux.c:1:0: error: target CPU does not support ARM mode
#include <stdio.h>

So perhaps these are the wrong libraries?
Reply With Quote
  #9  
Old 12th April 2017, 08:20 AM
nobody Offline
Banned
 
Join Date: Mar 2012
Location: München, Deutschland
Posts: 863
windows_98_nt_2000chrome
Wink Re: cross compiling with gcc help

Kid gitarist,

My two cent 15 minutes tryouts (transcript follows):

Code:
[user@localhost local]$ su -m
Password: 
[root@localhost local]# dnf install glibc-arm-linux-gnu-2.24-2.fc25.noarch
Last metadata expiration check: 0:33:06 ago on Wed Apr 12 08:42:27 2017.
Package glibc-arm-linux-gnu-2.24-2.fc25.noarch is already installed, skipping.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost local]# dnf install nacl-arm-newlib-2.1.0-3.git373135e.fc25.x86_64
Last metadata expiration check: 0:33:26 ago on Wed Apr 12 08:42:27 2017.
Package nacl-arm-newlib-2.1.0-3.git373135e.fc25.x86_64 is already installed, skipping.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost local]# dnf install binutils-arm-linux-gnu gcc-arm-linux-gnu
Last metadata expiration check: 0:34:00 ago on Wed Apr 12 08:42:27 2017.
Package binutils-arm-linux-gnu-2.27-3.fc25.x86_64 is already installed, skipping.
Package gcc-arm-linux-gnu-6.1.1-2.fc25.x86_64 is already installed, skipping.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost local]# dnf install arm-none-eabi-newlib
Last metadata expiration check: 0:34:46 ago on Wed Apr 12 08:42:27 2017.
Package arm-none-eabi-newlib-2.4.0-9.fc25.noarch is already installed, skipping.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost local]# exit
exit
[user@localhost local]$ cd ~
[user@localhost ~]$ cd ./projects1/asm-examples/rdrand/
[user@localhost rdrand]$ arm-linux-gnu-gcc test1.c
/usr/bin/arm-linux-gnu-ld: cannot find crt1.o: No such file or directory
/usr/bin/arm-linux-gnu-ld: cannot find crti.o: No such file or directory
/usr/bin/arm-linux-gnu-ld: cannot find -lc
/usr/bin/arm-linux-gnu-ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
[user@localhost rdrand]$ arm-linux-gnu-gcc -I/usr/local/target/include test1.c -L/usr/local/target/lib -lc
/usr/bin/arm-linux-gnu-ld: cannot find crt1.o: No such file or directory
/usr/bin/arm-linux-gnu-ld: cannot find crti.o: No such file or directory
/usr/bin/arm-linux-gnu-ld: cannot find -lc
/usr/bin/arm-linux-gnu-ld: cannot find -lc
/usr/bin/arm-linux-gnu-ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
[user@localhost rdrand]$
Sorry, kid...

But it might help ya!

_nobody_
Reply With Quote
  #10  
Old 12th April 2017, 12:04 PM
Jean Pierre Offline
Registered User
 
Join Date: Feb 2011
Posts: 169
linuxfirefox
Re: cross compiling with gcc help

Quote:
arm-linux-gnu-gcc test_linux.c
test_linux.c:1:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.
This means you have not installed (or not defined the path to) the standard headers for your target OS on ARM (which may be Linux or Android or other).
Quote:
[root@localhost local]# dnf install glibc-arm-linux-gnu-2.24-2.fc25.noarch
This installs the Fedora glibc library needed for executing the cross-compiler on Fedora. You need it, but...

What you also need are the headers and library for your target OS on ARM, which is unlikely to be Fedora. You have to get them from your target vendor, or copy them from your target system if present there.
Reply With Quote
  #11  
Old 13th April 2017, 07:59 AM
nobody Offline
Banned
 
Join Date: Mar 2012
Location: München, Deutschland
Posts: 863
windows_98_nt_2000chrome
Question Re: cross compiling with gcc help

Quote:
Originally Posted by Jean Pierre View Post
This installs the Fedora glibc library needed for executing the cross-compiler on Fedora. You need it, but...

What you also need are the headers and library for your target OS on ARM, which is unlikely to be Fedora. You have to get them from your target vendor, or copy them from your target system if present there.
CLI transcript follows:

Code:
[root@localhost ~]# dnf install glibc-arm-linux-gnu-devel.noarch 
Last metadata expiration check: 0:05:39 ago on Thu Apr 13 08:40:54 2017.
Dependencies resolved.
=================================================================================================
 Package                           Arch           Version                  Repository       Size
=================================================================================================
Installing:
 glibc-arm-linux-gnu-devel         noarch         2.24-2.fc25              fedora          7.4 M
 kernel-cross-headers              x86_64         4.10.8-200.fc25          updates         5.0 M

Transaction Summary
=================================================================================================
Install  2 Packages

Total download size: 12 M
Installed size: 62 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): glibc-arm-linux-gnu-devel-2.24-2.fc25.noarch.rpm          3.2 MB/s | 7.4 MB     00:02    
(2/2): kernel-cross-headers-4.10.8-200.fc25.x86_64.rpm           2.0 MB/s | 5.0 MB     00:02    
-------------------------------------------------------------------------------------------------
Total                                                            2.5 MB/s |  12 MB     00:04     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Installing  : kernel-cross-headers-4.10.8-200.fc25.x86_64                                  1/2 
  Installing  : glibc-arm-linux-gnu-devel-2.24-2.fc25.noarch                                 2/2 
  Verifying   : glibc-arm-linux-gnu-devel-2.24-2.fc25.noarch                                 1/2 
  Verifying   : kernel-cross-headers-4.10.8-200.fc25.x86_64                                  2/2 

Installed:
  glibc-arm-linux-gnu-devel.noarch 2.24-2.fc25    kernel-cross-headers.x86_64 4.10.8-200.fc25   

Complete!
[root@localhost ~]# exit
exit
[user@localhost ~]$ cd projects1/asm-examples/rdrand
[user@localhost rdrand]$ arm-linux-gnu-gcc test1.c
/usr/bin/arm-linux-gnu-ld: cannot find crt1.o: No such file or directory
/usr/bin/arm-linux-gnu-ld: cannot find crti.o: No such file or directory
/usr/bin/arm-linux-gnu-ld: cannot find -lc
/usr/bin/arm-linux-gnu-ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
[user@localhost rdrand]$ arm-linux-gnu-gcc -I/usr/local/target/include test1.c -L/usr/local/target/lib -lc
/usr/bin/arm-linux-gnu-ld: cannot find crt1.o: No such file or directory
/usr/bin/arm-linux-gnu-ld: cannot find crti.o: No such file or directory
/usr/bin/arm-linux-gnu-ld: cannot find -lc
/usr/bin/arm-linux-gnu-ld: cannot find -lc
/usr/bin/arm-linux-gnu-ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
[user@localhost rdrand]$


_nobody_
Reply With Quote
  #12  
Old 13th April 2017, 09:53 AM
Jean Pierre Offline
Registered User
 
Join Date: Feb 2011
Posts: 169
linuxfirefox
Re: cross compiling with gcc help

Quote:
Installing : kernel-cross-headers-4.10.8-200.fc25.x86_64
These are kernel headers for Fedora 25 on ARM. They are useful if you want to compile a Fedora 25 driver on ARM, but so far the OP has not mentioned which OS is being run on the target configuration. This is definitely bad if running Android.
Quote:
Installing : glibc-arm-linux-gnu-devel-2.24-2.fc25.noarch
This installs the glibc headers for glibc-2.24 on ARM. This is only good if the ARM configuration is running glibc-2.24 for Linux.
Quote:
/usr/bin/arm-linux-gnu-ld: cannot find crt1.o: No such file or directory
This means you have not installed the glibc library for ARM, and if you have compiled with glibc-2.24 headers, you need the matching library for ARM.
If this is what you really need, you can make a try with the Debian repository : https://packages.debian.org/sid/armh...6-dev/download
Extract the contents by :
Code:
ar x libc6-dev_2.24-10_armhf.deb
xz -d data.tar.xz
You get all you need in data.tar put the files into some directory and designate this directory on the command line for gcc and ld. Once again, this might not be what the OP needs.
Reply With Quote
  #13  
Old 21st April 2017, 12:44 AM
stratacast1 Offline
Registered User
 
Join Date: Apr 2014
Location: Washington
Posts: 271
linuxfirefox
Re: cross compiling with gcc help

My target OS is Raspbian, but my current OS is Fedora 25, sorry for the late response to this, I didn't get a notification. So in essence, I would need to have Debian 8 x86, and then get all the proper cross compile software for ARM, then cross compile to ARM which would then work on Raspbian because they have the same libraries and version, correct?
Reply With Quote
  #14  
Old 21st April 2017, 08:14 AM
Jean Pierre Offline
Registered User
 
Join Date: Feb 2011
Posts: 169
linuxfirefox
Re: cross compiling with gcc help

Quote:
So in essence, I would need to have Debian 8 x86
No, you need not. You can use arm-linux-gnu-gcc and associated tools which you have already installed on Fedora (you would use them the same way on Debian).
You have to understand you need two sets of headers and libraries : one for executing the compiler on Fedora (they are provided by Fedora), and one for generating executables runnable on the target system (they are provided by the target system packager). You have a different CPU model for host and target, hence the two sets of libraries have to be kept separately (and it is safer to also keep separate headers).
Quote:
My target OS is Raspbian
So you need to know which version of gnu lib is being used on Raspian. Just do (on Raspbian) :
Code:
dpkg -l libc6
This will output something like :
Code:
libc6:armhf    2.19-18+deb8 armhf        GNU C Library: Shared libraries
This tells you need the development package for libc6 2.19-18 for armhf, which is named like lib6-dev-2.19-18 (note the -dev appended to libc6). You can get it from Debian, as I explained in message #16, though your own version may be different. Extract it in a specific directory (be sure not to overwrite the Fedora ones). To use the headers, designate the headers sub-directory in option -I of arm-linux-gnu-gcc, to use the libraries, designate the libraries sub-directory in option -L of arm-linux-gnu-ld.

This should be all you need for a basic test (like hello.c), but you may need more headers and libraries for your target application.
Reply With Quote
  #15  
Old 21st April 2017, 09:03 AM
stratacast1 Offline
Registered User
 
Join Date: Apr 2014
Location: Washington
Posts: 271
linuxfirefox
Re: cross compiling with gcc help

Cool! Thank you so much for the information! I'll try to follow your instructions hopefully Saturday or early next week and report back
Reply With Quote
Reply

Tags
compiling, cross, gcc

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
bad ELF interpreter when cross-compiling bob p Using Fedora 2 5th March 2015 06:21 PM
Cross compiling userspace applications for ARM john_erlandsson Programming & Packaging 2 7th January 2014 04:33 PM
cross compiling an appliction badabagni Installation, Upgrades and Live Media 1 12th September 2013 01:17 PM
Cross Compiling with Mock ImNtReal Using Fedora 0 18th September 2008 03:38 AM
Cross-compiling possible? Mat Using Fedora 1 6th July 2005 05:03 PM


Current GMT-time: 14:43 (Saturday, 23-09-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat