Difference between revisions of "Compiling on OpenWRT"

1,164 bytes added ,  10:36, 20 October 2020
m
no edit summary
m
m
 
Line 21: Line 21:
**NOTE: The original DD-WRT / Entware version is this: export LDFLAGS="-Wl,-rpath=/opt/lib -Wl,--dynamic-linker=/opt/lib/ld-linux.so.3 -L/opt/lib", and needs to be modified for OpenWRT as shown above.
**NOTE: The original DD-WRT / Entware version is this: export LDFLAGS="-Wl,-rpath=/opt/lib -Wl,--dynamic-linker=/opt/lib/ld-linux.so.3 -L/opt/lib", and needs to be modified for OpenWRT as shown above.
***-WI is a switch that allows for additional items to be added via the GNU [https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html LD] (https://stackoverflow.com/questions/6562403/i-dont-understand-wl-rpath-wl) command
***-WI is a switch that allows for additional items to be added via the GNU [https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html LD] (https://stackoverflow.com/questions/6562403/i-dont-understand-wl-rpath-wl) command
***-rpath in LDLFLAGS = "-rpath=/usr/lib:/opt/lib" is the same as executing this on the command line: export LD_LIBRARY_PATH = "/opt/lib
***-rpath in LDFLAGS = "-rpath=/usr/lib:/opt/lib" is the same as executing this on the command line: export LD_LIBRARY_PATH = "/opt/lib
****rpath AND/OR <code>LD_LIBRARY_PATH</code> is a path used by an executable program to search directories containing shared libraries after the program has been compiled (from https://en.wikipedia.org/wiki/Rpath and https://stackoverflow.com/questions/4250624/ld-library-path-vs-library-path)
****rpath AND/OR <code>LD_LIBRARY_PATH</code> is a path used by an executable program to search directories containing shared libraries after the program has been compiled (from https://en.wikipedia.org/wiki/Rpath and https://stackoverflow.com/questions/4250624/ld-library-path-vs-library-path)
****OpenWRT has its own equivalent path, which is /usr/lib, however there are some variations in files and sizes.  Solution?  Include both paths (from https://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_24.html);
****OpenWRT has its own equivalent path, which is /usr/lib, however there are some variations in files and sizes.  Solution?  Include both paths (from https://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_24.html);
Line 27: Line 27:
***--dynamic-linker
***--dynamic-linker
****For DD-WRT / Entware, the GNU LD file is located here: /opt/lib/ld-linux.so.3, which is a symbolic link to ld-2.27.so (and here too: /lib/gcc/arm-openwrt-linux-gnueabi/7.4.0)
****For DD-WRT / Entware, the GNU LD file is located here: /opt/lib/ld-linux.so.3, which is a symbolic link to ld-2.27.so (and here too: /lib/gcc/arm-openwrt-linux-gnueabi/7.4.0)
****OpenWRT seems to have an equivalent GNU LD file located here: /usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/ld-musl-armhf.so.1, so use that path instead
****OpenWRT seems to have an equivalent GNU LD file located here: /usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/ld-musl-armhf.so.1, so use that path instead (from https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/file-not-found-when-executing-assembled-program)
*****--dynamic-linker /usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/ld-musl-armhf.so.1
*****--dynamic-linker /usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/ld-musl-armhf.so.1
****LD is not the same as GNU LD (https://en.wikipedia.org/wiki/Linker_(computing)#GNU_linker<nowiki/>)
****LD is not the same as GNU LD (https://en.wikipedia.org/wiki/Linker_(computing)#GNU_linker<nowiki/>)
***<nowiki/>-L in LDLFLAGS = "-L/usr/lib:/opt/lib:/opt/include" is the same as executing this on the command line: export LIBRARY_PATH=/usr/lib:/opt/lib:/opt/include (https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html)
***<nowiki/>-L in LDFLAGS = "-L/usr/lib:/opt/lib:/opt/include" is the same as executing this on the command line: export LIBRARY_PATH=/usr/lib:/opt/lib:/opt/include (https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html)
****<code>LIBRARY_PATH</code> is used by gcc before compilation to search directories containing static and shared libraries that need to be linked to your program (from https://stackoverflow.com/questions/4250624/ld-library-path-vs-library-path)
****<code>LIBRARY_PATH</code> is used by gcc before compilation to search directories containing static and shared libraries that need to be linked to your program (from https://stackoverflow.com/questions/4250624/ld-library-path-vs-library-path)
*****
*****
Line 50: Line 50:
**Although they offer a link and a command for downloading additional libraries to /opt/include, there doesn't appear to be any notation of where that is utilized.  Just having it as a bunch of files in a directory is useless unless a compiler like GCC can make use of the files.  It should be
**Although they offer a link and a command for downloading additional libraries to /opt/include, there doesn't appear to be any notation of where that is utilized.  Just having it as a bunch of files in a directory is useless unless a compiler like GCC can make use of the files.  It should be
*ELF Definition: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
*ELF Definition: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
== Example ==
* Create a file named ex.sh: nano ex.sh
* Add the following directives (from the above section) and save it;
<syntaxhighlight lang="text">
export LDFLAGS="-Wl,-rpath=/usr/lib:/opt/lib -Wl,--dynamic-linker=/usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/ld-musl-armhf.so.1 -L/usr/lib:/opt/lib:/opt/include
export CFLAGS="-O2 -pipe -march=armv7-a -mtune=cortex-a9 -fno-caller-saves -mfloat-abi=soft -I/opt/include "
</syntaxhighlight>
* Create a "Hello World" source code file: nano hw.c
* Add the following text and save it;
<syntaxhighlight lang="text">
#include <stdio.h>   
int main(void)   
{
printf("\nHello, world!\n\n");       
return 0;       
}
</syntaxhighlight>The below two commands are from an OpenWRT example here: https://openwrt.org/docs/guide-developer/helloworld/chapter2
* Type this command: gcc -c -o hw.o hw.c -Wall
* Type this command: gcc -o hw hw.o
...and then run it: ./hw, which should produce the below output;<syntaxhighlight lang="text">
Hello, world!
</syntaxhighlight>