Create esp32 static library based on multiple components - components

I'm new to esp-idf developement and I want to build a static library for esp32.
I've read the espressif documentation (available here : Programming Guide) but I can't manage to create a proper .a file.
The idea is to develop a new library of wrapper and functions which include functions from esp-dsp library. I've add the esp-dsp component to my project and created my own component: my_component.
There is my project structure right now:
- first_project/
- CMakeLists.txt
- sdkconfig
- components/ - esp-dsp/ - CMakeLists.txt
- ...
- ...
- my_component/ - CMakeLists.txt
- my_component.c
- include/
- my_component.h
- main/ - CMakeLists.txt
- main.c
- build/
Following are the CMakeLists.txt files:
CMakeLists.txt (first_project)
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(first_test)
CMakeLists.txt (main)
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "."
REQUIRES my_component esp-dsp)
CMakeLists.txt (my_component)
idf_component_register(SRCS "my_component.c"
INCLUDE_DIRS "include"
REQUIRES esp-dsp)
In my_component.c there are functions from esp-dsp
With the previous files, I managed to create a binary to flash into ESP32 board but the next step is to build a static library (my_library.a) where there are functions inside my_component.c.
So I tried to modify CmakeLists.txt (main) in order to create the static library [MyStaticLib.a] regarding the component my_component
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(first_test)
ADD_LIBRARY( MyStaticLib STATIC
components/my_component/my_component.c )
SET( APP_EXE StaticTest )
ADD_EXECUTABLE( ${APP_EXE}
main/main.c )
TARGET_LINK_LIBRARIES( ${APP_EXE}
MyStaticLib )
However, during the build, I can't see the include path folder of my_component. Then the compilation failed because of my_component.h:No such file or directory
FAILED: CMakeFiles/MyStaticLib.dir/components/my_component/my_component.c.obj
ccache C:\Users\julien\.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe -mlongcalls -Wno-frame-address -g -MD -MT CMakeFiles/MyStaticLib.dir/components/my_component/my_component.c.obj -MF CMakeFiles\MyStaticLib.dir\components\my_component\my_component.c.obj.d -o CMakeFiles/MyStaticLib.dir/components/my_component/my_component.c.obj -c ../components/my_component/my_component.c
../components/my_component/my_component.c:2:10: fatal error: my_component.h: No such file or directory
The problem is only present when I modify the CMakeLists.txt from main to build the static library.
I though the variable INCLUDE_DIRS from CMakeLists.txt file could solved the problem but in vain.
Any idea where I'm doing wrong in CMakeLists.txt parameter in order to build .a static library to be used as a component in other project?
Regards.

Just build the example with your component added as you do.
You will see all the components got build as a library(.a) under build/esp-idf/ folder.
Use that libmy_component.a in other projects along with libesp_dsp.a.

Related

linking shared libraries with cmake on ubuntu

I am trying to link to shared object library (json-c) using cmake. I am a super beginner with cmake, and have tried the solutions in various SO posts to no avail.
I am following the directions laid out here
first step is to build the json-c library, which I did sucesfully.
Second step is linking to it. That's what I am having issues with. After following these instructions, my project directory structure looks like this:
Project Structure (before running make, build directory was empty)
base
|-json-c-master
|-libjson-c.so
|-libjson-c.a
|-json.h
:
:
:
|-CMakeLists.txt
|-Makefile
|-libjson-c.so
|-build
|-CMakeFiles
|-
:
:
|-Makefile.
|-src
|-main.c
|-project1.c
|-project1.h
The contents of my CMakeLists.txt in ~/base/project1/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(project1) set(CMAKE_CXX_STANDARD 14)
set(SOURCES src/main.c src/project1.h src/project1.c )
add_executable(project1 ${SOURCES})
find_package(json-c CONFIG)
target_link_libraries(${PROJECT_NAME} PRIVATE json-c::json-c)
then, I ran
cd build
cmake -DCMAKE_PREFIX_PATH=/home/<username>/base/project1/json-c-master ..
this populated the previously empty build directory.
then, I ran make
and I got the error that in project1.h, which #includes "json.h", the file "json.h" was not found.
How can I link json.h to project1.h?
I again appreciate your patience, I am a super noob with cmake and I've been pouring over their documentation and other SO posts to no avail.

How do I write the cmake script for clion to include glfw?

cmake_minimum_required(VERSION 3.15)
project(hello)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
set(CMAKE_CXX_STANDARD 11)
add_executable(hello main.cpp)
INCLUDE_DIRECTORIES(${GLFW_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(hello ${GLFW_STATIC_LIBRARIES})
It tells me
CMake Error at /home/user/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/193.5233.144/bin/cmake/linux/share/cmake-3.15/Modules/FindPkgConfig.cmake:696 (message):
None of the required 'glfw3' found
when I try to build it. My glfw folder is located at /usr/local/include/GLFW.
AFAIK, glfw3 is using CMake as the build system,
(src: packages.debian.org/fr/sid/amd64/libglfw3-dev/filelist)
which uses modern CMake, so you don't need GLFW_INCLUDE_DIRS etc...
Inside this file /usr/lib/cmake/glfw3/glfw3Targets.cmake (loaded by /usr/lib/cmake/glfw3/glfw3Config.cmake), you'll see:
...
# Create imported target glfw
add_library(glfw SHARED IMPORTED)
set_target_properties(glfw PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "GLFW_DLL"
INTERFACE_INCLUDE_DIRECTORIES "/usr/include"
)
...
So you can simply use:
find_package(glfw3 REQUIRED)
...
target_link_libraries(Foo glfw)
ps: same as my previous comment

cmake/cpack doesn't recognize inter-component dependencies

I am trying to convince cmake/cpack to build a shared library package (component lib) and a development package (component dev).
The lib component should only contain the shared library, and should have no dependencies.
The dev component should only contain the header file, and should depend on the lib component.
Here is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.5)
project(libmy)
add_library(my SHARED src/main/my.c)
target_include_directories(my PUBLIC include)
set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_CONTACT "peter.spierenburg#nautel.com")
install(TARGETS my
LIBRARY
DESTINATION /usr/local/lib
COMPONENT lib)
install(FILES include/my.h
DESTINATION /usr/local/include
COMPONENT dev)
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_ENABLE_COMPONENT_DEPENDS ON)
include(CPack)
cpack_add_component(lib REQUIRED)
cpack_add_component(dev DEPENDS lib)
A build nets me two deb packages libmy-0.1.1-Linux-dev.deb and libmy-0.1.1-Linux-lib.deb. However, the dev package does not depend on the lib package.

linking against library using VS and CMake on windows

I'm new to the world of cmake and linking to libaries. Now for a project I need to include a third party library in the current application. However I'm having problems linking to the library.
Here is what I have:
The library is build in the location D:/qwt-6.1.2, which contains:
the folder /src with the header and source files
the folder /lib which contains (qwt.dll, qwt.ext, qwt.lib, qwtd.dll, qwtd.ext, qwtd.ilk, qwtd.lib and qwtd.pdb.
I have tried to add the following to the cmake file:
add_executable(Demo main.cpp mainwindow.cpp mainwindow.h )
include_directories(D:/qwt-6.1.2/src)
link_directories(D:/qwt-6.1.2/lib)
target_link_libraries(Demo qwt)
The including goes fine since the intellisense of visual studio can the includes that I do, however when I build I get the error:
Error 1 error LNK1104: cannot open file 'qwt.lib'
Does anybody have an idea what I'm doing wrong? I don't care at the moment if it is statically or dynamically linked.

Building and using shared libraries with bjam

Using bjam on ubuntu, I am building a c++ shared library and trying to use it in an executable. I have to build as shared since it wont link as static (lots of undefined references arise). Thats fine.
Two related problems:
1) Using a heirarchy of Jamfiles, my exe project (testServerHub) has a dependency on the shared library (pythonManager). Here's the Jamfile for the exe:
echo "Compiling serverHub//test" ;
# declare project name
project serverHub//testServerHub
: build-dir ../_gcc/intermediate
;
# build unit-test using these source files, dependent libraries and settings
exe testServerHub
: # Source
..\\..\\..\\common\\0_8_1\\test\\runner.cpp
successfulTest.cpp
# Dependent libraries by path and project name
../controller/pythonManager//pythonManager
/boost//unit_test_framework
: # Settings
<link>shared
;
install ..\\bin : testServerHub ;
And here's my lib Jamfile:
echo "Compiling serverHub/controller//pythonManager" ;
# declare project name
project serverHub/controller//pythonManager
: requirements
<define>URTH_SERVERHUB
: build-dir ../../_gcc/intermediate
;
# build library using these source files and settings
lib pythonManager
: ../../../../common/0_8_1/controller/pythonManager/pythonManager.cpp
../../../../common/0_8_1/controller/pythonManager/cppInterfaceBase.cpp
cppInterfaceServerHub.cpp
/boost/python//boost_python
/user-config//python
: <link>shared
;
# copy and rename
install ../../lib : pythonManager ;
If I run 'bjam pythonManager' the pythonManager shared library is built and copied to my project lib folder (by the final install command). However if I run 'bjam test', both testServerHub and pythonManager are built, but the libpythonManager.so is not copied to the project lib folder - the install command doens't run!
2) Okay, so as a temporary workaround, I build libpythonManager.so first and then build testServerHub executable. Both compile and link. At runtime, the executable complains about not being able to find libpythonManager.so. Not a great surprise since the runtime linker doesn't know about my project lib folder. How do I tell it to look in a certain directory for shared libraries? or how do I install libpythonManager.so into /usr/local/lib if the install command has no effect on dependent library builds?
Thank you very much
Si
I think that you could use <install-dependencies>on in the exe Jamfile, like in
install ..\\bin : testServerHub : <install-dependencies>on <install-type>LIB ;
This will install all the libraries (LIB) on which the exe depends.
See eg http://www.boost.org/doc/tools/build/doc/html/bbv2/tasks/installing.html as a reference.

Resources