PlatformIO Community

LDF error when trying to include from "include" Dir into lib in the "lib" dir

I have the Folowing dir stracture:

-include+
- foo.h
-lib+
- mylib+
- mylib.hpp
- mylib.cpp
-src+
- main.cpp

foo.h>>
#ifndef xxxfooxxx
#define xxxfooxxx
// Code Code Code
#endif

mylib.hpp>>
#ifndef xxxmylibxxx
#define xxxmylibxxx

#include <foo.h>
// Code Code Code
#endif

main.cpp>>
#include <foo.h>
#include <mylib.hpp>

I am getting the Error:
In file included from lib\mylib\mylib.cpp:1:0:
lib\mylib\mylib.hpp:11:24: fatal error: foo.h: No such file or directory

The only way I found around it is to put a full path in the #include statement inside “mylib.hpp”
aka: mylib.hpp>>
#include “C:\Users\myUser\Documents\PlatformIO\Projects\RO\include\foo.h”

Stab in the dark here, does setting

lib_ldf_mode = chain+

in your platformio.ini fix it? If it does, read here to find out more about what that flag does.

No.
Also triad deep/deep+

VS Code Intelisense don’t show errors.
It’s just when I try to compile.

1 Like

Eyes are glazing over, so I’ll look in the morning, unless someone else jumps in in the meantime :wink:

Isn’t the build system such that libraries are compiled in isolation?

If so, they can only access their source code plus the code of their own dependencies. They see neither the code of the main project nor of sibling or parent libraries.

So they way to inject configuration data from the main project into libraries is to use defines in the platformio.ini file.

(The ESP-IDF framework is an exception. The sdkconfig.h file from the main project is available to all libraries.)

Yes.
The problem is that I have the same dependency both in the main.cpp code and in some of the libraries that I created.
For example a typedef that is defined in \include\my_types.h .
in the main source file, every thing is fine and I can write:
#include <my_types.h>

but in a library I created, \lib\mylib\mylib.hpp ,
I must put a full path, “c:\Users…Projects\myProject\include\my_types.h”
Or I am getting the before mentiond error during build time.

Clearly your library depends on the main project. I don’t think that’s supported. Usually it’s the other way round.

Either move the typedef into the library or integrate all the library’s source code directly into the main project.

The idea behind creating this libraries is to do some code refractoring and not have every thing in the main project file.

Still,
There is no reason that I shuld not be able to include a header file from the “include” folder into a library that is in the “lib” folder.

Bump,

so is there a solution for this one?

Your intention to do code refactoring is good. However, the result is strange. The main project depends on the shared code and the shared code depends on the main project. Such cyclic dependencies are not supported by the library concept of PlatformIO as far as I can tell.

There is even a software metric for it: cyclomatic complexity. The higher this metric is, the lower is the software quality. There are tools to anlyze it so you can lower it and improve the software quality.

I strongly suggest you refactor your code such that the shared code does not depend on the main project. The code will be less complex and fully supported by PlatformIO.

1 Like