I have two separate projects that rely on the same external dependency in an embedded environment. I would want to share the code of that dependency between the projects.
So far I split those functions off into their own section via linker script. After linking one project I can have a map of all functions (with the -Map option in ld), but now I can't link the second project against this.
I thought of having a script that parses the map file and generates a header to define all the functions as explicit offsets by parsing the original headers of the external dependency (e.g. typedef int (*external_add_t)(int, int); external_add_t external_add = (external_add_t)(0x8dff000000);
) then add the results as headers in the second project.
I'm really hoping there's a better way to do that. Anyone tried something similar?
Example:
project1/
src/project1.c
project1.ldS
external_lib (submodule with CMake, static dependency)
CMakeLists.txt
project2/
src/project2.c
project2.ldS
external_lib (submodule with CMake, static dependency)
CMakeLists.txt
external_lib/
src/external_add.c
include/external_add.h
#include "external_add.h"
void project_entry_point() {
// This part is different between project1 and project2
const uint32_t param = 1;
external_add(param, 2); // But this is called from both
}
project1 is compiled and the resulting binary has 0x00008d7fe0089000 external_add
, project2 is compiled and the resulting binary has 0x00008d7fe0089008 external_add
. The desired result is to get project2 to reuse the same external_add
produced by the project1 binary.