I’m using mbed-tools and cmake to build projects.
Generally you put your source in subfolders. This requires you to put the following in your CMakeLists.txt file (the one in the project root folder):
so MBED_PATH can be relative to the app. With the Mbed generated CMakeLists.txt, cmake will complain about the outside directory, therefore you have to supply a binary directory in add_subdirectory.
The mbed-tools compile command needs also the path to the mbed-os dir: mbed-tools compile --mbed-os-path ../../mbed-os -t ... -m ...
Maybe it will work also that mbed-os is included in a mbed-os-static lib to build a static lib once.
@hudakz I remember that you also had this problem with an external mbed-os directory.
I don’t have a CMakeLists in the root, I’m working in one project and e.g. start from app/app1.
yes, because the projects CMakeList needs to be modified to use a different mbed-os path, then it will not matter if Linux or Windows. I will tests this also on Linux, I’m using both.
This is one of the differences between CLI1 and 2, some things that CLI1 managed are now done in cmake. Migration guide - Build tools | Mbed OS 6 Documentation
I don’t have a CMakeLists in the root, I’m working in one project and e.g. start from app/app1.
Neither my root directory of applications (app) contains a CMakeLists.txt file. To create a new ~/app/app1 program I navigate (cd) to the ~/app directory and then I create a new project, as suggested by the CLI2 documentation, by using the command:
mbed-tools new -c app1
...
The shared mbed-os directory could be located for example in the ~/sys directory.
yes, because the projects CMakeList needs to be modified to use a different mbed-os path, then it will not matter if Linux or Windows.
I tried to change the mbed-os path by using the --mbed-os-path option, as suggested in the CLI2 documentation and also manually by editing the CMakeLists.txt file. Unfortunately, neither method worked (on Linux). However, when I kept the CMakeLists.txt file (generated by the mbed-tools new -c app1 command) unchanged and created a symbolic link to the ~/sys/mbed-os directory then the build worked.
Thank you Johannes, that works (also with full path to the shared mbed-os) Actually, I did the first modification. But in my attempts I failed to add ${CMAKE_CURRENT_BINARY_DIR} in the second line. Why we have to add this to make it work?
that is neccessary for the location of the binary file. In cmake_build dir, there is a CMakeFiles/projectname.dir where the compiled object files are stored.
When a lib is in the project tree, then cmake uses this subdirectory also for the object files.
When the lib is somewhere outside, this binary dir info is used for the object files. cmake produces some generated dir names also, don’t know why, but the generated temp dir should not matter.
I’m not saying memap is not working, it is, but it might expecting stuff to look a certain way.
For example we used to name our library target libSomethingCool and memap would not like that because it looks for the lib string and so the output was completely broken.
Windows might be adding something to the string that breaks the output and shows you the full path instead.
I could fix the problem by adding set(CMAKE_OBJECT_PATH_MAX 512)
to the projects CMakeLists.txt. But this maybe not a generic solution, it must be checked if windows support for filenames longer than 260 chars is enabled and also the toolchain must handle this. For gcc, it seems to be ok.
It seems that the discussion has moved to another direction, but let me try to address the original issue.
As mentioned by Ladislas, static Mbed library is not yet supported. Mbed is compiled as an CMake INTERFACE library, which leads to the issue of recompilation of Mbed for each non-INTERFACE target.
I did not try out the mbed_create_distro(), but it seems to be a workaround for the issue if you are actually building multiple binaries. However, if you are building a single binary, I would recommend declaring your libraries as INTERFACE libraries too.
This way, Mbed will not be compiled separately for that library. Of course, this extends the problem if you are building multiple binaries, as your libraries will be compiled multiple times, too. I believe however that this is not the case for most people. Again, this is not the ideal way but another workaround for the issue, so maybe mbed_create_distro() is actually the better way to go.
I’m not convinced that static libs are an overall solution. I have used lvgl and this compiles by default as static lib, but then lto optimization is not applied. This leads to 100+ kb larger binaries.
For my use case where I have one project, but shared Mbed and libs, interface is working. Yes, every project need to compile mbed-os and libs, but the projects can have different options and then a common static will not fit all. For a project with several targets and same mbed-os it will be better, I understand this. But the question is here also about optimization, how is the size for a binary compared to linked with static / interface lib?