Cross-compiled library not found by toolchain - c

I'm new to developing for embedded systems, but I have installed arm-linux-gnueabi-gcc via the Linux Mint package manager and managed to build a few programs successfully.
I'm currently struggling with getting a program to compile using libusb. I've done the following:
Downloaded and unpacked the libusb 1.0.20 sources from https://sourceforge.net/projects/libusb/.
Compiled and installed them using the following commands:
~/Downloads/libusb-1.0.20 $ ./configure --host=arm-linux-gnueabi --prefix=/opt/ --disable-udev
~/Downloads/libusb-1.0.20 $ sudo make
~/Downloads/libusb-1.0.20 $ sudo make install
(The reason for sudo-ing the make commands was because I encountered permission problems related to removing old files.)
Copied a small sample file from somewhere on the internet:
#include <libusb-1.0/libusb.h>
#include <stdio.h>
int main()
{
int i=0;
libusb_context **c = NULL;
i = libusb_init(c);
printf("\nusing libusb.h\n");
return 0;
}
Tried to build it and run it with gcc:
~/Desktop/libtest $ gcc libtest1.c -o libtest1 -lusb-1.0
~/Desktop/libtest $ ./libtest1
using libusb.h
However, when I try to do the same with arm-linux-gnueabi-gcc, it can't find the library:
~/Desktop/libtest $ arm-linux-gnueabi-gcc libtest1.c -o libtest1 -lusb-1.0
/usr/lib/gcc-cross/arm-linux-gnueabi/4.7/../../../../arm-linux-gnueabi/bin/ld: cannot find -lusb-1.0
collect2: error: ld returned 1 exit status
Where did I go wrong? Is there something else I need to do in order to use the library? Did I fail at compiling the library for the arm compiler? I didn't include the compiler output here since it's quite long, but there are no obvious errors. This might be a very stupid question, but I'm completely clueless.

Related

Header not found error when adding stdint.h to my C code when compiling with Clang on Raspberry Pi

Here's the error that I am getting and none of the online solutions are effectively fixing the issues that I am having. Just adding #include <stdint.h> breaks the compilation of my code. I tried installing multilib but the library seems to have no support on Ubuntu. I also tried some of the compatibility libraries but to no avail.
clang -O2 -target bpf -c hello.c -o hello.o
In file included from hello.c:2:
In file included from /usr/lib/llvm-11/lib/clang/11.0.0/include/stdint.h:52:
/usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
#include <bits/libc-header-start.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
The code for reference. I am trying to run a compiled version of uBPF on an ARM device
#include <stdint.h>
static int idouble(int a)
{
return (a * 2);
}
int bpf_prog(void *ctx)
{
int a = 1;
a = idouble(a);
return (a);
}
This question is based on this Klyr's tutorial on how to setup user input with uBPF.
I was able to have my issue fixed actually. ARM does not have a gcc-multilib equivalent so you must install gcc-multilib-arm-linux-gnueabihf for this to work. When trying to compile and target with Clang, you must first
cd /usr/include/aarch64-linux-gnu
then
sudo cp -r . ..
It's a hacky solution but it will let you import libraries as you see fit

Unable to run windows binary compiled with clang/lld/mingw

I have downloaded and installed clang on windows 10 from http://releases.llvm.org/download.html
and mingw from https://sourceforge.net/projects/mingw-w64/
I am trying to compile a very basic C program using clang/lld/mingw:
int main(int argc, char* argv[argc + 1])
{
return 0;
}
To compile I invoke:
clang.exe -target x86_64-windows-gnu -fuse-ld=lld.exe -g -gcodeview -Wl,/debug,/pdb:example.pdb example.c -o example.exe
This creates an exe which faults on startup in mainCRTStartup (__security_init_cookie to be precise).
However, running with default ld from binutils is successful:
clang.exe -target x86_64-windows-gnu example.c -o example.exe
Please note that I wish to use mingw headers, not msvc.
In total I tried:
x86_64-8.1.0-posix-seh-rt_v6-rev0
x86_64-7.3.0-posix-seh-rt_v5-rev0
x86_64-8.1.0-win32-seh-rt_v6-rev0
x86_64-8.1.0-win32-sjlj-rt_v6-rev0
without any luck producing a functional program.
So I am wondering, is there something obvious I am doing wrong here?
EDIT:
I have also tried with msys2 to no avail. Specifically:
pacman -S mingw-w64-x86_64-clang mingw-w64-x86_64-lld
According to https://bugs.llvm.org/show_bug.cgi?id=40568
Linking against mingw import libraries from a normal mingw installation is a new feature, first present in LLD 8.
Unless wanting to compile a pre-release version of lld, have to wait for binary release of llvm 8.0.0. This will hopefully be sometime in March.

Link libsndfile source code with gcc

I just cloned libsndfile and created program.c file with the following contents:
#include <stdio.h>
#include <sndfile.h>
main() {
printf("hello world");
}
My goal is to get this file to compile using gcc (if indeed sndfile.h is the right header to include), however, I am not competent with c code nor the gcc compiler.
So far I've been referencing the gcc docs and the libsndfile FAQ and haven't been able to come up with a solution. I'm not sure if I even have a 'library' yet to feed the gcc -l option. Do I need to go through some sort of build process with the libsndfile source code first or can I 'link' to .c files?
So far, with program.c and the libsndfile clone directory in the same directory, I've tried the following:
gcc 'pkg-config --cflags libsndfile' program.c -o hello
gcc 'pkg-config --cflags ./libsndfile/sndfile.pc.in' program.c -o hello
I'm coding on my raspberry pi model B and did not install libsndfile, so, nothing is on my path... maybe I should rename sndfile.pc.in to sndfile.pc and somehow put it on my path? (not a linux guru either!)
If there are some resources you think I should study please comment! I'm perfectly willing to accept answers that simply push me enough to figure things out. Any help would be much appreciated.
First make sure you have gcc installed:
sudo apt-get install gcc
Then, install libsndfile1-dev:
sudo apt-get install libsndfile1-dev
I slightly fixed you code to avoid some warnings:
#include <stdio.h>
#include <sndfile.h>
int main(void) {
printf("hello world\n");
return 0;
}
Now you can compile:
gcc -o hello program.c
Execution:
bebs#rasp:~$ ./hello
hello world
bebs#rasp:~$
A small supplement of #alpereira7's answer.
It would cause linking failure without its lib. gcc -lsndfile -o hello program.c should be a 100% solution.

Linking C library to R

I recently found this C library (http://libxlsxwriter.github.io/), and attempted to use it with R.
Getting the C library to work by itself was not difficult. I downloaded zlib and libxlsxwriter using msys2, and ran make in the libxlsxwriter folder.
Now I can run this Hello-World example, lets call it test.c:
#include "xlsxwriter.h"
void main() {
lxw_workbook *workbook = workbook_new("myexcel.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
int row = 0;
int col = 0;
worksheet_write_string(worksheet, row, col, "Hello me!", NULL);
workbook_close(workbook);
}
Now I can compile test.c by running:
cc test.c -o test -lxlsxwriter -lz
And then run the executable:
./test
And now I have a Hello-World excel document.
Getting it to work with R has been much trickier. If I simply run:
R CMD SHLIB test.c
I get this error: ibxlsxwriter/include/xlsxwriter/common.h:19:42: fatal error: xlsxwriter/third_party/queue.h: No such file or directory
#include "xlsxwriter/third_party/queue.h"
Yet the file is clearly there when I check.
Any advice on how to connect this C library with R? At this point I am just trying to get the hello-world example to run from R.
Would it be a better approach to start out building a package, with xlsxwriter in the inst folder, and then try to write a makevars that will get xlsxwriter to compile correctly? I know I would have to include PKG_CPPFLAGS = -I../inst/libxlsxwriter but I am guessing I would need more than that.
You may want to try Continuum's Anaconda R packages. They use MSYS2 packages fairly directly. The toolchain package is called m2w64-toolchain and the posix package is sometimes useful for building R packages too.
conda install -c r r-essentials m2w64-toolchain posix
Disclaimer: I work for Continuum, but I also work on MSYS2.

How do I add a directory to C header include path?

I am having trouble installing a dependency for a program that itself depends on pcre.h. I have this installed to /opt/local/include, but the C compiler does not see it and thus gives me:
error: pcre.h: No such file or directory
I have confirmed this by writing a hello world program that tries to include it:
#include <pcre.h>
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
This also gives the error unless I specify the path as </opt/local/include/pcre.h>.
I would like the C compiler to find this by default but I do not know where this is configured. Tab completion hasn't revealed any HEADER_PATH environment variables and I cannot find anything like it that isn't specific to XCode. I am, however, using Mac OSX Snow Leopard on the off chance that makes a difference.
Use -I /opt/local/include on the command line or C_INCLUDE_PATH=/opt/local/include in the environment.
Use the pcre-config utility to get the right flags:
$ pcre-config --libs --cflags
-L/opt/local/lib -lpcre
-I/opt/local/include
If you're compiling via the command line,
$ gcc -Wall -g `pcre-config --libs --cflags` main.c

Resources