how to use my ".a" library (aka let ESP-IDF generate a ".h" file)

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

how to use my ".a" library (aka let ESP-IDF generate a ".h" file)

Postby Gfast2 » Sat Dec 09, 2017 8:16 pm

Hi ESP-IDF,

This is really an ancient question. I've tried to ask really awesome questions which can descript my problem without pain. And this is my new try.

Now I'm trying to port a library that uses "cmake" built it self firstly. I've successful let ESP-IDF drive cmake did most of the job. (This is the demo project, P.s:take care, it uses git submodule, if you check that out)

After the compiling, I got the result I need:

Code: Select all

gfast2@gfast2-VB:~/workspace/open62541_port03/build/open62541$ ll
total 2132
drwxrwxr-x  8 gfast2 gfast2    4096 Dez  9 20:55 ./
drwxrwxr-x 48 gfast2 gfast2    4096 Dez  9 20:56 ../
drwxrwxr-x  2 gfast2 gfast2    4096 Dez  9 20:55 bin/
drwxrwxr-x  2 gfast2 gfast2    4096 Dez  9 20:55 cmake/
-rw-rw-r--  1 gfast2 gfast2   21937 Dez  9 20:55 CMakeCache.txt
drwxrwxr-x 14 gfast2 gfast2    4096 Dez  9 20:55 CMakeFiles/
-rw-rw-r--  1 gfast2 gfast2    5865 Dez  9 20:55 cmake_install.cmake
-rw-rw-r--  1 gfast2 gfast2    6085 Dez  9 20:55 cmake_toolchain_file.txt
-rw-rw-r--  1 gfast2 gfast2     519 Dez  9 20:55 component_project_vars.mk
-rw-r--r--  1 gfast2 gfast2    3910 Dez  9 20:55 CPackConfig.cmake
-rw-r--r--  1 gfast2 gfast2    4409 Dez  9 20:55 CPackSourceConfig.cmake
drwxrwxr-x  3 gfast2 gfast2    4096 Dez  9 20:55 doc/
drwxrwxr-x  2 gfast2 gfast2    4096 Dez  9 20:55 doc_src/
-rw-rw-r--  1 gfast2 gfast2       8 Dez  9 20:55 libopen62541.a
-rw-rw-r--  1 gfast2 gfast2   13465 Dez  9 20:55 Makefile
-rw-rw-r--  1 gfast2 gfast2 1513283 Dez  9 20:55 open62541.c
-rw-r--r--  1 gfast2 gfast2    1269 Dez  9 20:55 open62541ConfigVersion.cmake
-rw-rw-r--  1 gfast2 gfast2  544521 Dez  9 20:55 open62541.h
drwxrwxr-x  2 gfast2 gfast2    4096 Dez  9 20:55 src_generated/
If I don't turn on this library's compiling feature: "Amalgamation", which will generate "open62541.h/.c" file. What i got is only the "libopen62541.a". And I don't know how to use the APIs in it. If I add "#include "open62541.h" in my "main.c" The compiler can not find it as I expected.

What I've done is: Just copy/past the generated open62541.h and open62541.c from the first compiling result (this compiling breaked out because of the missing open62541.h) into "/main" and recompile it again. It works, but still not awesome.

I believe I should spent some time on the component.mk. But I still missing the eye balls of a really senior level developer. :shock:

So am I have the correct suspection? Any possible tip to don't use "AMALGAMATION" feature, and call APIs from "libopen62541.a" directly?

Cheers

Gfast2

ESP_Sprite
Posts: 9024
Joined: Thu Nov 26, 2015 4:08 am

Re: how to use my ".a" library (aka let ESP-IDF generate a ".h" file)

Postby ESP_Sprite » Sun Dec 10, 2017 2:06 am

Okay, you can go two ways: either you use non-amalgamated mode, meaning you do not have an open62541.h but have to include the files you need from open62541/include/ manually, or you can use amalgamated mode and add the build directory to the include path. I assume you want to do the latter; most open62541 examples seem to be geared towards that process.

It looks like open62541.h is generated by the CMake process. All generated files end up in the build/, so this one as well (namely /build/open62541/open62541.h). According to http://esp-idf.readthedocs.io/en/latest ... ystem.html , the variable used to tell other components where to find this file is COMPONENT_ADD_INCLUDEDIRS. While we can add the build path to this hardcoded, the docs also tell us COMPONENT_BUILD_DIR is set to the component build dir (/your/project/build/open62541), so adding $(COMPONENT_BUILD_DIR) to the COMPONENT_ADD_INCLUDEDIRS:= line in components/open65241/component.mk should fix this. But Wait: COMPONENT_BUILD_DIR is an absolute path, and COMPONENT_ADD_INCLUDEDIR only supports relative paths... Hmm. There's no way to add absolute paths to the project include dirs in esp-idf right now, so the 'nicest' way to handle this is to do COMPONENT_ADD_INCLUDEDIRS:= ../../build/open65241 . I'll see if we can make something so this can be done in a better way in the future. But with all this, the thing compiles without the copied .c/.h-file.

The only problem is that the project doesn't *link* yet, I think because algamagated mode doesn't generate a .a file by itself. You could solve this by simply creating an empty .c file somewhere and including the open62541.c file there, or by figuring out a nicer way by possibly compiling the generated .c file somewhere in the component makefile.

(Another thing: Because your main component (the one containing main.c) needs to be compiled *after* the open62541 component is compiled, it's probably a good idea to add 'COMPONENT_DEPENDS := open62541' to your main/component.mk)

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

Re: how to use my ".a" library (aka let ESP-IDF generate a ".h" file)

Postby Gfast2 » Sun Dec 10, 2017 6:24 am

H iAngus,

Your declaration is super clear and clean. I will try these suggestions.

Thanks a lot!

cheers

Gfast2

Who is online

Users browsing this forum: Bing [Bot] and 134 guests