Porting Code and File Organization

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Porting Code and File Organization

Postby gregstewart90 » Wed Mar 22, 2017 11:36 pm

I am using Eclipse to develop, and currently everything is located in the main directory. When I create a subdirectory and move my .c files to that subdirectory, the compiler gives an "undefined reference to function_name()" error. This means, as I understand, that the .c file in the subdirectory is not being compiled.

Example:

/main/
main.c

/main/test_directory/
test.h
test.c

Inside /main/main.c I cannot call any functions in the test.c file.

I tried updating the Makefile in the main directory.

Code: Select all

TOP_DIR := $(shell pwd)

PROJECT_NAME := app-template

EXTRA_COMPONENT_DIRS := $(TOP_DIR) $(TOP_DIR)/main/tester/


include $(IDF_PATH)/make/project.mk
I also want to port the libssh and protobuf libraries so I can use them in the ESP32. Any help would be greatly appreciated.

Thanks!

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Porting Code and File Organization

Postby ESP_Angus » Thu Mar 23, 2017 5:53 am

Hi Greg,

Even inside Eclipse, IDF uses the Make-based IDF build system.

To add files inside "main", edit the component.mk file in the main directory and add a COMPONENT_SRCDIRS variable. The default value is "." but you can expand this to add more directories (relative to the directory containing component.mk)

For libraries like libssh or protobuf, I would recommend adding a new component ("main" is a single component, it's kind of a special component as it doesn't live in the components directory). Create a directory "components" in your project directory (the same directory with the Makefile). Add sub-directories inside "components" for each individual component (ie components/libssh, components/protobuf), and place a component.mk file in each subdirectory.

Putting the libraries into separate components makes it easier to share them (they are self-contained, they could be separate git submodules or repositories, etc). It also allows you to do things like override compiler settings for that particular component (can be useful to disable warnings, pass macro values, etc.)

For full details, check the build system documentation here: http://esp-idf.readthedocs.io/en/latest ... ystem.html .

Angus

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Re: Porting Code and File Organization

Postby gregstewart90 » Fri Mar 24, 2017 3:26 pm

Angus,

Thanks so much. COMPONENT_SRCDIRS worked perfect for me.

I am still having a hard time getting the libssh library to work. I created the components directory and put the libssh directory there.
my_project/components/libssh
I added the component.mk file which contains: COMPONENT_ADD_INCLUDEDIRS=.

When I #include <"libssh/libssh.h> it says "fatal error: libssh/libssh.h: No such file or directory"

The libssh directory contains a directory called "include" which contains "libssh" which contains "libssh.h". Full path is my_project/components/libssh/include/libssh/libssh.h. This directory only includes the header files. How does the .c files get compiled and used?

Thanks

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Porting Code and File Organization

Postby ESP_Angus » Mon Mar 27, 2017 1:04 am

gregstewart90 wrote: I added the component.mk file which contains: COMPONENT_ADD_INCLUDEDIRS=.

When I #include <"libssh/libssh.h> it says "fatal error: libssh/libssh.h: No such file or directory"

The libssh directory contains a directory called "include" which contains "libssh" which contains "libssh.h". Full path is my_project/components/libssh/include/libssh/libssh.h. This directory only includes the header files. How does the .c files get compiled and used?
In libssh/component.mk, try:

Code: Select all

COMPONENT_ADD_INCLUDEDIRS := include
This way "libssh/libssh.h" can be found relative to the "components/libssh/include" directory.

(This value for COMPONENT_ADD_INCLUDEDIRS is also the default if you don't set this variable at all, but it doesn't hurt to set it explicitly.)

Angus

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Re: Porting Code and File Organization

Postby gregstewart90 » Mon Mar 27, 2017 2:57 pm

In libssh/component.mk, try:

CODE: SELECT ALL
COMPONENT_ADD_INCLUDEDIRS := include


This way "libssh/libssh.h" can be found relative to the "components/libssh/include" directory.
That removed the missing "libssh/libssh.h" error. It then threw an error

Code: Select all

fatal error: sys/select.h: No such file or directory
I just commented it out, and it compiled. I added this code to the app_main() to test.

Code: Select all

ssh_session session;
	session = ssh_new();
	
It then said undefined reference to `ssh_new'. I figured it was because the c files were not compiled. I added COMPONENT_SRCDIRS := src to components/libssh/component.mk. The c files are located in the components/libssh/src directory. The compiler tried to compile these files, but it threw lots of errors.

Code: Select all

libssh/wrapper.h
error: unknown type name 'MD5CTX'
error: unknown type name 'SHACTX'
error: unknown type name 'SHA256CTX'
error: unknown type name 'SHA384CTX'
error: unknown type name 'SHA512CTX'
error: unknown type name 'EVPCTX'
error: unknown type name 'HMACCTX'

include/libssh/crypto.h:77:5: error: unknown type name 'bignum'
I resolved the above errors by including:

Code: Select all

typedef struct MD5Context MD5CTX;
typedef struct SHA_CTX SHACTX;
typedef struct SHA256_CTX SHA256CTX;
typedef struct SHA384_CTX SHA384CTX;
typedef struct SHA512_CTX SHA512CTX;
typedef struct EVP_CTX EVPCTX;
typedef struct HMAC_CTX HMACCTX;
bignum -> int
Not sure these are great changes anyways. Now it is throwing errors like "libssh/src/auth.c:1210:8: error: 'struct ssh_key_struct' has no member named 'rsa'". I know this library requires openssl. Any ideas?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Porting Code and File Organization

Postby ESP_Angus » Mon Mar 27, 2017 11:48 pm

gregstewart90 wrote: Not sure these are great changes anyways. Now it is throwing errors like "libssh/src/auth.c:1210:8: error: 'struct ssh_key_struct' has no member named 'rsa'". I know this library requires openssl. Any ideas?
Unfortunately you're at the "pointy end" of porting work now. :(. The struct name makes me think that struct is internal to libssh though, so missing member errors imply to me that maybe some include directories are still not being correctly included (are there errors higher up in the compiler output about unknown types?)

If libssh uses autoconf or something similar to add platform-specific definitions then you may need to mock up some of these platform-specific headers in order to integrate correctly into IDF's build system.

ESP-IDF contains an openssl compatibility layer (over mbedTLS) that exposes a lot of openssl functionality, but may be missing some parts that libssh requires. If there's something in particular that you find you need in order for libssl to work, feel free to open a feature request and we'll look into it.

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Re: Porting Code and File Organization

Postby gregstewart90 » Wed Mar 29, 2017 6:36 pm

I contacted libssh, and they said I need OpenSSL like you said. They said libssh has a strong dependency on libcrypto from OpenSSL or libgcrypt. Are either of these available? Without them, libssh will not compile.

Thank.

Carlitos5
Posts: 1
Joined: Mon Jun 11, 2018 7:03 pm

Re: Porting Code and File Organization

Postby Carlitos5 » Mon Jun 11, 2018 7:06 pm

Hi @gregstewart90, I'm also really interested in using protobufs (and gRPC) in the awesome ESP32, it would be great for all sorts of IoT applications and I believe it could benefit many people.

I'm curious to know if you ever managed to successfully port it to the ESP32, I'm having a really hard time trying to compile protobufs using the xtensa-esp32-elf-g++/xtensa-esp32-elf-gcc compilers :( Any help would be much appreciated!!

Who is online

Users browsing this forum: No registered users and 115 guests