samples/static_lib: Linking with a static library

This sample shows how to integrate a static libray into a Zephyr
application. A hello_world application and a small library are
included.

Origin: Original

Jira: ZEP-366
Change-Id: Idab38402b47042c3f9369b3a8e433d07d5fa4535
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
This commit is contained in:
Flavio Santes 2016-06-08 17:10:56 -05:00 committed by Inaky Perez-Gonzalez
commit b2383406cc
10 changed files with 225 additions and 0 deletions

View file

@ -0,0 +1,38 @@
#
# Copyright (c) 2016 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
MYLIB = mylib/lib/libmylib.a
all:
make clean
make mylib
make hello_world
hello_world: $(MYLIB)
make -C hello_world
$(MYLIB):
make -C mylib
run:
make -C hello_world qemu
pristine:
make -C mylib clean
make -C hello_world pristine
clean:
make pristine

30
samples/static_lib/README Normal file
View file

@ -0,0 +1,30 @@
Linking with a static library
#############################
This sample shows how to link a static library to a Zephyr application.
A sample library is also included.
Read mylib/Makefile to discover how to use the Zephyr's toolchain
to build a static library.
If the library's source code is available, perhaps it could be more
easier to integrate that source code to your application than creating
the static library.
Build instructions
==================
1. It is assumed that ZEPHYR_GCC_VARIANT and ZEPHYR_SDK_INSTALL_DIR
variables are already set. See:
https://www.zephyrproject.org/doc/getting_started/getting_started.html
2. source $ZEPHYR/zephyr-env.sh
Where $ZEPHYR points to the directory that contains the zephyr
repository.
3. make
4. make run

View file

@ -0,0 +1,3 @@
subdir-ccflags-y += -I$(SOURCE_DIR)/../mylib/include
obj-y += src/

View file

@ -0,0 +1,25 @@
#
# Copyright (c) 2016 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
KERNEL_TYPE = nano
BOARD = qemu_x86
CONF_FILE = prj.conf
export SOURCE_DIR = $(ZEPHYR_BASE)/samples/static_lib/hello_world
export LDFLAGS_zephyr += -L$(CURDIR)/../mylib/lib
export ALL_LIBS += mylib
include ${ZEPHYR_BASE}/Makefile.inc

View file

@ -0,0 +1 @@
CONFIG_STDOUT_CONSOLE=y

View file

@ -0,0 +1,17 @@
#
# Copyright (c) 2016 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
obj-y += main.o

View file

@ -0,0 +1,29 @@
/* hello world example: calling functions from a static library */
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <zephyr.h>
#include <stdio.h>
#include <mylib.h>
void main(void)
{
printf("Hello World!\n");
mylib_hello_world();
}

View file

@ -0,0 +1,36 @@
#
# Copyright (c) 2016 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
include $(ZEPHYR_BASE)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT)
# This Makefile is like a replacement of the $(ZEPHYR_BASE)/Makefile
# for libraries. So, we need to define ARCH here.
ARCH ?= x86
CROSS_COMPILE = $(CROSS_COMPILE_$(ARCH))
TOOLCHAIN_CFLAGS = $(TOOLCHAIN_CFLAGS_$(ARCH))
CC = $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar
all:
mkdir -p obj lib
$(CC) -c $(TOOLCHAIN_CFLAGS) -Iinclude src/mylib.c -o obj/mylib.o
$(AR) -rcs lib/libmylib.a obj/mylib.o
clean:
rm -rf obj lib

View file

@ -0,0 +1,22 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _MYLIB_H_
#define _MYLIB_H_
int mylib_hello_world(void);
#endif

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mylib.h"
#include <stdio.h>
int mylib_hello_world(void)
{
printf("mylib says: Hello World!");
return 0;
}