how to link the openssl library with the arm-cross compiler - c

I have application test.c which by using gcc on host(on ubuntu) machine i have succeed in compilation and successfully ran the application program on host.
now I would like to cross compile the same application with arm-cross compiler for LPC1788. please guide me how to link the openssl library files
My Mkakefile with GCC
CC = gcc
CFLAGS = -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_NO_XKMS=1
-DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -I/usr/include/xmlsec1
-I/usr/include/libxml2 -DXMLSEC_OPENSSL_097=1
-DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_CRYPTO=\"openssl\ -DUNIX_SOCKETS -D XML_SECURITY
LDFLAGS = -lcrypto -I/usr/include/libxml2 -lxml2 -I/usr/include/xmlsec1 -lxmlsec1
all:
$(CC) src/test.c -o test $(CFLAGS) $(LDFLAGS)
by changing the compiler I used the following Makefile
CC = /home/amarayya/doc/tools/arm-2010q1/bin/arm-uclinuxeabi-gcc
CFLAGS = -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_NO_XKMS=1
-DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -I/usr/include/xmlsec1
-I/usr/include/libxml2 -DXMLSEC_OPENSSL_097=1
-DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_CRYPTO=\"openssl\ -DUNIX_SOCKETS -D XML_SECURITY
LDFLAGS = -lcrypto -L/usr/include/libxml2 -lxml2 -L/usr/include/xmlsec1 -lxmlsec1
all:
$(CC) src/test.c -o test $(CFLAGS) $(LDFLAGS)
which leading to these errors
fatal error: openssl/rsa.h: No such file or directory
fatal error: openssl/rsa.h: No such file or directory
what causing these errors and how to over come

You cannot use your host libraries when compiling for a different architecture. First, you need to cross compile all non-standard libraries (libxml, libopenssl) for your target machine (i.e. ARM).
Basically, you need to download the source code for these libraries and configure it with
--host=arm-uclinuxeabi --prefix=SOME_HOST_DIR
(or something similar - you might check the README files)
assuming, that you have your cross compiler in PATH.
These libraries might also require more libraries to be cross compiled.
When compiling your application you should use these cross compiled libraries.

Related

Shared library disparity between Linux CentOS 7 and Ubuntu 20.04.1 LTS

I am porting a project written in C from a CentOS 7 (Core) to an Ubuntu 20.04.1 LTS (Focal Fossa) system. The project relies heavily on the <cpuset.h> library, and compiles and executes correctly on the CentOS system. However, when I try to use functions from cpuset.h on the Ubuntu system, I get 'undefined reference' errors.
The following code, stored in file test.c, compiles and runs correctly on CentOS:
#define _GNU_SOURCE
#include<stdio.h>
#include <cpuset.h>
int main(){
int x = cpuset_version();
printf("cpuset lib version: %d\n",x );
return 0;
}
How I compile:
gcc -Wall -O2 -std=gnu99 -g -lcpuset test.c -o test
Output:
[xxxx#CentOS]$ ./test
cpuset lib version: 3
However, when I try to compile the same test.c file on the Ubuntu system, I get this error:
xxxx#Ubuntu:$ gcc -Wall -O2 -std=gnu99 -g -lcpuset test.c -o test
/usr/bin/ld: /tmp/ccpxlk4F.o: in function `main':
test.c:8: undefined reference to `cpuset_version'
collect2: error: ld returned 1 exit status
Furthermore, this is not limited to the <cpuset.h> library. I tried to use a simple function from <pthread.h> and it also gave me the same error. Can anyone help with identifying why I cannot use shared libraries on the Ubuntu system? Thanks in advance
Since OP's issue is wrong order of parameters to GCC (many guides do show an incorrect order!), as discussed in the comments to the question, I believe showing a minimal Makefile to handle these is warranted:
CC := gcc
CFLAGS := -Wall -O2 -g
LDFLAGS := -lcpuset
TARGETS := test
.PHONY: all clean
all: $(TARGETS)
clean:
rm -f *.o $(TARGETS)
%.o: %.c
$(CC) $(CFLAGS) -c $^
test: test.o
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $#
Note that the indentation in Makefiles must use Tabs and not spaces. Since this forum converts Tabs to spaces, you will need to fix the above makefile, for example by running sed -e 's|^ *|\t|' -i Makefile.
If you want to compile say foo.c directly to an executable, the recipe is
foo: foo.c
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $#
You only need to run make (it defaults to using the Makefile in the current directory, and the default target is the first one, above the one named all), to recompile the TARGETS (here, test, but you can supply more by just adding them space-separated to the line).
You can also run make clean test to rebuild test from "scratch", i.e. removing all temporary files and all targets first.
You can override variables like CFLAGS by simply supplying them on the command line; for example, make CFLAGS="-Wall -Wextra -Os" clean all to recompile everything with different compilation flags.

/usr/bin/x86_64-linux-gnu-ld: cannot find?

I study C language in ubuntu 18.04(gcc 7.3)
When the make all command is entered, this error occurrs:
/usr/bin/x86_64-linux-gnu-ld: can not find -lkeccak
The Makefile is as follows.
CC=gcc
CFLAGS=-03 -fomit-frame-pointer -msse2avx -mavx2 -march=native -std=c99
all:
$(CC) $(CFLAGS) -c Lizard.c main.c randombytes.c sha512.c
$(CC) $(CFLAGS) -o Lizard Lizard.o main.o randobytes.o sha512.o -lkeccak
run: all
./Lizard
new:
make clean
make all
./Lizard
Currently the libkeccak.a file is in the same directory as the Makefile and
it is also in the /usr/include directory.
I do not know the solution method at all.
Please, Help me.
The -l option is for linking dynamic libraries (like libkeccak.so). Static libraries are linked into the executable already if they're in one of the "standard" directories, so there's no need to provide the option -lkeccak.
If you want to explicitly tell GCC to link a static library,
gcc -l:/path/to/libkeccak.a

Writing a make file for GLFW3

i'm writing an emulator program and I need a graphics library. I have 4 files, the graphics library GLWF3 is installed in my includes folder. Im using MacOs Yosemite. I can't figure out how to get the makefile working though to include the glfw3 library. Thanks in advance!
Also note the only file including GLWF3 is graphics.h
Makefile
OBJ = graphics.o chip8.o
emulator: $(OBJ)
gcc -o emulator $(OBJ)
graphics.o: graphics.c graphics.h
gcc -c graphics.c
chip8.o: chip8.c chip8.h
gcc -c chip8.c
clean:
rm -f $(OBJ) emulator
To build with a given library, you have to:
tell the compiler how to find library header file
tell the linker which what library at must link.
Compilation
To tell where are the headers, you must pass a -I/path/to/dir option to gcc. Often, the make CFLAGS variable is used to do so:
CFLAGS= -I/path/to/glfw/include/dir
graphics.o: graphics.c graphics.h
gcc -c graphics.c $(CFLAGS)
chip8.o: chip8.c chip8.h
gcc -c chip8.c
Link
To tell linker what library to use, and where it is located, option -L/path/to/sofile and -lthelib are used. Usually in LDFLAGS variable:
Warning: The -l options must come after the files to link (*.o files)
LDFLAGS = -L/path/to/libglfw/lib/dir
# if the so file name is "libglfw3.so", the "-l" option must be "-lglfw3"
LDFLAGS += -lglfw3
emulator: $(OBJ)
gcc -o emulator $(OBJ) $(LDFLAGS)
pkg-config
To not to have to deal with paths, you can use pkg-config tool: This tool will help you to set CFLAGS and LDFLAGS variables. See here for installation instructions..
Hence, you makefile will looks like:
OBJ = graphics.o chip8.o
# calling program "pkg-config" and store result in CFLAGS variable
CFLAGS = $(shell pkg-config --cflags glfw3)
# calling program "pkg-config" and store result in LDFLAGS variable
LDFLAGS = $(shell pkg-config --ldflags glfw3)
emulator: $(OBJ)
gcc -o emulator $(OBJ) $(LDFLAGS)
graphics.o: graphics.c graphics.h
gcc $(CFLAGS) -c graphics.c
chip8.o: chip8.c chip8.h
gcc $(CFLAGS) -c chip8.c
clean:
rm -f $(OBJ) emulator

How to properly link msgpack into a shared library

I'm trying to compile a few .c files that used msgpack-c functions into a shared library. I have the following Makefile:
MSGPACK_CS = msgpack.c
CFLAGS = -std=c99
MSGPACK_OBJECTS = $(subst .c,.o,$(MSGPACK_CS))
MSGPACK_LIBS = msgpack.so
all: $(MSGPACK_OBJECTS) $(MSGPACK_LIBS)
%.o: %.c
$(CC) -c -shared -fPIC $(CFLAGS) $<
$(MSGPACK_LIBS): $(MSGPACK_OBJECTS)
ld -Lmsgpack/.libs -share -o $# $(MSGPACK_OBJECTS) -lmsgpack
I can compile a program that uses msgpack without problem, but this gives me the following error:
msgpack.o: In function `msgpack_pack_int64':
/usr/local/include/msgpack/pack_template.h:373: undefined reference to `__stack_chk_fail_local'
ld: msgpack.so: hidden symbol `__stack_chk_fail_local' isn't defined
ld: final link failed: Bad value
Apparently the linkage process isn't going well, but I don't know what is wrong. What can I do?
Try linking with the gcc driver instead of calling ld directly. ld doesn't know about the gcc support libs that are needed for the C runtime:
gcc -Lmsgpack/.libs -shared -o $# $(MSGPACK_OBJECTS) -lmsgpack
If this still doesnt't work, you might need to add -fno-stack-protector to your CFLAGS to supress runtime stack checking.

Statically linking C++ libraries is failed

I am interested in including C++ language in my c based program. I have two systems (Host and Target) which target system is limited, especially in memory. So, in my host machine, I have to statically link some libraries which will be needed by the program on target side. However, in my makefile I have been manipulating gcc patch (using a variety of gcc flags, during both compiling and linking steps).
Everything is ok until using C++ language;
For example, By including iostream header file in .cpp file, there exist an error on the target system showing that : "can not resolve symbol 'wctob' "
I must indicate my makefile as bellow:
in the compiling step:
$(CXX) -O0 -I<Headers dir> $(LIBSC) -Wno-write-strings -Wno-narrowing -Wno-return-type -Wno-abi -Wno-unused-variable -DNDEBUG -Wa, -c -fmessage-length=0 -I<Headers dir> -march=4kec -EL -o "$#" "$<"
in the linking step:
$(CXX) -lpthread -Wl,-Map,output.map $(MyFlags) $(LIBS) $(STRIP) -muclibc -march=4kec -EL -o
"output.elf" $(OBJS) $(LIBS) $(MyFlags) -Wl,-rpath -Wl,<LIBDIR> -L<LIBDIR>
------some definitions
MyFlags = -lc -static-libstdc++ -static-libgcc;(such flags used in the linking step)
LIBDIR = is the address of needed libraries
LIBSC = -nostdinc++ ;(such flags used in the compiling step)
Headers dir = is the address of needed both c and c++ header files included by the programmer.
LIBS and OBJS are also the list of needed libraries and objects.
Also, I added LIBDIR to ld.so.conf and LD_LIBRARY_PATH, but the problem is not solved far away. (My g++ version is 4.7.3.)
Can anybody help me out?
You have to tell C++ compiler to use C naming and symbols. so :
extern "C" {
// your cpp codes
}

Resources