Issue cross-compiling apr-1.5.2 for ARM - c

I am trying to cross compile Apache Portable Run-time library APR-1.5.2 lib for ARM platform. I am following below steps.
./configure --host=aarch64-unknown-linux-gnu CC=aarch64-unknown-linux-gnu-gcc
make
I am not getting any error in configure and make but when i try to link it to my code i am getting linking error.
#include <iostream>
#include <stdio.h>
using namespace std;
#include "apr_general.h"
#include "apr_network_io.h"
#include "apr_strings.h"
int main(){
apr_initialize();
std::cout<<"Welcome Program compiling "<<std::endl;
return 0;
}
When i am compiling the code using cross compiler getting error.
aarch64-unknown-linux-gnu-g++ -o Test -I ../../../../Static_APR/apr-1.5.2/include DAS.cpp ../../../../Static_APR/apr-1.5.2/.libs/libapr-1.a -lpthread
**apr-1.5.2/.libs/libapr-1.a(start.o): Relocations in generic ELF (EM: 62)**
Code compiles fine with g++.
g++ -o Test -I ../../../../Static_APR/apr-1.5.2/include DAS.cpp ../../../../Static_APR/apr-1.5.2/.libs/libapr-1.a -lpthread
Why APR lib didn't built for arm (cross compiler) even though i have used CC=aarch64-unknown-linux-gnu-gcc
Can anyone help me with correct way to build APR for cross compilation?

I am following below steps.
./configure --host=aarch64-unknown-linux-gnu CC=aarch64-unknown-linux-gnu-gcc
make
Your ./configure should include a --build. --host is the machine you are compiling for. Also see How To Configure for Android? on the Autoconf mailing list. Maybe something like:
export CPP=aarch64-unknown-linux-gnu-cpp
export CC=aarch64-unknown-linux-gnu-gcc
export CXX=aarch64-unknown-linux-gnu-g++
export LD=aarch64-unknown-linux-gnu-ld
export AR=aarch64-unknown-linux-gnu-ar
export AS=aarch64-unknown-linux-gnu-as
export RANLIB=aarch64-unknown-linux-gnu-ranlib
export CFLAGS="..."
export CXXFLAGS="..."
./configure --build=`config.guess` --host=aarch64-unknown-linux-gnu
The snippet above should ensure all the tools are available. You may need to add CFLAGS and CXXFLAGS with the appropriate header location; and an LDFLAGS with the appropriate library location.
You should verify the program for RANLIB. Its may not be what you think. For example, on one version of Ubuntu for ARM it is:
export RANLIB=aarch64-unknown-linux-gcc-ranlib-4.7
You may need to find config.guess:
$ find /usr -name 'config.guess'
/usr/lib/rpm/redhat/config.guess
/usr/share/automake-1.15/config.guess
...
Finally, you might find these scripts useful. They help build another library by setting paths and setting tools: setenv-android.sh and setenv-embedded.sh.

Related

How to use the hidapi library from Signal11?

I installed the hidapi library from Signall11 on my windows10 pc (using minGW). But now I'm having some trouble actually getting it to work with gcc. I have some main.c file in which I include the hidapi.h file. My gcc command looks like
gcc main.c
I'm not sure where I'm going wrong because whenever I try to run this command I get an undefined reference error to some function that is defined in the hidapi.h file.
A full compile command for a project using hidapi is like this:
gcc -o your_app your_app.c -lhidapi-hidraw
It's not enough to include #include "hidapi.h" in the C-code, which does let gcc compile. You also need -lhidapi-hidraw to link with the library. I.e. compiling is in fact a 2 step process.

Eclipse: unrecognized option '-pthread'

I'm new programming in C, now I'm trying to implement threads in a motor control program I am developing but my problem is that I can not run the pthread.h library in eclipse. despite haver installed POSIX threading library for Win32
Info: Internal Builder is used for build
gcc -O3 -g3 -Wall -pthread -c -fmessage-length=0 -o src\main.o ..\src\main.c
gcc: error: unrecognized option '-pthread'
Info: Parallel threads used: 1
Even though I'm just stating nothing but the library
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main(void) {
return EXIT_SUCCESS;
}
check forum several times and did what they wanted to establish in the -pthread miscellaneous and link phtread but I can not solve the problem, could someone please help me?
I have Windows 10, 64 bits.
If your code is not compiling, you need to add -pthread argument in your gcc compilation as follows.
Navigate to: Project -> Properties
On the left: c/c++ build -> GCC C Compiler -> Miscellaneous
Add -pthread argument at the beginning Other Flags section
Also pthread library add c/c++ build -> Settings -> GCC C Linker -> Libraries
And include pthread library into other libraries.
Click apply.
Clean and build the project.
If you have only Unresolved inclusion problem alone then specify the file system path for the pthread.h as your compiler does not know this.
Check here how to add pthread.h in your filesystem.
I gave up trying, resort to the use of counters card that I use (KL25Z) and events programming.

Compile error when using FFMPEG library

I have tried to compile a C program using FFMPEG but every time I compile it fails at the include statement:
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
Gcc provides this error message:
libavcodec/avcodec.h: No such file or directory
I have installed FFMPEG and created the shared libraries, but when I try to link those libraries when I compile I get the same error.
gcc main.c -L ffmpeg_build/lib -l ffmpeg_build/include/libavcodec/avcodec.h
Do I get this error because I am linking the library incorrectly, or is there some other issue?
This is the first time I am using someone else's library, so please excuse me if I am asking a silly question.
A lowercase -l is a linker option used to specify libraries. They might look like libsomething.a and becomes -lsomething in the linker invocation. In order to add a directory to the header search path, use a capital i, -I.
gcc main.c -L/some/path/ffmpeg_build/lib -I/some/path/ffmpeg_build/include

Linux : Glib was not found

I have a sample C project that use GLib Library. In that source code, it use :
#include <glib.h>
When I compile, I found this error : "Glib.h : no such file or folder". I have google and find out that I should install this lib. So I use those command:
apt-get install libgtk2.0-dev
apt-get install glade
After that, I have checked and see already exist this header file in my system: usr/include/glib-2.0/glib.h But when I compile, I still meet problem above.
So I have change include line to :
#include <glib-2.0/glib.h>
So, after that, when I compile, I meet error inside glib.h header :
#ifndef __G_LIB_H__
#define __G_LIB_H__
#define __GLIB_H_INSIDE__
#include <glib/galloca.h>
#include <glib/garray.h>
// more code here
glib/galloca.h : no such file or directory. Because this error is inside system header file, I cannot modify anymore and still cannot compile.
I don't know how to fix this. I have read some post, that they change makefile. But, because my project is compiled automatically by IDE (CodeBlock) and I cannot really write a makefile, so that method doesn't suitable for me.
Please tell me a way to fix this.
Thanks :)
There must be some problem with how you build. To compile C programs that use GLib, you need package libglib2.0-dev. You can either install it directly, or install libgtk2.0-dev, which pulls it in as a dependency. So you have the packages you need.
The correct way to compile a GLib program is to use -I with the path to the GLib include files.
An example (from How to compile a helloworld GLib program? on askubuntu):
gcc $(pkg-config --cflags --libs glib-2.0) hello_glib.c
This should let you compile this program:
#include <stdio.h>
#include <glib.h>
int main(int argc, char** argv) {
GList* list = NULL;
list = g_list_append(list, "Hello world!");
printf("The first item is '%s'\n", g_list_first(list)->data);
return 0;
}
The errors you are getting indicate that you are not setting the include path (-I) correctly. How to do this depends on your build system/IDE.
In Code::Blocks, you must set the include path and the linker options in the appropriate configuration dialog. Run pkg-config --cflags --libs glib-2.0, which will output something like
-I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -lglib-2.0
The directories after -I must be set in the compiler options of your project (should be under Project -> Build Options -> Search Directories), and the names after -l must be set in the linker settings. Another option is to create a Makefile, and let Code::Blocks use that.
See e.g. Q: What do I need to know when using 3rd party libs? in the Code::Blocks Wiki.
You should not alter your source code (e.g. the #include directives).
You just need to use pkg-config (both for compiling, with --cflags, and for linking, with --libs), preferably with a builder program like make.
This is an example for exactly your situation: a Makefile using pkg-config to compile some source program using glib

How to compile C code using NDK for Android Device (ARM)?

I need to perform the following steps:
write a simple counter that keeps resetting itself after overflow in C/C++
compile and push that code into the phone via ADB
run it as a regular executable in background via ADB shell
how to compile the above C code using NDK toolchain? I found a couple of similar links but none of them give simple and complete steps to do so.
If there is a link with complete steps please do refer me to the same.
Compile Environment: Ubuntu, compile should be done via console not any IDE
You're right, I made a mistake, I had not even tested it and gave me the
same error, is due to the entry point of the "main", as this has not
changed but I hope this works for you. Anyway check the symbol table "nm",
the real-time execution "strace", you can even use gdbserver.
#include <stdio.h>
int main (int argc, char *argv[])
{
printf ("hello world");
return 0;
}
export NDK_ROOT=your_ndk_path
export PATH=$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin:$PATH
export CC=arm-linux-androideabi-gcc
export LD=arm-linux-androideabi-ld
arm-linux-androideabi-gcc -I$NDK_ROOT/platforms/android-18/arch-arm/usr/include -Wl,-rpath-link=$NDK_ROOT/platforms/android-18/arch-arm/usr/lib -Wl,-L$NDK_ROOT/platforms/android-18/arch-arm/usr/lib -Wl,-lc -o test test.c
If ld return with erros like "... ld: error: cannot open... : No such file or directory"
try this for your losed files:
ln -s $NDK_ROOT/platforms/android-18/arch-arm/usr/lib/crtend_android.o
ln -s $NDK_ROOT/platforms/android-18/arch-arm/usr/lib/crtbegin_dynamic.o
I use gcc4.8 and android API-level18
usin this you can try:
First way using command line.
export NDK_ROOT=your_ndk_path
export PATH=$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin:$PATH
export CC=arm-linux-androideabi-gcc
export LD=arm-linux-androideabi-ld
export CPPFLAGS=-I$NDK_ROOT/platforms/android-18/arch-arm/usr/include
export CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$NDK_ROOT/platforms/android-18/arch-arm/usr/lib/ -L$NDK_ROOT/platforms/android-18/arch-arm/usr/lib"
export LIBS="-lc"
arm-linux-androideabi-gcc -nostdlib -o test test.c
If run "file test" you should see this:
test: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped
Second way using autotools.
export PATH=$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin:$PATH
./configure --host=arm-linux-androideabi CC=arm-linux-androideabi-gcc LD=arm-linux-androideabi-ld CPPFLAGS="-I$NDK_ROOT/platforms/android-18/arch-arm/usr/include" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$NDK_ROOT/platforms/android-18/arch-arm/usr/lib/ -L$NDK_ROOT/platforms/android-18/arch-arm/usr/lib" LIBS="-lc"
make
http://embelinux.blogspot.com/2013/09/autotools1-hola-mundo-la-autotools.html
Third way using android developers ndk full feature.
Read Android.mk file syntax specification. This document describes the
syntax of Android.mk build file written to describe your C and C++ source
files to the Android NDK.
http://www.kandroid.org/ndk/docs/ANDROID-MK.html
gdbserver is to debug an application running on the Android device and can
control gdb from the PC using a TCP connection. gdb (The GNU Debugger)
need the debugging symbols.
When you compile an application the compiler puts all the symbols defined
in something called as symbol table, the problem was not link to rtbegin_dynamic.o,
crtend_android.o that if you specify -nostdlib option the program is created but no work (Table Simbol empty)
nm (list symbols from object files) eg: nm test
gcc is not a compiler, it is a driver that controls the execution
of other applications that are what make the job
for example try invoque the compiler using -### as unique option for see details
LDFLAGS: are the flags for the linker
CFLAGS: are the flags to the compiler (not links)
Anyway if you are new to the compilation, even in cross compiling strongly
recommend you use the Android.mk way
Sorry for my English ;)
#Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#module name
LOCAL_MODULE := test
#src
LOCAL_SRC_FILES := test.c
#build executable
include $(BUILD_EXECUTABLE)
export PATH=path_to_ndk_root:$PATH
export NDK_PROJECT_PATH=.
ndk-build APP_BUILD_SCRIPT=Android.mk

Resources